<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"> 
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            clearable maxlength="20"
            style="width: 300px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="证件编号" prop="certificateNo">
          <el-input
            v-model="queryParams.certificateNo"
            placeholder="请输入证件编号"
            clearable maxlength="20"
            style="width: 300px"
            @keyup.enter.native="handleQuery"
          />
        </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-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >核验任务下达</el-button>
        </el-col> 
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="tableListData" @selection-change="handleSelectionChange" height="800">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="80" type="index">
            <template slot-scope="scope">
                <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
            </template>
        </el-table-column> 
        <el-table-column label="姓名" align="center" prop="name" :show-overflow-tooltip="true" width="120"/> 
        <el-table-column label="身份证号" align="center" prop="idNumber" :show-overflow-tooltip="true"/>
        <el-table-column label="证件类型" align="center" prop="certificateTypeName" :show-overflow-tooltip="true"/>
        <el-table-column label="证件编号" align="center" prop="certificateNo" :show-overflow-tooltip="true"/>
        <el-table-column label="申请出境时间" align="center" prop="" :show-overflow-tooltip="true"/>
        <el-table-column label="申请入境时间" align="center" prop="" :show-overflow-tooltip="true"/>
        <el-table-column label="核验类型" align="center" prop="" :show-overflow-tooltip="true"/>
        <el-table-column label="核验状态" align="center" prop="" :show-overflow-tooltip="true"/>
        <el-table-column label="核验结果" align="center" prop="" :show-overflow-tooltip="true"/> 
        <!-- <el-table-column label="当前状态" align="center" prop="stateName" :show-overflow-tooltip="true">
          <template slot-scope="scope">
              <span v-if="scope.row.state==0" style="color: #FF7322;font-size: 15px;">{{scope.row.stateName}}</span>
              <span v-if="scope.row.state==1" style="color: #FEE838;font-size: 15px;">{{scope.row.stateName}}</span>
              <span v-if="scope.row.state==2" style="color: #03DF6D;font-size: 15px;">{{scope.row.stateName}}</span>
              <span v-if="scope.row.state==3" style="color: #FEE838;font-size: 15px;">{{scope.row.stateName}}</span>
              <span v-if="scope.row.state==4" style="color: #FF7322;font-size: 15px;">{{scope.row.stateName}}</span>
          </template>
        </el-table-column> --> 
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog title="核验任务下达" :visible.sync="open" width="700px" append-to-body> 
        <el-form ref="baseForm" :model="baseForm" :rules="baseFormRules" label-width="80px">
          <el-row>
              <el-col :span="24">
                  <el-form-item label-width="120px" label="选择核验类型"  prop="verificationType">
                    <el-radio-group v-model="baseForm.verificationType" size="small">
                      <el-radio label="1">有无证件</el-radio>
                      <el-radio label="2">一人多证</el-radio>
                      <el-radio label="3">出入境时间</el-radio>
                    </el-radio-group>
                  </el-form-item>
              </el-col> 
          </el-row> 
          <el-row>
             <el-col :span="24">
                <el-form-item label="核验截止日期" prop="verificationDate">
                    <el-date-picker
                        v-model="baseForm.verificationDate"
                        type="date"
                        placeholder="选择日期"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
            </el-col> 
          </el-row> 
        </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 {
        listDept,
        getDept,
        delDept,
        addDept,
        updateDept,
        listDeptExcludeChild,
    } from '@/api/system/dept'
    import { getCertificateInfoListPageApi,addCertificateInfoApi, updateCertificateInfoApi,
     deleteCertificateInfoApi,getSysUserListApi } from "@/api/certificateManage/index";
    import base64 from 'base-64';
    import { getToken } from '@/utils/auth'
    import { downloadFileByUrl } from '@/utils/download'
    import Treeselect from '@riophae/vue-treeselect'
    import '@riophae/vue-treeselect/dist/vue-treeselect.css'
    export default {
        name: "",
        dicts: ["sys_certificate_type"],
        components: { Treeselect },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                //表格数据
                tableListData: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,   
                certificateNos:[],   
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10, 
                    name:null,//姓名
                    certificateNo:null,//证件编号
                },  
                // 表单参数
                baseForm: {
                    verificationType:1,
                    verificationDate:"", 
                }, 
                // 表单校验
                baseFormRules: {
                    verificationType: [
                        { required: true, message: "核验类型不能为空", trigger: "blur" }
                    ],
                    verificationDate: [
                        { required: true, message: "核验截至日期不能为空", trigger: "blur" }
                    ]
                },    
            };
        },
        mounted(){
            this.getList();

        },

        methods: {
            validateIDCard(rule, value, callback) {
              // 15位身份证正则表达式
              const reg15 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
              // 18位身份证正则表达式
              const reg18 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|X|x)$/;
              if (reg15.test(value) || reg18.test(value)) {
                callback(); // 验证通过，无错误信息提示
              } else {
                callback(new Error('请输入有效的身份证号')); // 验证失败，显示错误信息提示
              }
            },
            handleRadioChange(val){
              if(val==1){
                this.userNameFlage=true;
                this.baseFormRules= {
                    deptId: [
                        { required: true, message: "部门不能为空", trigger: "blur" }
                    ],
                    name: [
                        { required: true, message: "姓名不能为空", trigger: "blur" }
                    ],
                    idNumber: [
                        { required: true, message: "身份证不能为空", trigger: "blur" },
                        { validator: this.validateIDCard, trigger: 'blur' }
                    ],
                    certificateType: [
                        { required: true, message: "证件类型不能为空", trigger: "blur" }
                    ],
                    certificateNo: [
                        { required: true, message: "证件编号不能为空", trigger: "blur" }
                    ],
                    issueDay: [
                        { required: true, message: "签发日期不能为空", trigger: "blur" }
                    ],
                    phone: [
                        { required: true, message: "手机号码不能为空", trigger: "blur" },
                        { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
                    ],
                    issueLifespan: [
                        { required: true, message: "有效期不能为空", trigger: "blur" }
                    ],
                    userName: [
                        { required: true, message: "用户名不能为空", trigger: "blur" }
                    ]
                }
              }else{
                this.userNameFlage=false;
                this.baseFormRules= {
                    deptId: [
                        { required: true, message: "部门不能为空", trigger: "blur" }
                    ],
                    name: [
                        { required: true, message: "姓名不能为空", trigger: "blur" }
                    ],
                    idNumber: [
                        { required: true, message: "身份证不能为空", trigger: "blur" },
                        { validator: this.validateIDCard, trigger: 'blur' }
                    ],
                    certificateType: [
                        { required: true, message: "证件类型不能为空", trigger: "blur" }
                    ],
                    certificateNo: [
                        { required: true, message: "证件编号不能为空", trigger: "blur" }
                    ],
                    issueDay: [
                        { required: true, message: "签发日期不能为空", trigger: "blur" }
                    ],
                    phone: [
                        { required: true, message: "手机号码不能为空", trigger: "blur" },
                        { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
                    ],
                    issueLifespan: [
                        { required: true, message: "有效期不能为空", trigger: "blur" }
                    ]
                }
              }
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 查询列表 */
            getList() {
                this.loading = true;
                let param = {
                    "pageNum":this.queryParams.pageNum,
                    "pageSize":this.queryParams.pageSize,
                    "idNumber":this.queryParams.idNumber,
                    "name":this.queryParams.name,
                    "certificateNo":this.queryParams.certificateNo
                }
                console.log("param",param)
                // let str = base64.encode(JSON.stringify(param))
                // console.log({"content":str})
                getCertificateInfoListPageApi(param).then(response => {
                    this.tableListData = response.rows;
                    this.total = Number(response.total);
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            }, 
            // 表单重置
            reset() {
                this.baseForm = {
                    deptId:null,
                    name:"",
                    idNumber:"",
                    birthday:"",
                    birthAddr:"",
                    certificateType:"",
                    certificateTypeName:"",
                    certificateNo:"",
                    issueAddr:"",
                    issueDay:"",
                    issueLifespan:"",
                    isEle:"0",isCreate:"0"
                };
                this.resetForm("baseForm");
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true; 
                console.log("this.baseForm",this.baseForm);
            },   
            // 多选框选中数据
            handleSelectionChange(selection) {
              this.certificateNos = selection.map(item => item.certificateNo)
            },
            /** 提交按钮 */
            submitForm: function() {
                console.log("this.baseForm",this.baseForm);
                this.$refs["baseForm"].validate(valid => {
                if (valid) {
                    if (this.baseForm.createTime != undefined) {
                        updateCertificateInfoApi(this.baseForm).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addCertificateInfoApi(this.baseForm).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
                });
            },
          
            formatDate(date) {
                // 格式化为 YYYY-MM-DD
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
                const day = String(date.getDate()).padStart(2, '0');
                return `${year}-${month}-${day}`;
            }, 
        }
    };
  </script>
  <style scoped lang="scss">
    .form-title{
        display: flex;
        align-items: flex-end;
        // width: 100%;
        // height: 40px;
        background: #e7f0fa;
        border-left: 3px solid #46a6ff;
        margin: 10px 0;
        padding: 5px;
    }
    .form-item{
        width: 100%;
        font-size: 14px !important;
    }
  </style>
