<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="idNumber">
          <el-input
            v-model="queryParams.idNumber"
            placeholder="请输入身份证号"
            clearable maxlength="20"
            style="width: 300px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <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="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <div style="width: 98%;margin: 10px;display: flex;justify-content: flex-end;">
            <span style="font-weight: bold;">过期数量：{{statisticsData.overdNum}}</span>
            <span style="font-weight: bold;margin-left: 20px;">临期数量：{{statisticsData.overNum}}</span>
      </div>
      <el-table v-loading="loading" :data="tableListData" height="800">
        <el-table-column label="序号" align="center" width="80" type="index">
            <template 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" width="180" />
        <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="issueLifespan" :show-overflow-tooltip="true"/>
        <el-table-column label="过期类型" align="center" prop="" :show-overflow-tooltip="true">
          <template slot-scope="scope">
              <span v-if="scope.row.type==1" style="color: orange;font-size: 25px;">临期</span>
              <span v-else="scope.row.type==2" style="color: red;font-size: 25px;">过期</span>
          </template>
        </el-table-column>
        <el-table-column label="天数" align="center" prop="" :show-overflow-tooltip="true">
          <template slot-scope="scope">
              <span v-if="scope.row.type==1" style="color: orange;font-size: 25px;">{{scope.row.processGapDate}}</span>
              <span v-else="scope.row.type==2" style="color: red;font-size: 25px;">{{scope.row.processGapDate}}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="过期类型" align="center" prop="" :show-overflow-tooltip="true">

        </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="title+''" :visible.sync="open" width="700px" append-to-body>
        <el-tabs v-model="activeName" @tab-click="handleTabClick">
            <!-- 基础设置 -->
            <el-tab-pane label="添加证件" name="baseSetting" style="height: 400px;">
                <el-form ref="baseForm" :model="baseForm" :rules="baseFormRules" label-width="80px">
                  <el-row>
                    <el-col :span="24" v-if="baseForm.deptId !== 0">
                        <el-form-item label="单位" prop="deptId">
                            <treeselect
                                v-model="baseForm.deptId"
                                :options="deptOptions"
                                :normalizer="normalizer"
                                placeholder="选择单位"
                            />
                        </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="12">
                        <el-form-item label="姓名" prop="name">
                        <el-input  v-model="baseForm.name" placeholder="请输入" maxlength="30" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="身份证号" prop="idNumber">
                            <el-input  v-model="baseForm.idNumber" placeholder="请输入" maxlength="30" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="出生日期" prop="birthday">
                            <el-date-picker
                                v-model="baseForm.birthday"
                                type="date"
                                placeholder="选择日期"
                                format="yyyy-MM-dd"
                                value-format="yyyy-MM-dd"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="出生地点" prop="birthAddr">
                            <el-input  v-model="baseForm.birthAddr" placeholder="请输入" maxlength="30" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="证件类型" prop="certificateType">
                            <el-select v-model="baseForm.certificateType" placeholder="证件类型" clearable>
                              <el-option
                                v-for="dict in dict.type.sys_certificate_type"
                                :key="dict.value"
                                :label="dict.label"
                                :value="dict.value"
                              />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="证件编号" prop="certificateNo">
                            <el-input  v-model="baseForm.certificateNo" placeholder="请输入" maxlength="30" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="签发地" prop="issueAddr">
                            <el-input  v-model="baseForm.issueAddr" placeholder="请输入" maxlength="30" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="签发日期" prop="issueDay">
                            <el-date-picker
                              v-model="baseForm.issueDay"
                              type="date"
                              placeholder="选择日期"
                              format="yyyy-MM-dd"
                              :picker-options="startPickerOptions"
                              value-format="yyyy-MM-dd"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="有效期至" prop="issueLifespan">
                            <el-date-picker
                              v-model="baseForm.issueLifespan"
                              type="date"
                              placeholder="选择日期"
                              format="yyyy-MM-dd"
                              :picker-options="endPickerOptions"
                              value-format="yyyy-MM-dd"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    </el-row>
                    <el-row>
                      <el-col :span="12">
                          <el-form-item label-width="120px" label="是否电子标签"  prop="isEle">
                            <el-radio-group v-model="baseForm.isEle" size="small">
                              <el-radio label="1">是</el-radio>
                              <el-radio label="0">否</el-radio>
                            </el-radio-group>
                          </el-form-item>
                      </el-col>
                    </el-row>
                </el-form>
            </el-tab-pane>
        </el-tabs>
        <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 { getCertificateOverdueListPageApi,addCertificateInfoApi, updateCertificateInfoApi, deleteCertificateInfoApi,getCertificateStatisticsApi } from "@/api/certificateManage/index";
    import base64 from 'base-64';
    import { getToken } from '@/utils/auth'
    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,
                checked: false,
                // 部门树选项
                deptOptions: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    idNumber:null,//身份证号
                    name:null,//姓名
                    certificateNo:null,//证件编号
                },
                activeName:"baseSetting",
                deviceData:{},
                // 表单参数
                baseForm: {
                    deptId:null,
                    name:"",
                    idNumber:"",
                    birthday:"",
                    birthAddr:"",
                    certificateType:"",
                    certificateTypeName:"",
                    certificateNo:"",
                    issueAddr:"",
                    issueDay:"",
                    issueLifespan:"0",
                    isEle:""
                },
                // 表单校验
                baseFormRules: {
                    deptId: [
                        { required: true, message: "部门不能为空", trigger: "blur" }
                    ],
                    name: [
                        { required: true, message: "姓名不能为空", trigger: "blur" }
                    ],
                    idNumber: [
                        { required: true, message: "身份证不能为空", trigger: "blur" }
                    ],
                    certificateType: [
                        { required: true, message: "证件类型不能为空", trigger: "blur" }
                    ],
                    certificateNo: [
                        { required: true, message: "证件编号不能为空", trigger: "blur" }
                    ],
                    issueDay: [
                        { required: true, message: "签发日期不能为空", trigger: "blur" }
                    ],
                    issueLifespan: [
                        { required: true, message: "有效期不能为空", trigger: "blur" }
                    ]
                },
                logTableData:[],//异常弹窗
                // 总条数
                dialogTotal: 0,
                //当前菜谱
                openMenu:false,
                 //导入参数
                upload: {
                    // 是否禁用上传
                    isUploading: false,
                    // 设置上传的请求头部
                    headers: { Authorization: 'Bearer ' + getToken() },
                    // 上传的地址
                    url: process.env.VUE_APP_BASE_API + '/file/upload',
                },
                appFormRules:{
                    version: [
                        { required: true, message: "版本号不能为空", trigger: "blur" }
                    ],
                    versionName: [
                        { required: true, message: "版本号名称不能为空", trigger: "blur" }
                    ],
                    apkName: [
                        { required: true, message: "APP名称不能为空", trigger: "blur" }
                    ],
                    apkPath: [
                        { required: true, message: "APP路径不能为空", trigger: "blur" }
                    ],
                    updateContent: [
                        { required: true, message: "更新内容不能为空", trigger: "blur" }
                    ]
                },
                statisticsData:{overdNu:0,overNu:0}
            };
        },
        mounted(){
            this.getList();
            this.getCertificateStatistics();
        },

        methods: {
            /** 查询列表 */
            getCertificateStatistics() {
                let param = {}
                getCertificateStatisticsApi(param).then(response => {
                    this.statisticsData = response.data
                });
            },
            /** 搜索按钮操作 */
            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})
                getCertificateOverdueListPageApi(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"
                };
                this.resetForm("baseForm");
            },
            /** 电子标签*/
            handleCheckedOne(value, type) {
              console.log(type,value)
            },
            handleCheckedTwo(value, type) {
              console.log(type,value)
            },
            /** 转换部门数据结构 */
            normalizer(node) {
                if (node.children && !node.children.length) {
                    delete node.children
                }
                return {
                    id: node.deptId,
                    label: node.deptName,
                    children: node.children,
                }
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                this.open = true;
                this.baseForm = Object.assign({}, row)
                // this.$set(this,"metadata",row.deviceMetadata)
                this.title = "修改";
            },
            handleTabClick(tab, event) {
                console.log(tab, event);
            },
            /** 提交按钮 */
            submitForm: function() {
                console.log("this.baseForm",this.baseForm);
                this.$refs["baseForm"].validate(valid => {
                if (valid) {
                    if (this.baseForm.certificateNo != 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();
                        });
                    }
                }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                // const dictIds = row.dictId || this.ids;
                this.$modal.confirm('是否确认删除数据项？').then(function() {
                    return  deleteCertificateInfoApi({deviceId:row.deviceId});
                }).then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {});
            },
             /** 导出按钮操作 */
            handleExport() {
            //   this.download('certificate/certificate_manage/certificateInfo/export', {
            //     ...this.queryParams
            //   }, `存证超期记录_${new Date().getTime()}.xlsx`);
            },
            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>
