<template>
  <div class="grading-user-manage">
    <div class="part-box-top">
      <Button type="success" icon="md-add" @click="toAdd">新增</Button>
    </div>
    <div class="part-box">
      <Table
        ref="TableList"
        :columns="columes"
        :data="users"
        disabled-hover
        border
      ></Table>
    </div>

    <!-- modify-grading-user -->
    <modify-grading-user
      :instance="curUser"
      @modified="getList"
      ref="ModifyGradingUser"
    ></modify-grading-user>
  </div>
</template>

<script>
import { gradingUserList, deleteGradingUser, resetPwd } from "@/api";
import ModifyGradingUser from "./components/ModifyGradingUser";

export default {
  name: "grading-user-manage",
  components: { ModifyGradingUser },
  data() {
    return {
      subjectId: this.$route.params.subjectId,
      workId: this.$route.params.workId,
      users: [],
      curUser: {},
      columes: [
        {
          type: "index",
          title: "序号",
          width: 60,
          align: "center"
        },
        {
          title: "科目",
          key: "subjectName"
        },
        {
          title: "账号",
          key: "loginName"
        },
        {
          title: "姓名",
          key: "name"
        },
        {
          title: "角色",
          key: "roleName"
        },
        {
          title: "权限",
          key: "rightName"
        },
        {
          title: "权重",
          key: "weight"
        },
        {
          title: "操作",
          key: "action",
          width: 240,
          align: "center",
          render: (h, param) => {
            let actions = [
              {
                name: "重置",
                action: () => {
                  this.toResetPwd(param.row);
                }
              },
              {
                name: "编辑",
                action: () => {
                  this.toEdit(param.row);
                }
              },
              {
                name: "删除",
                type: "error",
                action: () => {
                  this.toDelete(param.row);
                }
              }
            ];
            return h("div", this.$tableAction(h, actions));
          }
        }
      ]
    };
  },
  methods: {
    async getList() {
      const datas = {
        workId: this.workId,
        subjectId: this.subjectId
      };
      const data = await gradingUserList(datas);
      this.users = data;
    },
    async toResetPwd(row) {
      await resetPwd(row.id);
      this.$Message.success("重置密码成功！");
    },
    toAdd() {
      this.curUser = {
        workId: this.workId,
        subjectId: this.subjectId
      };
      this.$refs.ModifyGradingUser.open();
    },
    toEdit(row) {
      this.curUser = row;
      this.$refs.ModifyGradingUser.open();
    },
    toDelete(row) {
      this.$Modal.confirm({
        title: "删除警告",
        content: "确定要删除当前账号吗？",
        onOk: () => {
          this.toDel(row.id);
        }
      });
    },
    async toDel(id) {
      await deleteGradingUser(id);
      this.$Message.success("删除成功！");
      this.getList();
    }
  }
};
</script>
