<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="请输入姓名"
        class="filter-item"
        clearable
      />

      <el-input
        v-model="listQuery.username"
        placeholder="请输入用户名"
        class="filter-item"
        clearable
      />

      <el-select
        v-model="listQuery.status"
        class="filter-item"
        placeholder="请选择状态"
        clearable
      >
        <el-option
          v-for="(item, index) in staffOptions"
          :key="index"
          :label="item.label"
          :value="item.value"
        />
      </el-select>

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
        >搜索</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
        >新建员工帐号</el-button
      >
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="用户名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>

      <el-table-column label="账号类型" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.type | typeFilter }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.status ? "在职" : "离职" }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width="180"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.type !== 0"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row.id)"
            >修改</el-button
          >
          <el-button
            v-if="scope.row.type !== 0"
            type="primary"
            size="mini"
            @click="handleResetPsw(scope.row)"
            >重置密码</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { list, reset } from "@/api/staff";
import Pagination from "@/components/Pagination/index";
import { staffOptions } from "@/utils";
export default {
  components: { Pagination },
  filters: {
    typeFilter(type) {
      const typeMap = {
        0: "管理员",
        1: "领导",
        2: "普通用户",
        3: "街道办",
        4: "开发商",
        5: "银行",
        6: "签署四方协议",
        7: "登记备案",
        8: "结算确认阶段",
        9: "还款处理",
      };
      return typeMap[type];
    },
  },
  data() {
    return {
      total: 0,
      list: [],
      listQuery: {
        current: 1,
        size: 10,
      },
      listLoading: false,
      staffOptions,
    };
  },
  created() {
    // this.getBusinessList()
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      list(this.listQuery).then((res) => {
        this.list = res.d.records;
        this.total = res.d.total;
        this.listLoading = false;
      });
    },
    handleSearch() {
      this.listQuery.current = 1;
      this.getList();
    },
    handleCreate() {
      this.$router.push({
        path: "/staff/create",
      });
    },
    handerDetail(row) {
      const { id, type } = row;
      this.$router.push({
        path: "/staff/detail",
        query: { id, type },
      });
    },
    // 修改(提交)
    confirm({ data, close, hideLoading }) {
      update(data)
        .then((res) => {
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs["table"].getList();
          close();
        })
        .catch(() => {
          hideLoading();
        });
    },
    // 修改(表单显示)
    handleUpdate(id) {
      // this.formProps.title = '修改'
      // this.formProps.defaultValue = row
      // this.$refs['form'].show()
      // const { id } = row;
      this.$router.push({
        path: "/staff/update",
        query: { id },
      });
    },
    // 重置密码
    handleResetPsw(row) {
      const { id } = row;
      this.$prompt("请输入新密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputValidator: (value) => {
          if (!value) return "请输入新密码";
          if (value.length < 6) return "请输入6位以上密码";
          return true;
        },
        inputErrorMessage: "密码不正确",
      }).then(({ value }) => {
        reset({
          id,
          password: value,
        }).then((res) => {
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
      });
    },
    // 删除
    handerDel(row) {
      const { name, id } = row;
      this.$confirm(`是否删除${name}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteInfo({ id }).then((res) => {
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs["table"].getList();
        });
      });
    },
    // 强制登录
    async handerLogin(row) {
      const { type } = row;
      const { d: token } = await forceLogin({
        forceLogin: true,
        // staffType: row.type,
        secondaryType: row.secondaryType,
        username: row.username,
        password: "123456",
      });
      if (type === "SYSTEM") {
        setToken(token);
        location.reload();
        return;
      }
      const { d: userInfo } = await forceLoginInfo({ id: row.id });
      console.log(token);
      console.log(userInfo);
      sessionStorage.setItem("token", token);
      sessionStorage.setItem("userInfo", JSON.stringify(userInfo));
      sessionStorage.setItem("platform", "BUSINESS");
      sessionStorage.setItem(
        "roles",
        JSON.stringify([row.type, row.secondaryType])
      );
      switch (row.secondaryType) {
        case "EMPLOYER":
          // location.href = location.origin + '/employerServices'
          window.open(location.origin + "/employerServices");
          break;
        case "ACADEMIC_EDUCATION":
        case "NON_ACADEMIC_EDUCATION":
          window.open(location.origin + "/educationalServices");
          // location.href = location.origin + '/educationalServices'
          break;
      }
    },
    // handerParent(row) {
    // businessInfoId
    // console.log(this.tableProps.filterFields);
    // this.tableProps.filterFields.push({
    //   prop: "businessInfoId",
    // });

    // this.tableProps.fields.secondaryType.props.disabled = true
    // },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px;
  margin-right: 10px;
}
.el-select {
  margin-right: 10px;
}
</style>
