<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
      <el-form-item style="width: 300px">
        <div style="display: flex; justify-content: center; font-size: 20px; font-weight: 500" class="mt20, title">
          {{ xxfbName }}
        </div>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="匹配结果" prop="matchResult">
        <el-select v-model="queryParams.matchResult" @keyup.enter.native="handleQuery" style="width: 100%"
                   placeholder="请选择匹配结果">
          <el-option
            v-for="item in matchList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="ryList" :height="scrollHeight">
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="匹配结果" align="center" prop="matchResult">
        <template slot-scope="scope">
          <span>{{ scope.row.matchResult == '0' ? "不匹配" : "已匹配" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" min-width="180" prop="idCard"/>
      <el-table-column label="姓名" align="center" prop="studentName"/>
      <el-table-column label="班级" align="center" prop="classAlias"/>
      <template
        v-for="key in Object.keys(dynamicTitle)"
      >
        <el-table-column :key="key" min-width="120" align="center" :prop="key" :label="dynamicTitle[key]">
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:ry:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:ry:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改信息发布内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入姓名"/>
        </el-form-item>
        <template v-for="item in titleData">
          <el-form-item :label="item[1]" :prop="item[0]" :key="item[0]">
            <el-input v-model="form[item[0]]" :placeholder=placeholderValue(item[1])></el-input>
          </el-form-item>
        </template>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {dataList, getRy, delRy, addRy, updateRy} from "@/api/smartSchool/gradeWork/infoPublish/dataList.js";

export default {
  name: "DataList",
  computed: {
    placeholderValue() {
      return (value) => {
        return `请输入${value}`
      }

    }
  },
  data() {
    return {
      scrollHeight: document.body.scrollHeight - 100 + 'px',
      titleData: [],
      xxfbName: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      // 总条数
      total: 0,
      // 信息发布内容表格数据
      ryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        xxfbId: null,
        studentName: null,
        idCard: null,
        studentId: null,
        matchResult: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //动态列
      dynamicProp: [],
      // 动态标题
      dynamicTitle: {},
      matchList: [{
        label: "已匹配",
        value: "1"
      }, {
        label: "不匹配",
        value: "0"
      }]
    };
  },
  created() {
    this.xxfbId = this.$route.query && this.$route.query.id
    this.xxfbName = this.$route.query && this.$route.query.xxfbName
    this.getList();
  },
  activated() {
    this.xxfbId = this.$route.query && this.$route.query.id
    this.xxfbName = this.$route.query && this.$route.query.xxfbName
    this.getList();
  },
  methods: {
    /** 查询信息发布内容列表 */
    getList() {
      this.loading = true;
      dataList(this.queryParams, this.xxfbId).then(response => {
        this.ryList = response.data;
        //获取标题
        const titles = response.titles ?? {};
        Object.keys(titles).forEach(key => {
          if (titles[key]) {
            this.dynamicTitle[key] = titles[key];
          }
        })
        this.titleData = Object.entries(this.dynamicTitle)
        this.loading = false;
      }).catch(error => {
        this.loading = false;
      });
      ;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        xxfbId: null,
        studentName: null,
        idCard: null,
        studentId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信息发布内容";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRy(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信息发布内容";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRy(this.form, this.xxfbId).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRy(this.form, this.xxfbId).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除此信息？').then(function () {
        return delRy(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
