<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <data-table :fields="fields" :opera="opera" :data="list" :total="total" @query="search" @edit="toEdit"></data-table>
      </el-col>
    </el-row>
    <el-dialog title="菜单信息管理" width="40%" :visible.sync="dialog" @closed="handleClose" :destroy-on-close="true">
      <data-form :data="form" :fields="formfields" :rules="rules" @save="toSave">
        <template #custom="{item, form}">
          <el-checkbox-group v-model="form.menus">
            <el-checkbox v-for="(i, index) in menuList" :key="index" :label="i.id">{{ i.title }}</el-checkbox>
          </el-checkbox-group>
        </template>
      </data-form>
    </el-dialog>
  </div>
</template>

<script>
import dataTable from '@common/src/components/frame/filter-page-table.vue';
import dataForm from '@common/src/components/frame/form.vue';
import { iconmenu } from '@common/src/util/iconmenu';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
const { mapActions: menu } = createNamespacedHelpers('menu');
export default {
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  name: 'index',
  props: {},
  components: {
    dataTable,
    dataForm,
  },
  data: function() {
    return {
      opera: [
        {
          label: '分配权限',
          method: 'edit',
        },
      ],
      fields: [
        { label: '机构代码或邀请码', prop: 'code' },
        { label: '用户名', prop: 'name' },
        { label: '联系电话', prop: 'phone' },
        { label: '机构名称', prop: 'deptname' },
      ],
      list: [
        {
          name: 'sadas',
          menus: [
            {
              id: '1',
              title: '菜单一',
            },
            {
              id: '2',
              title: '菜单二',
            },
          ],
        },
      ],
      total: 0,
      // 分配权限
      dialog: false,
      formfields: [
        { label: '用户名', model: 'name' },
        { label: '机构名称', model: 'deptname' },
        { label: '权限', model: 'menus', custom: true },
      ],
      form: {
        menus: [],
      },
      rules: {},
      // 菜单列表
      menuList: [
        {
          id: '1',
          title: '菜单一',
        },
        {
          id: '2',
          title: '菜单二',
        },
        {
          id: '3',
          title: '菜单三',
        },
      ],
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...adminLogin(['query']),
    ...menu({ menuQuery: 'query' }),
    // 查询列表
    async search({ skip = 0, limit = 10, ...info } = {}) {
      let res = await this.query({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        // this.$set(this, `list`, res.data);
        // this.$set(this, `total`, res.total);
      }
    },
    // 分配权限
    toEdit({ data }) {
      this.dialog = true;
      this.$set(this, 'form', { ...data, menus: data.menus.map(i => i.id) });
    },
    // 提交分配
    toSave({ data }) {
      console.log(data);
    },
    // 取消添加
    handleClose() {
      this.form = {};
      this.dialog = false;
      this.search();
    },
    // 查询其他信息
    async searchOther() {
      let res = await this.menuQuery();
      if (this.$checkRes(res)) {
        // this.$set(this, `menuList`, res.data);
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
};
</script>

<style lang="less" scoped>
.main {
  .add {
    text-align: right;
    margin: 0 0 10px 0;
  }
}
</style>
