<template>
  <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="学校"
          @clear="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="年级">
          <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="学科"
          @clear="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>
        <import-button url="/user/main" @success="getListData"></import-button>
        <el-button type="primary" @click="addData">添加</el-button>
        <el-button
          type="danger"
          @click="delData"
          :disabled="multipleSelection?.length == 0"
          >删除</el-button
        >
      </div>
      <img class="print" src="/images/print.png" alt="" />
    </div>
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      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="780px"
      append-to-body
    >
      <el-form
        :inline="true"
        ref="dialogFormRef"
        :rules="rules"
        :model="dialogForm"
        label-width="100px"
        class="form-list"
      >
        <h4
          style="
            font-size: 16px;
            font-family: PingFangSC, PingFangSC-Regular;
            color: #265cd4;
            margin: 12px 42px;
          "
        >
          账号信息
        </h4>
        <el-form-item label="账号" prop="user_name">
          <el-input
            v-model="dialogForm.user_name"
            :disabled="isEdit"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="user_password"
          :class="title == '添加' ? 'pwd' : ''"
        >
          <el-input v-model="dialogForm.user_password"></el-input>
        </el-form-item>
        <h4
          style="
            font-size: 16px;
            font-family: PingFangSC, PingFangSC-Regular;
            color: #265cd4;
            margin: 12px 42px;
          "
        >
          基本信息
        </h4>
        <el-form-item label="姓名" prop="user_realname">
          <el-input v-model="dialogForm.user_realname"></el-input>
        </el-form-item>
        <el-form-item label="职工编号" prop="ue_zgbh">
          <el-input v-model="dialogForm.ue_zgbh"></el-input>
        </el-form-item>
        <el-form-item label="身份证件类型" prop="ue_zjlx">
          <el-select
            v-model="dialogForm.ue_zjlx"
            placeholder="请选择身份证件类型"
            size="large"
            value-key="v"
          >
            <el-option
              v-for="item in zjlxData"
              :key="item.v"
              :label="item.n"
              :value="item.v"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="user_cardno">
          <el-input v-model="dialogForm.user_cardno"></el-input>
        </el-form-item>
        <el-form-item label="籍贯" prop="ue_jg_name">
          <el-select
            placeholder="请选择籍贯"
            v-model="dialogForm.ue_jg_name"
            size="large"
            value-key="v"
          >
            <el-option
              v-for="item in jgData"
              :key="item.v"
              :label="item.n"
              :value="item.v"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="user_birthday">
          <el-date-picker
            v-model="dialogForm.user_birthday"
            value-format="YYYY-MM-DD"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="曾用名" prop="ue_cym">
          <el-input v-model="dialogForm.ue_cym"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="dialogForm.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="民族" prop="ue_mz_name">
          <el-select
            placeholder="请选择民族"
            v-model="dialogForm.ue_mz_name"
            size="large"
            value-key="v"
          >
            <el-option
              v-for="item in mzData"
              :key="item.v"
              :label="item.n"
              :value="item.n"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="政治面貌" prop="ue_zzmm">
          <el-select
            placeholder="请选择政治面貌"
            v-model="dialogForm.ue_zzmm"
            size="large"
            value-key="v"
          >
            <el-option
              v-for="item in optionData"
              :key="item.v"
              :label="item.n"
              :value="item.n"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学校" prop="sm_id">
          <el-select-v2
            v-model="dialogForm.sm_id"
            placeholder="请选择学校"
            size="large"
            :options="SelectOptionMap.school"
            @change="handleSelectV2Change($event, 'school', 'ue_xxmc')"
          >
          </el-select-v2>
        </el-form-item>
        <el-form-item label="年级" prop="user_grade_num">
          <el-select-v2
            v-model="dialogForm.user_grade_num"
            placeholder="请选择年级"
            size="large"
            :options="SelectOptionMap.grade"
            @change="handleSelectV2Change($event, 'grade', 'ue_grade_name')"
          >
          </el-select-v2>
        </el-form-item>
        <el-form-item label="学科" prop="subject_id">
          <el-select-v2
            v-model="dialogForm.subject_id"
            placeholder="请选择学科"
            size="large"
            :options="SelectOptionMap.subject"
            @change="handleSelectV2Change($event, 'subject', 'subject_name')"
          >
          </el-select-v2>
        </el-form-item>
        <h4
          style="
            font-size: 16px;
            font-family: PingFangSC, PingFangSC-Regular;
            color: #265cd4;
            margin: 12px 42px;
          "
        >
          联系方式
        </h4>
        <el-form-item label="联系电话" prop="user_phone">
          <el-input v-model="dialogForm.user_phone"></el-input>
        </el-form-item>
        <el-form-item label="电子信箱" prop="user_email">
          <el-input v-model="dialogForm.user_email"></el-input>
        </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_add,
  jsxx_edit,
  jsxx_del,
  grade_list,
  subject_list,
  school_list,
  option_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: {
        user_realname: "",
        user_name: "",
        user_password: "",
        user_drowssap: "",
        ue_zgbh: "",
        ue_zjlx: "",
        user_cardno: "",
        ue_jg_name: "",
        user_birthday: "",
        ue_cym: "",
        gender: "",
        ue_mz_name: "",
        ue_zzmm: "",
        ue_xxmc: "",
        ue_grade_name: "",
        subject_name: "",
        user_phone: "",
        user_email: "",
        user_grade_num: "",
        subject_id: "",
        sm_id: "",
      },
      loading: false,
      isEdit: false,
      activeGrade1: "",
      activeSubject1: "",
      activeSchool1: "",
      activeSubject: "",
      activeSchool: "",
      dialogFormVisible: false,
      user_id: "",
      multipleSelection: [],
      jgData: [],
      mzData: [],
      optionData: [],
      zjlxData: [],
      rules: {
        user_name: [{ required: true, message: "请输入账号", trigger: "blur" }],
        user_realname: [
          { required: true, message: "请输入姓名", trigger: "blur" },
        ],
        ue_zgbh: [
          { required: true, message: "请输入职工编号", trigger: "blur" },
        ],
        user_grade_num: [
          { required: true, message: "请选择年级", trigger: "blur" },
        ],
        subject_id: [
          { required: true, message: "请选择学科", trigger: "blur" },
        ],
      },
      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];
      console.log(this.dialogForm[key], " this.dialogForm[key] ");
    },
    // 年级
    initGradeData() {
      this.gradeData = [];
      grade_list().then((res) => {
        if (res.code == 1) {
          this.gradeData = res.data.page_data;
        }
      });
    }, // 科目
    initSubjectData() {
      this.subjectData = [];
      subject_list().then((res) => {
        if (res.code == 1) {
          this.subjectData = res.data.page_data;
        }
      });
    },
    //学校
    initSchoolData() {
      this.schoolData = [];
      school_list().then((res) => {
        if (res.code == 1) {
          this.schoolData = res.data.page_data;
        }
      });
    },

    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,
        ue_zgbh: this.ue_zgbh,
        user_cardno: this.user_cardno,
        grade_id: this.activeGrade1,
        sm_id: this.activeSchool1,
        subject_id: this.activeSubject,
      };
      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);
      });
    },
    addData() {
      this.handleSelectV2Option(this.schoolData, "id", "sm_name", "school");
      this.handleSelectV2Option(
        this.gradeData,
        "grade_id",
        "grade_name",
        "grade"
      );
      this.handleSelectV2Option(
        this.subjectData,
        "subject_id",
        "subject_name",
        "subject"
      );
      try {
        console.log("addData");
        this.title = "添加";
        this.zzmmListData();
        this.jgListData();
        this.mzListData();
        this.zjlxListData();
        this.dialogForm = {
          user_grade_num: "",
          ue_grade_name: "",
          subject_id: "",
          subject_name: "",
          sm_id: "",
          ue_xxmc: "",
          user_realname: "",
          user_name: "",
          user_password: "",
          user_drowssap: "",
          ue_zgbh: "",
          ue_zjlx: "",
          user_cardno: "",
          ue_jg_name: "",
          user_birthday: "",
          gender: "",
          ue_mz_name: "",
          ue_zzmm: "",
          user_phone: "",
          user_email: "",
          ue_cym: "",
        };
        console.log("dialogForm :>> ", this.dialogForm);
        this.dialogFormVisible = true;

        console.log("dialogFormVisible :>> ", this.dialogFormVisible);
      } catch (error) {
        console.error(error);
      }
    },
    delData() {
      ElMessageBox.confirm("是否确定删除当前数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            user_id: this.multipleSelection,
          };
          console.log(data, 111);
          jsxx_del(data).then((res) => {
            if (res.code == 1) {
              ElMessage({
                message: "删除成功",
                type: "success",
              });
              this.getListData();
            } else {
              ElMessage({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },

    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].user_id);
      }
    },

    editData(item) {
      this.title = "编辑信息";
      console.log(this.title, 11111);
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.zzmmListData();
      this.jgListData();
      this.mzListData();
      this.zjlxListData();
      this.id = item.user_id;
      this.dialogForm.user_realname = item.user_realname;
      this.dialogForm.user_name = item.user_name;
      this.dialogForm.user_password = item.user_drowssap;
      this.dialogForm.ue_zgbh = item.ue_zgbh;
      this.dialogForm.ue_zjlx = item.ue_zjlx;
      this.dialogForm.user_cardno = item.user_cardno;
      this.dialogForm.ue_jg_name = item.ue_jg_name;
      this.dialogForm.user_birthday = item.user_birthday;
      this.dialogForm.gender = item.gender;
      this.dialogForm.ue_cym = item.ue_cym;
      this.dialogForm.ue_mz_name = item.ue_mz_name;
      this.dialogForm.ue_zzmm = item.ue_zzmm;
      this.dialogForm.gradeName.label = item.ue_grade_name;
      this.dialogForm.gradeName.value = item.user_grade_num;
      this.dialogForm.subjectName.label = item.subject_name;
      this.dialogForm.subjectName.value = item.subject_id;
      this.dialogForm.schoolName.label = item.ue_xxmc;
      this.dialogForm.schoolName.value = item.sm_id;
      this.dialogForm.user_phone = item.user_phone;
      this.dialogForm.user_email = item.user_email;
      this.loading = true;
    },

    formSubmit(formName) {
      this.$refs.dialogFormRef.validate((valid) => {
        console.log("valid :>> ", valid);
        if (valid) {
          let data = {
            user_name: this.dialogForm.user_name,
            user_realname: this.dialogForm.user_realname,
            user_password: this.dialogForm.user_password,
            ue_zgbh: this.dialogForm.ue_zgbh,
            ue_zjlx: this.dialogForm.ue_zjlx,
            user_cardno: this.dialogForm.user_cardno,
            ue_jg_name: this.dialogForm.ue_jg_name,
            ue_jg_id: jgid,
            user_birthday: this.dialogForm.user_birthday,
            gender: this.dialogForm.gender,
            ue_cym: this.dialogForm.ue_cym,
            ue_mz_name: this.dialogForm.ue_mz_name,
            ue_mz_id: mzid,
            ue_zzmm: this.dialogForm.ue_zzmm,
            user_grade_num: this.dialogForm.user_grade_num,
            ue_grade_name: this.dialogForm.ue_grade_name,
            subject_id: this.dialogForm.subject_id,
            subject_name: this.dialogForm.subject_name,
            sm_id: this.dialogForm.sm_id,
            ue_xxmc: this.dialogForm.ue_xxmc,
            user_phone: this.dialogForm.user_phone,
            user_email: this.dialogForm.user_email,
          };
          data.user_drowssap = this.dialogForm.user_password;
          if (!this.isEdit) {
            if (!data.user_password) {
              return ElMessage({
                type: "error",
                message: "请输入密码",
              });
            }
            data.user_role_id = "75";
            jsxx_add(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",
                });
              }
            });
          } else {
            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;
    },
    zzmmListData() {
      this.loading = true;
      let data = {
        cs_code: "ZZMM",
      };
      option_list(data).then((res) => {
        this.loading = false;
        this.optionData = res.data.option;
        console.log(res.data.option);
      });
    },
    jgListData() {
      this.loading = true;
      let data = {
        cs_code: "JG",
      };
      option_list(data).then((res) => {
        this.loading = false;
        this.jgData = res.data.option;
        console.log(res.data.option);
      });
    },
    mzListData() {
      this.loading = true;
      let data = {
        cs_code: "MZ",
      };
      option_list(data).then((res) => {
        this.loading = false;
        this.mzData = res.data.option;
        console.log(res.data.option);
      });
    },
    zjlxListData() {
      this.loading = true;
      let data = {
        cs_code: "SFZJLX",
      };
      option_list(data).then((res) => {
        this.loading = false;
        this.zjlxData = res.data.option;
        console.log(res.data.option);
      });
    },
    exportAll() {
      let arr = [];
      this.multipleSelection.map((user_id) => {
        arr.push(user_id);
        console.log(arr, 123);
      });
      let obj = {
        limit: "99999",
        keyword: this.keyword,
        user_role_id: "75",
        user_ids: arr.join(","),
      };
      console.log();
      download("/user/main/index", obj);
    },
    //清空
    clearData() {
      this.user_realname = "";
      this.user_cardno = "";
      this.ue_zgbh = "";
      this.activeSchool = "";
      this.activeGrade1 = "";
      this.activeSubject = "";
    },
  },
  mounted() {
    console.log("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-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;
  }
}
:deep(.pwd .el-form-item__label::before) {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>
