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

  .minimenuDiv .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="minimenuDiv">
    <div class="query-box">
      <button type="button" class="normalBtn" @click="setMiniProgramPage()">保存</button>
      <button type="button" class="normalBtn" @click="CommonJump('minimenu',{})">小程序菜单</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.PageTypeName}}
          </el-checkbox>
        </td>
        <td style="text-align:left; padding-left:5px;">
          <template v-for="(subItem,subIndex) in item.SubMenuList">
            <el-checkbox v-model="subItem.IsChecked" :key="subIndex">{{subItem.PageName}}</el-checkbox>
          </template>
        </td>
      </tr>
    </table>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        loading: false,
        dataList: [],
        pageType: [],
        MallBaseId: 0, //小程序Id
      };
    },
    created() {

    },
    methods: {
      ChangeItem(item) {
        if (item.SubMenuList && item.SubMenuList.length > 0) {
          item.SubMenuList.forEach(subItem => {
            subItem.IsChecked = item.IsChecked;
          })
        }
      },
      //获取分页数据
      getPageList() {
        this.apipost("/api/Tenant/GetMiniProgramePageManageList", {}, res => {
          if (res.data.resultCode == 1) {
            this.dataList = res.data.data;
          } else {
            this.Info(res.data.message);
          }
        })
      },
      //添加修改菜单
      setMiniProgramPage() {
        var setArray = []; //新增修改的数组
        var deleteArray = []; //删除的数组
        if (this.dataList && this.dataList.length > 0) {
          this.dataList.forEach(item => {
            if (item.SubMenuList && item.SubMenuList.length > 0) {
              item.SubMenuList.forEach(subItem => {
                if (subItem.IsChecked) {
                  setArray.push({
                    Id: subItem.Id,
                    PageId: subItem.PageId,
                    MallBaseId: this.MallBaseId
                  });
                } else {
                  deleteArray.push({
                    Id: subItem.Id,
                    PageId: subItem.PageId,
                    MallBaseId: this.MallBaseId
                  })
                }
              })
            }
          })
        }
        if (setArray.length == 0 && deleteArray.length == 0) {
          this.Info("请选择要操作的菜单!");
          return;
        }
        var msg = {
          setMsg: setArray,
          delMsg: deleteArray
        };
        this.apipost("/api/Tenant/SetMiniProgram_Page", msg, res => {
          if (res.data.resultCode == 1) {
            this.getPageList();
            this.Success(res.data.message);
          } else {
            this.Info(res.data.message);
          }
        })
      },
    },
    mounted() {
      this.MallBaseId = this.getLocalStorage().MallBaseId;
      this.getPageList();
    }
  };

</script>
