<template>
  <div class="grading-user-manage">
    <div class="part-box-top">
      <Button type="success" icon="md-add" @click="toAdd">新增</Button>
      <Button type="primary" icon="md-download" @click="toExport"
        >导出表格</Button
      >
    </div>
    <div class="part-box">
      <Table
        ref="TableList"
        :columns="columns"
        :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";
import { ROLE_TYPE, MARKER_RIGHT_TYPE } from "@/constants/enumerate";

export default {
  name: "grading-user-manage",
  components: { ModifyGradingUser },
  data() {
    return {
      subjectId: this.$route.params.subjectId,
      workId: this.$route.params.workId,
      subject: "",
      users: [],
      curUser: {},
      columns: [
        {
          type: "index",
          title: "序号",
          width: 60,
          align: "center"
        },
        {
          title: "科目",
          key: "subject"
        },
        {
          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));
          }
        }
      ]
    };
  },
  mounted() {
    this.subject = this.subjectId.split("-")[1];
    this.getList();
  },
  methods: {
    async getList() {
      const datas = {
        workId: this.workId,
        subject: this.subject
      };
      const data = await gradingUserList(datas);
      this.users = data.map(item => {
        item.roleName = ROLE_TYPE[item.role];
        item.rightName = MARKER_RIGHT_TYPE[item.markRight];
        return item;
      });
    },
    async toResetPwd(row) {
      await resetPwd({ userId: row.id, password: "123456" });
      this.$Message.success("重置密码成功！");
    },
    toAdd() {
      this.curUser = {
        workId: this.workId,
        subject: this.subject
      };
      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();
    },
    toExport() {
      window.open(`${this.GLOBAL.domain}/export/users?workId=${this.workId}`);
    }
  }
};
</script>
