<style>
  .setmarketingplugDiv .query-box {
    width: 100%;
    padding: 0 0 20px;
    border-bottom: 1px solid #ccc;
    position: relative;
    text-align: right;
  }

  .setmarketingplugDiv .normalBtn {
    color: #fff;
    padding: 0 15px;
    height: 30px;
    background: #e95252;
    border: 1px solid #e95252;
    cursor: pointer;
    border-radius: 15px;
    margin-left: 10px;
    outline: none;
  }

</style>
<template>
  <div class="setmarketingplugDiv">
    <div class="query-box">
      <button type="button" class="normalBtn" @click="setMiniProgramPage()">保存</button>
      <button type="button" class="normalBtn" @click="CommonJump('pluglist',{})">插件中心</button>
    </div>
    <table class="commonTable" border="0" cellspacing="0" cellpadding="0" v-loading="loading">
      <tr>
        <th style="width:200px">插件类型</th>
        <th>插件名称</th>
      </tr>
      <tr v-for="(item,index) in dataList" :key="index">
        <td>
          <el-checkbox v-model="item.IsChecked" :key="index" @change="ChangeItem(item)">{{item.PlugTypeName}}
          </el-checkbox>
        </td>
        <td style="text-align:left; padding-left:5px;">
          <template v-for="(subItem,subIndex) in item.SubList">
            <el-checkbox v-model="subItem.IsChecked" :key="subIndex">{{subItem.PlugName}}</el-checkbox>
          </template>
        </td>
      </tr>
    </table>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        loading: false,
        dataList: [],
        TenantId: 0, //商户Id
      };
    },
    created() {

    },
    methods: {
      ChangeItem(item) {
        if (item.SubList && item.SubList.length > 0) {
          item.SubList.forEach(subItem => {
            subItem.IsChecked = item.IsChecked;
          })
        }
      },
      //获取分页数据
      getPageList() {
        this.apipost("/api/MContent/GetMarketingConfigList", {}, res => {
          if (res.data.resultCode == 1) {
            this.dataList = res.data.data;
          } else {
            this.Info(res.data.message);
          }
        })
      },
      //添加修改插件
      setMiniProgramPage() {
        var setArray = []; //新增修改的数组
        if (this.dataList && this.dataList.length > 0) {
          this.dataList.forEach(item => {
            if (item.SubList && item.SubList.length > 0) {
              item.SubList.forEach(subItem => {
                if (subItem.IsChecked) {
                  setArray.push({
                    PConfigId: 0,
                    PlugId: subItem.PId,
                    TenantId: this.TenantId
                  });
                }
              })
            }
          })
        }
        if (setArray.length == 0) {
          this.Info("请选择要操作的插件!");
          return;
        }
        this.apipost("/api/MContent/SetMarketingConfig", setArray, res => {
          if (res.data.resultCode == 1) {
            this.getPageList();
            this.Success(res.data.message);
          } else {
            this.Info(res.data.message);
          }
        })
      },
    },
    mounted() {
      this.TenantId = this.getLocalStorage().TenantId;
      this.getPageList();
    }
  };

</script>
