<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleForm('add')"
        ></el-button>
        <!--
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				<el-button type="primary" plain :disabled="selection.length!=1" @click="permission">权限设置</el-button> -->
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="query.search"
            placeholder="姓名/手机号"
            clearable
            @keyup.enter="handleQuery"
          ></el-input>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="apiObj" row-key="id" stripe>
        <el-table-column
          label="#"
          fixed="left"
          type="index"
          width="50"
        ></el-table-column>
        <el-table-column
          label="姓名"
          fixed="left"
          prop="name"
        ></el-table-column>
        <el-table-column label="手机号" prop="phone"> </el-table-column>
        <el-table-column label="身份证号" prop="id_number"></el-table-column>
        <el-table-column label="证件照" prop="photo">
          <template #default="scope">
            <sc-upload v-model="scope.row.photo" :disabled="true" width="70" height="80"></sc-upload>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="left" width="170">
          <template #default="scope">
              <el-button
                link
                type="warning"
                size="small"
                @click="handleForm('edit', scope.row)">编辑</el-button
              >
              <el-popconfirm
                title="确定删除该访客吗？"
                @confirm="delVisitor(scope.row)"
              >
                <template #reference>
                  <el-button link type="danger" size="small">删除</el-button>
                </template>
              </el-popconfirm>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <el-dialog v-model="dialogcart" draggable title="创建访客">
    <el-form
      ref="dialogForm"
      :model="form"
      :rules="rules"
      :disabled="mode === 'show'"
      label-width="120px"
    >
      <el-row>
        <el-col :md="12" :sm="24">
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入姓名"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="手机号" prop="phone">
            <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="身份号" prop="id_number">
            <el-input
              v-model="form.id_number"
              placeholder="请输入身份证号"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :md="24" :sm="12" :xs="24">
          <el-form-item label="人员照片" prop="photo">
            <sc-upload v-model="form.photo" title="人员照片"></sc-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogcart = false">取 消</el-button>
      <el-button type="primary" @click="submitcert">确 定</el-button>
    </template>
  </el-dialog>
</template>
<script>
  import IdentityCodeValid from '@/utils/checkIdent'
  const defaultform = { id: "", name: "", phone: "", id_number: "" };
  export default {
    name: "certificate",
    components: {},
    data() {
      return {
        form: defaultform,
        //验证规则
        rules: {
          name: [{ required: true, message: "请输入姓名" }],
          phone: [{ required: true, message: "请输入手机号" }],
          id_number: [{ required: true, message: "请输入身份证号" }],
          photo: [{ required: true, message: "请上传" }],
        },
        dialogcart: false,
        apiObj: this.$API.vm.visitor.list,
        query: {},
        selection: [],
        search: {
          keyword: null,
        },
      };
    },
    mounted() {
      // this.getvisitorlist();
    },
    methods: {
      handleForm(type, row) {
        if (type === "add") {
          this.dialogcart = true;
          this.form = Object.assign({}, defaultform);
        } else {
          this.dialogcart = true;
          this.form = row;
        }
      },
      //访客列表
      // getvisitorlist() {
      //   this.$API.vm.visitor.list.req({ page: 0 }).then((res) => {
      //     this.apiObj = res;
      //   });
      // },
      //访客创建
      submitcert() {
        this.form.employee = this.$route.query.id;
        this.$refs.dialogForm.validate((valid) => {
          if(valid){
            debugger;
            this.form.id_number=this.form.id_number.toUpperCase();
            let ide = IdentityCodeValid( this.form.id_number )//得到 true or false
            if(ide){
              if (this.form.id == "") {
                this.$API.vm.visitor.create
                  .req(this.form)
                  .then((res) => {
                    this.$message.success("创建成功");
                    this.dialogcart = false;
                    this.$refs.table.refresh();
                    return res;
                  })
                  .catch((err) => {
                    return err;
                  });
              } else {
                this.$API.vm.visitor.update
                  .req(this.form.id, this.form)
                  .then((res) => {
                    this.$message.success("修改成功");
                    this.dialogcart = false;
                    this.$refs.table.refresh();
                    return res;
                  })
                  .catch((err) => {
                    return err;
                  });
              }
            }else{
              this.$message.warning("请输入正确的身份证号");
            }
          }
        });
      },
      //删除
      delVisitor(row) {
        this.$API.vm.visitor.delete
          .req(row.id)
          .then((res) => {
            this.$message.success("访客删除成功");
            this.$refs.table.refresh();
            return res;
          })
          .catch((err) => {
            return err;
          });
      },
      handleQuery() {
          this.$refs.table.queryData(this.query)
        },
    },
  };
</script>
