<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="套餐名称" prop="packageName">
        <el-input v-model="form.packageName" placeholder="请输入套餐名称" />
      </el-form-item>
      <el-form-item label="关联菜单">
        <el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>
        <el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event, 'menu')">全选/全不选 </el-checkbox>
        <el-checkbox v-model="form.menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">父子联动 </el-checkbox>
        <el-tree
            class="tree-border"
            :data="menuOptions"
            show-checkbox
            ref="menu"
            node-key="id"
            :check-strictly="!form.menuCheckStrictly"
            empty-text="加载中，请稍候"
            :props="{ label: 'label', children: 'children' }"
        ></el-tree>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ImageUpload from '@/components/ImageUpload/index'
import {
  addTenantPackage,
  getTenantPackage,
  updateTenantPackage
} from '@/api/system/tenantPackage'
import { tenantPackageMenuTreeselect, treeselect as menuTreeselect } from '@/api/system/menu'

export default {
  components: {
    ImageUpload,
  },
  data () {
    return {
      open: false,
      title: '',
      menuExpand: false,
      menuNodeAll: false,
      menuOptions: [],
      borrowList: [],
      form:{
        packageId: undefined,
        packageName: undefined,
        menuCheckStrictly: true,
        remark: undefined,
        status: '0',
      },
      rules:{
        packageName: [{ required: true, message: "套餐名称不能为空", trigger: "blur" }]
      },
      buttonLoading:false,
      packageList: [],
    }
  },
  created() {

  },
  methods: {
    init (packageId) {
      this.form.packageId = packageId || undefined;
      this.open = true;
      this.title = "新增租户套餐";
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        this.form.menuIds = undefined
        if(this.form.packageId){
          this.title = "修改租户";
          getTenantPackage(packageId).then(response => {
            this.form = response.data;
          });
          tenantPackageMenuTreeselect(packageId).then(res => {
            this.menuOptions = res.data.menus;
            let checkedKeys = res.data.checkedKeys
            checkedKeys.forEach((v) => {
              this.$nextTick(()=>{
                this.$refs.menu.setChecked(v, true ,false);
              })
            });
          })
        }else{
          this.getMenuTreeselect()
        }
      })
    },
    getMenuTreeselect() {
      menuTreeselect().then(response => {
        this.menuOptions = response.data;
      });
    },
    handleCheckedTreeExpand(value, type){
      if (type == 'menu') {
        let treeList = this.menuOptions;
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
        }
      }
    },
    handleCheckedTreeNodeAll(value, type){
      if (type == 'menu') {
        this.$refs.menu.setCheckedNodes(value ? this.menuOptions: []);
      }
    },
    handleCheckedTreeConnect(value, type) {
      if (type == 'menu') {
        this.form.menuCheckStrictly = value ? true: false;
      }
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if(!this.form.packageId){
            this.buttonLoading = true;
            this.form.menuIds = this.getMenuAllCheckedKeys();
            addTenantPackage(this.form).then(data => {
              this.$modal.msgSuccess("新增成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.buttonLoading = false;
            });
          }else{
            this.buttonLoading = true;
            this.form.menuIds = this.getMenuAllCheckedKeys();
            updateTenantPackage(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.buttonLoading = false;
            });
          }
        }
      })
    },
  }
}
</script>
<style>
</style>
