<template>
  <div class="content">
    <el-form :inline="true" label-width="70px" 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="student_no" label="学生学号：">
        <el-input
          placeholder="请输入学生学号"
          v-model="student_no"
          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 label="班级：" prop="ue_class_name">
        <el-select
          v-model="activeClass1"
          placeholder="班级"
          size="default"
          @change="getListData"
        >
          <el-option label="全部" value="" />
          <el-option
            v-for="item in classData"
            :key="item.class_id"
            :label="item.class_name"
            :value="item.class_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="gender" label="性别：">
        <el-select v-model="gender" size="large" placeholder="性别">
          <el-option label="全部" value="" />
          <el-option
            v-for="item in genderData"
            :key="item.gender_id"
            :label="item.gender_name"
            :value="item.gender_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="primary" @click="addData">添加</el-button>
        <el-button type="success" @click="exportAll">导出</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="student_no" 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="80">
      </el-table-column>
      <el-table-column label="班级" prop="ue_class_name" width="100">
      </el-table-column>
      <el-table-column label="性别" prop="gender1" 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" @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="true"
    >
      <el-form
        ref="dialogFormRef"
        :rules="rules"
        :inline="true"
        :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="student_no">
          <el-input v-model="dialogForm.student_no"></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.n"
            />
          </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.n"
            />
          </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_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="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="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="user_class_num">
          <el-select-v2
            v-model="dialogForm.user_class_num"
            placeholder="请选择班级"
            size="large"
            :options="SelectOptionMap.class"
            @change="handleSelectV2Change($event, 'class', 'ue_class_name')"
          >
          </el-select-v2>
        </el-form-item>
        <el-form-item label="联系方式" prop="user_phone">
          <el-input v-model="dialogForm.user_phone"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址" prop="ue_province_id">
          <el-input v-model="dialogForm.ue_province_id"></el-input>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="dialogForm.address"></el-input>
        </el-form-item>
        <el-form-item label="爱好特长:" prop="about">
          <el-input
            type="textarea"
            style="width: 470px"
            v-model="dialogForm.about"
          ></el-input>
        </el-form-item>
        <el-form-item label="结业信息" prop="ue_jbxx_stu_jyxx">
          <el-input v-model="dialogForm.ue_jbxx_stu_jyxx"></el-input>
        </el-form-item>
        <el-form-item label="荣誉信息" prop="ue_jbxx_stu_ryxx">
          <el-input v-model="dialogForm.ue_jbxx_stu_ryxx"></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 Request, { download } from "~/utils/request";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  xsxx_detail,
  xsxx_list,
  grade_list,
  school_list,
  class_list,
  xsxx_add,
  xsxx_edit,
  xsxx_del,
  option_list,
  province_list,
} from "./api";
export default {
  data() {
    return {
      disabled: false,
      loading: false,
      value: [],
      options: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      user_realname: "",
      student_no: "",
      user_cardno: "",
      dialogForm: {
        user_name: "",
        user_password: "",
        user_drowssap: "",
        user_realname: "",
        student_no: "",
        ue_zjlx: "",
        user_cardno: "",
        ue_jg_name: "",
        user_birthday: "",
        ue_mz_name: "",
        ue_class_name: "",
        user_grade_num: "",
        ue_grade_name: "",
        user_class_num: "",
        ue_class_name: "",
        sm_id: "",
        ue_xxmc: "",
        ue_province_id: "",
        ue_city_id: "",
        address: "",
        about: "",
        gender: "1",
        user_phone: "",
        ue_jbxx_stu_jyxx: "",
        ue_jbxx_stu_ryxx: "",
      },

      gradeData: [],
      schoolData: [],
      classData: [],
      activeGrade: "",
      activeSchool: "",
      activeClass: "",
      activeGrade1: "",
      activeSchool1: "",
      activeClass1: "",
      gender: "",

      tableData: [],
      isEdit: false,
      dialogFormVisible: false,
      jgData: [],
      mzData: [],
      zjlxData: [],
      provinceData: [],
      user_id: "",
      multipleSelection: [],
      arr: [],
      genderData: [
        {
          gender_id: "1",
          gender_name: "男",
        },
        {
          gender_id: "2",
          gender_name: "女",
        },
      ],
      detailData: {},
      jgid: "",
      mzid: "",
      rules: {
        user_name: [{ required: true, message: "请输入账号", trigger: "blur" }],
        user_realname: [
          { required: true, message: "请输入学生姓名", trigger: "blur" },
        ],
        student_no: [
          { required: true, message: "请输入学生学号", trigger: "blur" },
        ],
        ue_zjlx: [
          { required: true, message: "请选择证件类型", trigger: "blur" },
        ],
        ue_jg_id: [{ required: true, message: "请选择籍贯", trigger: "blur" }],
        user_birthday: [
          { required: true, message: "请输入出生日期", trigger: "blur" },
        ],
        gender: [{ required: true, message: "请选择性别", trigger: "blur" }],
        sm_id: [{ required: true, message: "请选择学校", trigger: "blur" }],
        user_grade_num: [
          { required: true, message: "请选择年级", trigger: "blur" },
        ],
        user_class_num: [
          { required: true, message: "请选择班级", trigger: "blur" },
        ],
        user_phone: [
          { required: true, message: "请输入电话号码", trigger: "blur" },
        ],
        ue_province_id: [
          { required: true, message: "请选择家庭住址", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      SelectMap: {},
      SelectOptionMap: {},
      title: "",
    };
  },
  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"
          );
        }
      });
    },
    initGrade(value) {
      this.dialogForm.grade_name = value.grade_name;
      this.dialogForm.grade_id = value.grade_id;
    },
    //学校
    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");
        }
      });
    },
    initSchool(value) {
      this.dialogForm.sm_name = value.sm_name;
      this.dialogForm.id = value.id;
    },
    //班级
    initClassData() {
      this.classData = [];
      class_list().then((res) => {
        if (res.code == 1) {
          this.classData = res.data.page_data;
          this.handleSelectV2Option(
            this.classData,
            "class_id",
            "class_name",
            "class"
          );
          console.log(this.classData, 22222);
        }
      });
    },
    initClass(value) {
      this.dialogForm.class_name = value.class_name;
      this.dialogForm.class_id = value.class_id;
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },

    jgListData() {
      let data = {
        cs_code: "JG",
      };
      option_list(data).then((res) => {
        this.jgData = res.data.option;
        console.log(res.data.option);
      });
    },
    mzListData() {
      let data = {
        cs_code: "MZ",
      };
      option_list(data).then((res) => {
        this.mzData = res.data.option;
        console.log(res.data.option);
      });
    },
    zjlxListData() {
      let data = {
        cs_code: "SFZJLX",
      };
      option_list(data).then((res) => {
        this.zjlxData = res.data.option;
        console.log(res.data.option);
      });
    },

    provinceListData() {
      let data = {
        upid: 1,
      };
      province_list(data).then((res) => {
        this.provinceData = res.data.page_data;
        console.log(res.data.page_data, 1111);
      });
    },

    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        user_realname: this.user_realname,
        student_no: this.student_no,
        user_cardno: this.user_cardno,
        user_grade_num: this.activeGrade1,
        user_class_num: this.activeClass1,
        sm_id: this.activeSchool1,
        gender: this.gender,
      };
      xsxx_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);
        this.tableData.map((item) => {
          if (item.gender == "1") {
            item.gender1 = "男";
          } else {
            item.gender1 = "女";
          }
        });
      });
    },
    addData() {
      try {
        this.title = "添加";
        this.isEdit = false;
        this.jgListData();
        this.mzListData();
        this.zjlxListData();
        this.provinceListData();

        // this.dialogFormVisible = true;
        this.dialogForm = {
          user_name: "",
          user_password: "",
          user_drowssap: "",
          user_realname: "",
          student_no: "",
          ue_zjlx: "",
          user_cardno: "",
          user_phone: "",
          ue_jg_name: "",
          user_birthday: "",
          ue_mz_name: "",
          user_grade_num: "",
          ue_grade_name: "",
          user_class_num: "",
          ue_class_name: "",
          sm_id: "",
          ue_xxmc: "",
          ue_province_id: "",
          ue_city_id: "",
          address: "",
          about: "",
          gender: "",
          ue_jbxx_stu_jyxx: "",
          ue_jbxx_stu_ryxx: "",
        };
        console.log("dialogForm :>> ", this.dialogForm);
        this.dialogFormVisible = true;
        console.log("dialogFormVisible :>> ", this.dialogFormVisible);
      } catch (error) {
        console.error(error);
      }
    },
    editData(item) {
      this.title = "编辑信息";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.jgListData();
      this.mzListData();
      this.zjlxListData();
      this.provinceListData();
      this.id = item.user_id;
      this.dialogForm.user_name = item.user_name;
      this.dialogForm.user_password = item.user_drowssap;
      this.dialogForm.user_realname = item.user_realname;
      this.dialogForm.student_no = item.student_no;
      this.dialogForm.ue_zjlx = item.ue_zjlx;
      this.dialogForm.user_cardno = item.user_cardno;
      this.jgid = item.ue_jg_id;
      this.dialogForm.ue_jg_name = item.ue_jg_name;
      this.dialogForm.user_birthday = item.user_birthday;
      this.mzid = item.ue_mz_id;
      this.dialogForm.ue_mz_name = item.ue_mz_name;
      this.dialogForm.address = item.address;
      this.dialogForm.about = item.about;
      this.dialogForm.gender = item.gender;
      this.dialogForm.user_phone = item.user_phone;
      this.dialogForm.ue_province_id = item.ue_province_id;
      this.dialogForm.ue_city_id = item.ue_city_id;
      this.dialogForm.ue_grade_name = item.ue_grade_name;
      this.dialogForm.user_grade_num =
        item.user_grade_num == "0" ? "" : item.user_grade_num;
      this.dialogForm.ue_class_name = item.ue_class_name;
      this.dialogForm.user_class_num =
        item.user_class_num == "0" ? "" : item.user_class_num;
      this.dialogForm.ue_xxmc = item.ue_xxmc;
      this.dialogForm.sm_id = item.sm_id;
      (this.dialogForm.ue_jbxx_stu_jyxx = item.ue_jbxx_stu_jyxx),
        (this.dialogForm.ue_jbxx_stu_ryxx = item.ue_jbxx_stu_ryxx);
      console.log(this.dialogForm, 2222222);
    },

    formSubmit() {
      let mzid;
      this.mzData.forEach((item) => {
        if (item.n == this.dialogForm.ue_mz_name) {
          mzid = item.v;
        }
      });
      let jgid;
      this.jgData.forEach((item) => {
        if (item.n == this.dialogForm.ue_jg_name) {
          jgid = item.v;
        }
      });
      this.$refs.dialogFormRef.validate((valid) => {
        if (valid) {
          //  this.loading = true;
          let data = {
            user_name: this.dialogForm.user_name,
            user_password: this.dialogForm.user_password,
            student_no: this.dialogForm.student_no,
            user_realname: this.dialogForm.user_realname,
            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,
            user_phone: this.dialogForm.user_phone,
            ue_mz_name: this.dialogForm.ue_mz_name,
            ue_mz_id: mzid,
            ue_province_id: this.dialogForm.ue_province_id,
            ue_city_id: this.dialogForm.ue_city_id,
            address: this.dialogForm.address,
            about: this.dialogForm.about,
            gender: this.dialogForm.gender,
            user_grade_num: this.dialogForm.user_grade_num,
            ue_grade_name: this.dialogForm.ue_grade_name,
            user_class_num: this.dialogForm.user_class_num,
            ue_class_name: this.dialogForm.ue_class_name,
            sm_id: this.dialogForm.sm_id,
            ue_xxmc: this.dialogForm.ue_xxmc,
            ue_jbxx_stu_jyxx: this.dialogForm.ue_jbxx_stu_jyxx,
            ue_jbxx_stu_ryxx: this.dialogForm.ue_jbxx_stu_ryxx,
          };
          data.user_drowssap = this.dialogForm.user_password;
          if (!this.isEdit) {
            if (!data.user_password) {
              return ElMessage({
                type: "error",
                message: "请输入密码",
              });
            }
            data.user_role_id = "76";
            xsxx_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;
            xsxx_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",
                });
              }
            });
          }
        }
      });
    },

    delData() {
      ElMessageBox.confirm("是否确定删除当前数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            user_id: this.multipleSelection,
          };
          console.log(data, 111);
          xsxx_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);
      }
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    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: "76",
        user_ids: arr.join(","),
      };

      download("/user/main/index", obj);
    },

    //清空
    clearData() {
      this.activeGrade1 = "";
      this.activeSchool1 = "";
      this.activeClass1 = "";
      this.user_realname = "";
      this.student_no = "";
      this.user_cardno = "";
      this.gender = "";
    },
  },
  mounted() {
    this.getListData();
    this.initGradeData();
    this.initSchoolData();
    this.initClassData();
  },
};
</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-input__wrapper {
  width: 160px;
}
.form-list {
  ::v-deep .el-form-item__label {
    margin-left: 45px;
  }
  ::v-deep .el-form-item {
    margin-right: 0;
  }
}
::v-deep .el-select-v2__wrapper {
  width: 160px;
}
:deep(.pwd .el-form-item__label::before) {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>
