<template>
  <div class="client-account-set ">
    <Table
      ref="TableList"
      :columns="columns"
      :data="users"
      :row-class-name="getRowClassName"
      disabled-hover
      border
    ></Table>
  </div>
</template>

<script>
import { inspectionUserPageList, resetPwd } from "@/api";

const initModalForm = {
  id: "",
  roleName: "纪检",
  loginName: "",
  password: ""
};

export default {
  name: "client-account-set",
  data() {
    return {
      workId: this.$route.params.workId,
      users: [],
      curUser: {},
      columns: [
        {
          type: "index",
          title: "序号",
          width: 100,
          align: "center",
          indexMethod: row => {
            return row._index + 1;
          }
        },
        {
          title: "角色",
          key: "roleName"
        },
        {
          title: "账号",
          key: "loginName"
        },
        {
          title: "密码",
          key: "action",
          width: 100,
          align: "center",
          className: "table-action",
          render: (h, param) => {
            let actions = [
              {
                name: "重置",
                type: "text",
                action: () => {
                  this.toResetPwd(param.row);
                }
              }
            ];

            return h("div", this.$tableAction(h, actions));
          }
        }
      ]
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const datas = {
        workId: this.workId
      };
      const data = await inspectionUserPageList(datas);

      this.users = data.map(item => {
        return {
          id: item.id,
          loginName: item.loginName,
          password: item.password,
          roleName: "纪检"
        };
      });
    },
    toAdd() {
      this.users.push({ ...initModalForm });
    },
    async toResetPwd(user) {
      let result = true;
      await resetPwd({ userId: user.id, password: "123456" }).catch(() => {
        result = false;
      });

      if (!result) return;
      this.$Message.success("重置密码成功！");
    }
  }
};
</script>
