<template>
    <div class="app-container">
       <TabTitle :text="nowText" />
       <el-card class="left-list">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
          <el-form-item label="姓名" prop="personName">
             <el-input
                v-model="queryParams.personName"
                placeholder="请输入姓名"
                clearable
                style="width: 200px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item label="身份证号" prop="idNumber">
                <el-input
                  v-model="queryParams.idNumber"
                  placeholder="请输入身份证号"
                  clearable
                  style="width: 200px"
                  @keyup.enter="handleQuery"
               />
          </el-form-item>
          <el-form-item label="所属部门" prop="deptId">
             <el-select v-model="queryParams.deptId" placeholder="请选择所属部门" clearable style="width: 200px">
                <el-option
                   v-for="dict in sys_job_status"
                   :key="dict.value"
                   :label="dict.label"
                   :value="dict.value"
                />
             </el-select>
          </el-form-item>
          <el-form-item>
             <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
             <el-button icon="Refresh" @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="Plus"
                @click="handleAdd"
             >新增</el-button>
          </el-col>
          <el-col :span="1.5">
             <el-button
                type="warning"
                plain
                icon="Download"
                @click="handleExport"
             >导入</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
       </el-row>
 
       <el-table v-loading="loading" :data="faceList" >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="姓名" width="100" align="center" prop="personName" />
          <el-table-column label="手机号" align="center" prop="phoneNumber" :show-overflow-tooltip="true" />
          <el-table-column label="身份证号" align="center" prop="idNumber" />
          <el-table-column label="所属部门" align="center" prop="deptId" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true" >
            <template #default="scope">
               {{ parseTime(scope.row.createTime) }}
             </template>
          </el-table-column>
          <el-table-column label="操作人" align="center" prop="createByName" />
          <el-table-column label="人脸照片" align="center">
             <template #default="scope">
               <ImagePreview style="width: 50px;"  :src="scope.row.faceImg"/>
             </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
             <template #default="scope">
                <el-tooltip content="修改" placement="top">
                   <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" ></el-button>
                </el-tooltip>
                <el-tooltip content="删除" placement="top">
                   <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" ></el-button>
                </el-tooltip>
             </template>
          </el-table-column>
       </el-table>
 
       <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
       />
       </el-card>
 
 
       <!-- 添加或修改定时任务对话框 -->
       <el-dialog :title="title" v-model="open" width="500px" append-to-body>
          <el-form ref="faceRef" :model="form" :rules="rules" label-width="120px">
             <el-row>
                <el-col :span="22">
                   <el-form-item label="姓名" prop="personName">
                      <el-input v-model="form.personName" placeholder="请输入姓名" />
                   </el-form-item>
                </el-col>
                <el-col :span="22">
                   <el-form-item label="手机号" prop="phoneNumber">
                      <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
                   </el-form-item>
                </el-col>
                <el-col :span="22">
                   <el-form-item label="身份证号" prop="idNumber">
                      <el-input v-model="form.idNumber" placeholder="请输入身份证号" />
                   </el-form-item>
                </el-col>
                <el-col :span="22">
                   <el-form-item label="所属部门" prop="deptId">
                      <el-select v-model="form.deptId" placeholder="请选择">
                         <el-option
                            v-for="dict in sys_job_group"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                         ></el-option>
                      </el-select>
                   </el-form-item>
                </el-col>
                <el-col :span="22">
                   <el-form-item label="人脸照片" prop="faceImg">
                     <ImageUpload  :modelValue="form.faceImg" :limit='1' :fileSize="10" @update:modelValue="getImageUrl"/>
                   </el-form-item>
                </el-col>
             </el-row>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
             </div>
          </template>
       </el-dialog>
    </div>
 </template>
 
 <script setup name="face">
 import { listFace, detailAlg, deleteFace, addFace, updateFace} from "@/api/faceControl/index.js";
 const router = useRouter();
 const { proxy } = getCurrentInstance();
 const { sys_job_group, sys_job_status } = proxy.useDict("sys_job_group", "sys_job_status");
 
 const nowText = ref("人脸管理");
 const faceList = ref([]);
 const open = ref(false);
 const loading = ref(true);
 const showSearch = ref(true);
 const ids = ref([]);
 const total = ref(0);
 const title = ref("");
 
 const data = reactive({
   form: {},
   queryParams: {
     pageNum: 1,
     pageSize: 10,
   },
   rules: {
      personName: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
      phoneNumber: [{ required: true, message: "手机号不能为空", trigger: "blur" },
      {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }
      ],
      idNumber: [{ required: true, message: "身份证号不能为空", trigger: "blur" },
      { pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '请输入正确的身份证号', trigger: 'blur' }
      ],
      faceImg: [{ required: true, message: "人脸照片不能为空", trigger: "blur" }]
   }
 });
 
 const { queryParams, form, rules } = toRefs(data);
 
 
function getImageUrl(url){
    form.value.faceImg=url
}

 /** 查询列表 */
 function getList() {
   loading.value = true;
   listFace(queryParams.value).then(response => {
     faceList.value = response.rows;
     total.value = response.total;
     loading.value = false;
   });
 }
 
 /** 取消按钮 */
 function cancel() {
   open.value = false;
   reset();
 }
 
 /** 表单重置 */
 function reset() {
   form.value = {
     personName: undefined,
     phoneNumber: undefined,
     idNumber: undefined,
     deptId: undefined,
     faceImg: undefined,
   };
   proxy.resetForm("faceRef");
 }
 
 /** 搜索按钮操作 */
 function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   proxy.resetForm("queryRef");
   handleQuery();
 }

 /** 新增按钮操作 */
 function handleAdd() {
   reset();
   open.value = true;
   title.value = "添加任务";
 }
 
 /** 修改按钮操作 */
 function handleUpdate(row) {
   reset();
   const id = row.id || ids.value;
   detailAlg(id).then(response => {
     form.value = response.data;
     open.value = true;
     title.value = "修改任务";
   });
 }
 
 /** 提交按钮 */
 function submitForm() {
   proxy.$refs["faceRef"].validate(valid => {
     if (valid) {
       if (form.value.id != undefined) {
         updateFace(form.value).then(response => {
           proxy.$modal.msgSuccess("修改成功");
           open.value = false;
           getList();
         });
       } else {
         addFace(form.value).then(response => {
           proxy.$modal.msgSuccess("新增成功");
           open.value = false;
           getList();
         });
       }
     }
   });
 }
 
 /** 删除按钮操作 */
 function handleDelete(row) {
   const ids = row.id || ids.value;
   proxy.$modal.confirm('是否确认删除"' + row.personName + '"的数据项?').then(function () {
     return deleteFace(ids);
   }).then(() => {
     getList();
     proxy.$modal.msgSuccess("删除成功");
   }).catch(() => {});
 }
 
 /** 导出按钮操作 */
 function handleExport() {
   // proxy.download("monitor/job/export", {
   //   ...queryParams.value,
   // }, `job_${new Date().getTime()}.xlsx`);
 }
 
 getList();
 </script>
 <style scoped lang="scss">
 .left-list{
  min-height: 700px;
 }
</style>

