<template>
  <div class="client-account-set ">
    <div class="part-box">
      <table class="table">
        <tr>
          <th>序号</th>
          <th>角色</th>
          <th>账号</th>
          <th>操作</th>
        </tr>
        <tr v-for="(user, index) in users" :key="index">
          <td style="width: 60px;">{{ index + 1 }}</td>
          <td>{{ user.roleName }}</td>
          <td style="width: 200px;">
            {{ user.loginName }}
          </td>
          <td>
            <Button size="small" type="primary" @click="toResetPwd(user)"
              >重置</Button
            >
          </td>
        </tr>
      </table>
    </div>
  </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: {}
    };
  },
  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: "纪检"
        };
      });

      if (!this.users.length) this.toAdd();
    },
    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>
