<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-col :span="24" class="one_1"> 权限分配 </el-col>
          <!-- <el-col :span="24" class="one_2">
            <el-form ref="searchFrom" :model="searchFrom" label-width="100px">
              <el-col :span="24" class="one_2txt">
                <el-col :span="8">
                  <el-form-item label="姓名">
                    <el-input v-model="searchFrom.name"></el-input>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :span="24" class="one_2txt">
                <el-col :span="24" class="btn">
                  <el-button type="primary" size="mini" @click="search">查询</el-button>
                  <el-button type="primary" size="mini" @click="show = true">添加</el-button>
                </el-col>
              </el-col>
            </el-form>
          </el-col> -->
        </el-col>
        <el-col :span="24" class="two">
          <data-table @query="search" :fields="fields" :opera="opera" :data="list" :usePage="false" @edit="toEdit"> </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog title="权限分配管理" :visible.sync="show" width="50%" :before-close="close" :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" readonly></el-input>
        </el-form-item>
        <el-form-item label="菜单" prop="menu">
          <el-cascader
            v-model="form.menu"
            :options="options"
            :props="{ multiple: true, value: 'num', label: 'title', children: 'subs' }"
            clearable
            collapse-tags
            style="width: 100%"
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
const { purpose } = require('@common/src/layout/deploy/dict');
const { companyMenu } = require('@common/src/layout/deploy/menu');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: systen_role } = createNamespacedHelpers('systen_role');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      // 查询
      searchFrom: {},
      // 列表
      fields: [{ label: '企业类型', model: 'name' }],
      opera: [
        {
          label: '权限管理',
          method: 'edit',
        },
      ],
      list: purpose,
      // 添加角色
      show: false,
      form: {},
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        num: [{ required: true, message: '请输入菜单', trigger: 'blur' }],
      },
      options: companyMenu,
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...systen_role(['query', 'create', 'update', 'fetch']),
    async search() {
      let list = _.cloneDeep(purpose);
      list = list.map((i) => {
        return { name: i.label };
      });
      this.$set(this, `list`, list);
    },
    async toEdit({ data }) {
      if (!data || !data.name) {
        this.$message.error('缺少要查询的信息');
        return;
      }
      const res = await this.fetch({ name: data.name });
      if (this.$checkRes(res)) {
        let resData = _.cloneDeep(res.data);
        if (resData) {
          if (resData.menu) resData.menu = JSON.parse(resData.menu);
        } else {
          resData = {};
          resData.name = data.name;
          resData.type = '1';
        }
        this.$set(this, 'form', resData);
      }
      this.show = true;
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let data = this.form;
          if (data.menu) data.menu = JSON.stringify(data.menu);
          if (data.id) {
            let res = await this.update(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `修改信息成功` });
              this.close();
            }
          } else {
            let res = await this.create(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `创建信息成功` });
              this.close();
            }
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 取消添加
    close() {
      this.form = {};
      this.show = false;
      this.search();
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    .one_1 {
      height: 40px;
      line-height: 40px;
      border-bottom: 1px solid #ccc;
      margin: 0 0 10px 0;
      font-size: 18px;
      font-weight: bold;
    }
    .one_2 {
      .one_2txt {
        border: 1px solid #ccc;
        /deep/.el-form-item {
          margin: 0;
        }
        /deep/.el-date-editor.el-input,
        .el-date-editor.el-input__inner {
          width: 100%;
        }
        /deep/.el-select {
          width: 100%;
        }
        .btn {
          text-align: right;
          padding: 5px 5px;
        }
      }
    }
  }
}
</style>
