<template>
  <div id="index">
    <admin-frame @search="search" :limit="limit" :total="total" topType="2" @add="toAdd" @back="back" :useNav="false">
      <template v-slot:info>
        <list-1 :list="list" @toEdit="toEdit" @toDel="toDel" @toPsd="toPsd"></list-1>
      </template>
    </admin-frame>
  </div>
</template>
<script>
import list1 from './parts/list_1.vue';
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
export default {
  name: 'index',
  props: {},
  components: {
    adminFrame,
    list1,
  },
  data: function () {
    return {
      list: [],
      limit: 5,
      total: 0,
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...adminLogin(['query', 'delete', 'updatePwd']),
    async search({ skip = 0, limit = this.limit, searchName, ...info } = {}) {
      if (searchName) info.name = this.searchName;
      let res = await this.query({ skip, limit, pid: this.user.id, role: '2', ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 添加
    toAdd() {
      this.$router.push({ path: `/account/mechanism/detail` });
    },
    // 编辑
    toEdit(data) {
      this.$router.push({ path: `/account/mechanism/detail`, query: { id: data.id } });
    },
    // 删除
    async toDel(data) {
      let res = await this.delete(data.id);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `删除成功` });
        this.search();
      }
    },
    // 重置密码
    async toPsd(data) {
      let res = await this.updatePwd({ id: data.id, passwd: '123456' });
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `重置密码完成` });
        this.search();
      }
    },
    back() {
      this.$router.push({ path: `/account/index` });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

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