<template>
  <div id="goods">
    <template v-if="view === 'list'">
      <el-row>
        <el-col :span="24" style="padding: 10px">
          <el-button type="primary" size="mini" @click="toBack()">返回</el-button>
        </el-col>
      </el-row>
      <data-search :fields="searchFields" v-model="searchInfo" @query="search"> </data-search>
      <data-btn :fields="btnList" @add="toAdd"></data-btn>
      <data-table
        ref="dataTable"
        :fields="fields"
        :opera="opera"
        :data="list"
        :total="total"
        :limit="limit"
        @query="search"
        @edit="toEdit"
        @del="toDel"
        @reset="toReset"
      ></data-table>
    </template>
    <template v-else>
      <el-row>
        <el-col :span="24">
          <el-button type="primary" size="mini" @click="toBackList()">返回</el-button>
        </el-col>
        <el-col :span="24">
          <data-form :fields="infoFields" :rules="rules" v-model="form" labelWidth="150px" @save="toSave">
            <template #role>
              <el-option v-for="i in roleList" :key="i._id" :label="i.name" :value="i._id"></el-option>
            </template>
          </data-form>
        </el-col>
      </el-row>
    </template>
  </div>
</template>

<script>
const _ = require('lodash');
import methodsUtil from '@/util/opera';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: admins } = createNamespacedHelpers('admins');
const { mapActions: role } = createNamespacedHelpers('role');
const { mapActions: emailResetPwd } = createNamespacedHelpers('emailResetPwd');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      view: 'list',
      fields: [
        { label: '名称', model: 'name' },
        { label: '账号', model: 'account' },
        { label: '角色', model: 'role.name' },
      ],
      opera: [
        { label: '修改', method: 'edit' },
        { label: '重置密码', method: 'reset' },
        { label: '删除', method: 'del', confirm: true, type: 'danger' },
      ],
      btnList: [{ label: '添加', method: 'add' }],
      searchFields: [{ label: '名称', model: 'name' }],
      searchInfo: {},
      list: [],
      total: 0,
      limit: 10,
      // info部分
      infoFields: [
        { label: '名称', model: 'name' },
        { label: '账号', model: 'account' },
        { label: '角色', model: 'role', type: 'select' },
        { label: '邮箱', model: 'email' },
        { label: '密码', model: 'password', type: 'password', display: (i) => i.password == '' },
      ],
      rules: {},
      form: {},
      roleList: [],
    };
  },
  created() {
    this.searchOthers();
    this.search();
  },
  methods: {
    ...methodsUtil,
    ...role({ roleQuery: 'query' }),
    ...emailResetPwd({ pwdCreate: 'create' }),
    ...admins(['query', 'delete', 'fetch', 'update', 'create']),
    // 重置
    async search({ skip = 0, limit = this.limit, ...others } = {}) {
      others.shop = this.id;
      let query = { skip, limit, ...others };
      if (Object.keys(this.searchInfo).length > 0) query = { ...query, ...this.searchInfo };
      const res = await this.query(query);
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 添加自定义
    initAddData() {
      const obj = { shop: this.id };
      this.$set(this, 'form', obj);
    },
    // 修改
    async toEdit({ data }) {
      const res = await this.fetch(data._id);
      if (this.$checkRes(res)) {
        let data = res.data;
        this.$set(this, `form`, data);
        this.view = 'info';
      } else {
        this.$message.error('未找到指定数据');
      }
    },
    // 重置密码
    async toReset({ data }) {
      this.$confirm('忘记密码，是否确认重置密码?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await this.pwdCreate({ account: data.account });
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `重置密码成功` });
          this.search();
        }
      });
    },
    // 删除
    async toDel({ data }) {
      let res = await this.delete(data._id);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `刪除信息成功` });
        this.search();
      }
    },
    // 保存
    async toSave({ data }) {
      let res;
      if (data.id) res = await this.update(data);
      else res = await this.create(data);
      if (this.$checkRes(res, `维护信息成功`, res.errmsg)) {
        this.search();
        this.toBackList();
      }
    },
    // 查询其他信息
    async searchOthers() {
      let res = await this.roleQuery();
      if (this.$checkRes(res)) {
        this.$set(this, `roleList`, res.data);
      }
    },
    // 返回
    toBack() {
      window.history.go('-1');
    },
    // 返回列表
    toBackList() {
      this.view = 'list';
      this.form = {};
    },
  },
  computed: {
    id() {
      return this.$route.query.id;
    },
  },
};
</script>

<style lang="less" scoped></style>
