<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight">
        <el-col :span="24" class="one">
          <search-1 :form="searchForm" @onSubmit="search" @toReset="toClose"></search-1>
        </el-col>
        <el-col :span="24" class="two">
          <data-btn :fields="btnList" @add="toAdd"></data-btn>
        </el-col>
        <el-col :span="24" class="thr">
          <data-table
            :fields="fields"
            :opera="opera"
            :vOpera="false"
            :data="list"
            @query="search"
            :total="total"
            @edit="toEdit"
            @del="toDel"
            :select="true"
            :selected="selected"
            @handleSelect="handleSelect"
          >
          </data-table>
        </el-col>
      </el-col>
    </el-row>
    <e-dialog :dialog="dialog" @toClose="toClose">
      <template v-slot:info>
        <form-1 v-if="dialog.type == '1'" :form="form" @onSubmit="onSubmit" @toReset="toClose" :menuList="menuList" :typeList="typeList"></form-1>
      </template>
    </e-dialog>
  </div>
</template>

<script>
import search1 from './parts/search-1.vue';
import form1 from './parts/form-1.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('role');
const { mapActions: menu } = createNamespacedHelpers('menus');
export default {
  name: 'index',
  props: {},
  components: {
    search1,
    form1,
  },
  data: function () {
    return {
      btnList: [{ label: '添加', method: 'add' }],
      // 列表
      opera: [
        { label: '修改', method: 'edit' },
        { label: '删除', method: 'del', confirm: true, type: 'danger' },
      ],
      fields: [
        { label: '角色名称', model: 'name' },
        { label: '角色代码', model: 'code' },
        {
          label: '角色状态',
          model: 'status',
          format: (i) => (i === '0' ? '使用' : '停用'),
        },
      ],
      list: [],
      total: 0,
      // 多选值
      selected: [],
      // 弹框
      dialog: { title: '信息管理', show: false, type: '1' },
      // 增加数据
      form: {},
      // 查詢
      searchForm: {},
      // 菜单
      menuList: [],
      // 角色类型
      typeList: [],
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...menu({ menuQuery: 'query' }),
    ...mapActions(['query', 'fetch', 'create', 'update', 'delete']),
    // 查询
    async search({ skip = 0, limit = this.$limit, ...info } = {}) {
      const condition = _.cloneDeep(this.searchForm);
      let res = await this.query({ skip, limit, ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 新增
    toAdd() {
      this.dialog = { title: '信息管理', show: true, type: '1' };
    },
    // 修改
    async toEdit({ data }) {
      let res = await this.fetch(data._id);
      if (this.$checkRes(res)) {
        this.$set(this, `form`, res.data);
        this.dialog = { title: '信息管理', show: true, type: '1' };
      }
    },
    // 提交
    async onSubmit({ data }) {
      if (data.id) {
        let res = await this.update(data);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `维护信息成功` });
          this.toClose();
        }
      } else {
        let res = await this.create(data);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `创建信息成功` });
          this.toClose();
        }
      }
    },
    // 删除
    async toDel({ data }) {
      let res = await this.delete(data._id);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `刪除信息成功` });
        this.search();
      }
    },
    // 关闭
    toClose() {
      this.form = {};
      this.searchForm = {};
      this.dialog = { title: '信息管理', show: false, type: '1' };
      this.search();
    },
    // 多选
    handleSelect(data) {
      this.$set(this, `selected`, data);
    },
    // 查询其他信息
    async searchOther() {
      let res = await this.menuQuery({ status: '0' });
      if (this.$checkRes(res)) this.$set(this, `menuList`, res.data);
    },
  },
  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 {
    margin: 0 0 10px 0;
  }
  .two {
    margin: 0 0 10px 0;
  }
}
</style>
