<template>
  <!-- <h1>test</h1> -->
  <div class="content">
    <el-form :inline="true" class="search-list">
      <el-form-item prop="user_realname" label="姓名：">
        <el-input placeholder="请输入姓名" v-model="user_realname" clearable @clear="getListData"></el-input>
      </el-form-item>
      <el-form-item prop="ue_zgbh" label="职工编号：">
        <el-input placeholder="请选择职工编号" v-model="ue_zgbh" clearable @clear="getListData"></el-input>
      </el-form-item>
      <el-form-item prop="user_cardno" label="身份证号：">
        <el-input clearable @clear="getListData" v-model="user_cardno" placeholder="请输入身份证号"></el-input>
      </el-form-item>
      <el-form-item prop="ue_xxmc" label="学校：">
        <el-select v-model="activeSchool1" size="large" placeholder="学校" @change="getListData">
          <el-option label="全部" value="" />
          <el-option v-for="item in schoolData" :key="item.id" :label="item.sm_name" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item prop="ue_grade_name" label="年级：">
        <el-select v-model="activeGrade1" size="large" placeholder="年级" @change="getListData">
          <el-option label="全部" value="" />
          <el-option v-for="item in gradeData" :key="item.grade_id" :label="item.grade_name" :value="item.grade_id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="subject_name" label="学科：">
        <el-select v-model="activeSubject" size="large" placeholder="学科" @change="getListData">
          <el-option label="全部" value="" />
          <el-option v-for="item in subjectData" :key="item.subject_id" :label="item.subject_name" :value="item.subject_id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getListData">搜索</el-button>
        <el-button type="warning" @click="clearData">清空</el-button>
      </el-form-item>
    </el-form>
    <div class="search flex">
      <div>
        <!-- <el-button type="success" @click="exportAll">导出</el-button> -->
      </div>
    </div>
    <el-table :data="tableData" tooltip-effect="dark" v-loading="loading" ref="multipleTable" class="mt20" style="width: 100%">
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="姓名" prop="user_realname" width="100">
        <template #default="scope">
          <span style="color: #00a3ff">{{ scope.row.user_realname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="职工编号" prop="ue_zgbh" width="150">
      </el-table-column>
      <el-table-column label="身份证件号" prop="user_cardno" width="200"> </el-table-column>
      <el-table-column label="年级" prop="ue_grade_name" width="100">
      </el-table-column>
      <el-table-column label="学科" prop="subject_name" width="100">
      </el-table-column>
      <el-table-column label="学校" prop="ue_xxmc" width="auto">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="text" size="default" @click="editData(scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <footer class="flex-item-none" style="display: flex; justify-content: flex-end; margin-top: 30px">
      <el-pagination background layout="total,prev, pager, next" :page-size="limit" :total="total" v-model:currentPage="cur_page" @current-change="handleCurrentChange"></el-pagination>
    </footer>

    <el-dialog :title="title" v-model="dialogFormVisible" width="770px" append-to-body>
      <el-form :inline="true" ref="dialogFormRef" :rules="rules" :model="dialogForm" label-width="100px" class="form-list">

        <el-form-item label="状态:" prop="ue_jbxx_tea_jsll_shzt">
          <el-radio-group v-model="dialogForm.ue_jbxx_tea_jsll_shzt">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="text-center">
          <el-button @click="formCancel">取 消</el-button>
          <el-button type="primary" @click="formSubmit('dialogFormRef')">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ElMessage, ElMessageBox } from "element-plus";
import Request, { download } from "~/utils/request";
import {
  jsxx_list,
  jsxx_edit,
  grade_list,
  subject_list,
  school_list,

} from './api'
export default {
  data() {
    return {
      disabled: false,
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      user_realname: "",
      user_name: '',
      user_cardno: "",
      user_password: '',
      ue_zgbh: "",
      gradeData: [],
      subjectData: [],
      schoolData: [],
      activeGrade: {},
      activeSubject: {},
      activeSchool: {},
      dialogForm: {
        ue_jbxx_tea_jsll_shzt: "1"
      },
      rules: {
        ue_jbxx_tea_jsll_shzt: [{ required: true, message: "请选择状态", trigger: "blur" }],
      },
      loading: false,
      isEdit: false,
      activeGrade1: "",
      activeSubject1: "",
      activeSchool1: "",
      activeSubject: "",
      activeSchool: "",
      dialogFormVisible: false,
      user_id: "",
      tableData: [],
      title: '',
      SelectMap: {},
      SelectOptionMap: {}
    };
  },
  methods: {
    handleSelectV2Option(data, value, label, name) {
      const res = data.map((item) => {
        if (!this.SelectMap[name]) {
          this.SelectMap[name] = {}
        }
        this.SelectMap[name][item[value]] = item[label]
        return ({ value: item[value], label: item[label] })
      })
      this.SelectOptionMap[name] = res
      return res
    },
    handleSelectV2Change(val, name, key) {
      console.log('handleSelectV2Change :>> ', this.SelectMap[name]);
      this.dialogForm[key] = this.SelectMap[name][val]
    },
    // 年级
    initGradeData() {
      this.gradeData = [];
      grade_list().then((res) => {
        if (res.code == 1) {
          this.gradeData = res.data.page_data;
          this.handleSelectV2Option(this.gradeData, 'grade_id', 'grade_name', 'grade')
        }
      });
    }, // 科目
    initSubjectData() {
      this.subjectData = [];
      subject_list().then((res) => {
        if (res.code == 1) {
          this.subjectData = res.data.page_data;
          this.handleSelectV2Option(this.subjectData, 'subject_id', 'subject_name', 'subject')
        }
      });
    },
    //学校
    initSchoolData() {
      this.schoolData = [];
      school_list().then((res) => {
        if (res.code == 1) {
          this.schoolData = res.data.page_data;
          this.handleSelectV2Option(this.schoolData, 'id', 'sm_name', 'school')
        }
      });
    },

    initGrade(value) {
      this.dialogForm.grade_name = value.grade_name;
      this.dialogForm.grade_id = value.grade_id;
    },
    initSubject(value) {
      this.dialogForm.subject_name = value.subject_name;
      this.dialogForm.subject_id = value.subject_id;
    },
    initSchool(value) {
      this.dialogForm.sm_name = value.sm_name;
      this.dialogForm.id = value.id;
    },
    handleChange(value) {
      console.log(value);
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        user_realname: this.user_realname,
        // user_name: this.user_name,
        ue_zgbh: this.ue_zgbh,
        user_cardno: this.user_cardno,
        user_grade_num: this.activeGrade1,
        sm_id: this.activeSchool1,
        subject_id: this.activeSubject,
        ue_jbxx_tea_jsll_shzt: "2"
        // grade_id: this.grade_id
      };
      jsxx_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },

    editData(item) {
      this.title = "审核";
      this.dialogFormVisible = true;
      this.id = item.user_id;
      this.dialogForm.ue_jbxx_tea_jsll_shzt = item.ue_jbxx_tea_jsll_shzt;
    },

    formSubmit(formName) {
      this.$refs.dialogFormRef.validate((valid) => {
        if (valid) {
          let data = {
            ue_jbxx_tea_jsll_shzt: this.dialogForm.ue_jbxx_tea_jsll_shzt,
            user_role_id: '75',
          };
          data.user_id = this.id;
          jsxx_edit(data).then((res) => {
            // this.loading = false;
            if (res.code == 1) {
              ElMessage({
                message: "操作成功",
                type: "success",
              });

              this.dialogFormVisible = false;
              this.getListData();
            } else {
              ElMessage({
                message: res.msg,
                type: "error",
              });
            }
          });

        }
      });
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    //清空
    clearData() {
      this.user_realname = "";
      this.user_cardno = "";
      this.ue_zgbh = "";
      this.activeSchool = "";
      this.activeGrade1 = "";
      this.activeSubject = "";

    },
  },
  mounted() {
    this.getListData();
    this.initGradeData();
    this.initSubjectData();
    this.initSchoolData();

  },
}
</script>

<style lang="scss" scoped>
.content {
  width: 100%;
}

.search {
  border-bottom: 2px solid #e3e3e3;
  justify-content: space-between;
  padding: 10px 0;

  .print {
    width: 20px;
    height: 20px;
    margin: 7px 10px 0px 0px;
  }
}

.search-list {
  ::v-deep .el-form-item__label {
    font-size: 14px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #000000;
    padding: 0;
  }
}
::v-deep .el-select-v2__wrapper {
  width: 160px;
}
::v-deep .el-form-item__content {
  margin-right: 20px;
}

::v-deep .el-input__wrapper {
  width: 160px;
}

.form-list {
  ::v-deep .el-form-item__label {
    margin-left: 45px;
  }

  ::v-deep .el-form-item {
    margin-right: 0;
  }
}
</style>
