<template>
  <div class="student-management">
    <div class="part-box-head">
      <div class="part-box-head-left"><h1>学生档案设置</h1></div>
    </div>
    <div class="part-filter">
      <div class="part-filter-form">
        <el-form :model="form" inline>
          <el-form-item label="姓名">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
          <el-form-item label="证件号">
            <el-input v-model.trim="form.identity"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <StateSelect v-model="form.enable"></StateSelect>
          </el-form-item>
          <el-form-item label="底照是否上传">
            <el-select
              v-model="form.photo"
              placeholder="底照是否上传"
              clearable
            >
              <el-option
                v-for="(val, key) in BOOLEAN_TYPE"
                :key="key"
                :value="key * 1"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleCurrentChange(0)"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>

    <el-table :data="tableData">
      <el-table-column type="selection" width="42" />
      <el-table-column label="ID">
        <span slot-scope="scope">{{ scope.row.id }}</span>
      </el-table-column>
      <el-table-column width="120" label="姓名">
        <span slot-scope="scope">{{ scope.row.name }}</span>
      </el-table-column>
      <el-table-column width="200" label="证件号">
        <span slot-scope="scope">{{ scope.row.identity }}</span>
      </el-table-column>
      <!-- <el-table-column width="100" label="机构">
        <span slot-scope="scope">{{ scope.row.orgName }}</span>
      </el-table-column> -->
      <el-table-column width="120" label="状态">
        <span slot-scope="scope">{{
          scope.row.enable | zeroOneEnableDisableFilter
        }}</span>
      </el-table-column>
      <el-table-column width="120" label="照片">
        <span slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="openBasePhotoDialog(scope.row.basePhotoPath)"
            v-if="scope.row.basePhotoPath"
          >
            查看底照
          </el-button>
          <span v-else>无底照</span>
        </span>
      </el-table-column>
      <el-table-column width="120" label="更新人">
        <span slot-scope="scope">{{ scope.row.updateName }}</span>
      </el-table-column>
      <el-table-column sortable width="170" label="更新时间">
        <span slot-scope="scope">{{
          scope.row.updateTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="260" fixed="right">
        <div slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="openExamRecord(scope.row)"
          >
            考试记录
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            @click="resetStudentPassword(scope.row)"
          >
            重置密码
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            @click="toggleEnableStudent(scope.row)"
          >
            {{ !scope.row.enable | booleanEnableDisableFilter }}
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100, 200, 300]"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>

    <el-dialog
      title="底照"
      :visible.sync="basePhotoDialogVisible"
      destroy-on-close
    >
      <img :src="selectedBasePhoto" style="width: 400px;" />
    </el-dialog>

    <StudentManagementDialog ref="theDialog" :student="selectedStudent" />
  </div>
</template>

<script>
import {
  searchStudents,
  toggleEnableStudent,
  resetStudentPassword,
} from "@/api/examwork-student";
import StudentManagementDialog from "./StudentManagementDialog";
import { BOOLEAN_TYPE } from "@/constant/constants";
export default {
  name: "StudentManagement",
  data() {
    return {
      form: {
        name: "",
        identity: "",
        enable: null,
        photo: null,
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      basePhotoDialogVisible: false,
      selectedBasePhoto: null,
      selectedStudent: {},
      BOOLEAN_TYPE,
    };
  },
  components: { StudentManagementDialog },
  async created() {
    this.searchForm();
  },
  methods: {
    async searchForm() {
      let datas = {
        enable: this.form.enable,
        identity: this.form.identity,
        name: this.form.name,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      };
      if (this.form.photo !== null && this.form.photo !== "") {
        datas.photo = !!this.form.photo;
      }
      const res = await searchStudents(datas);
      this.tableData = res.data.data.records;
      // this.tableData.forEach(
      //   (v) =>
      //     (v.basePhotoPath =
      //       "https://ecs-test-static.qmth.com.cn/org_logo/0/1597046412749.png")
      // );
      this.total = res.data.data.total;
      if (this.total > 0 && this.tableData.length === 0) {
        this.handleCurrentChange(this.currentPage - 1);
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    openBasePhotoDialog(url) {
      this.selectedBasePhoto = url;
      this.basePhotoDialogVisible = true;
    },
    openExamRecord(user) {
      this.selectedStudent = user;
      this.$refs.theDialog.openDialog();
    },
    async toggleEnableStudent(user) {
      await toggleEnableStudent({
        id: user.id,
        enable: user.enable === 0 ? 1 : 0,
      });
      this.searchForm();
    },
    async resetStudentPassword(user) {
      await resetStudentPassword({
        id: user.id,
        password: "123456",
      });
      this.searchForm();
    },
  },
};
</script>

<style></style>
