<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="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入登录账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNumber">
        <el-input
          v-model="queryParams.idCardNumber"
          placeholder="请输入身份证号"
          clearable
          @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"
          v-hasPermi="['business:userVerification:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:userVerification:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:userVerification:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:userVerification:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userVerificationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="登录账号" align="center" prop="userAccount" />
      <el-table-column label="姓名" align="center" prop="userInfoName" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="国籍简码" align="center" prop="nationality" />
      <el-table-column label="认证状态" align="center" prop="status" />
      <el-table-column label="认证时间" align="center" prop="authenticationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.authenticationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失败原因" align="center" prop="failureReason" />
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="(scope.row.status == null || scope.row.status == '2')"
                     size="mini"
                     type="text"
                     icon="el-icon-document-add"
                     @click="handleAdd(scope.row)"
          >认证</el-button>
          <el-button v-if="(scope.row.status == 1)"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:userVerification:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:userVerification:remove']"
          >删除</el-button>
        </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="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="姓名" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCardNumber">
          <el-input v-model="form.idCardNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="国籍简码" prop="nationality">
          <el-input v-model="form.nationality" placeholder="请输入国籍简码" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证正面照" prop="idCardFrontImagePath">
          <el-input v-model="form.idCardFrontImagePath" placeholder="请输入身份证正面照" />
        </el-form-item>
        <el-form-item label="身份证反面照" prop="idCardBackImagePath">
          <el-input v-model="form.idCardBackImagePath" placeholder="请输入身份证反面照" />
        </el-form-item>
        <el-form-item label="手持身份证照" prop="userWithIdCardImagePath">
          <el-input v-model="form.userWithIdCardImagePath" placeholder="请输入手持身份证照" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 实名认证详情 -->
        <el-dialog :title="title" :visible.sync="detailOpen" width="700px" append-to-body>
            <el-form ref="form" :model="form" label-width="auto">
                    <el-form-item label="用户关联ID：">
                      {{form.userId}}
                    </el-form-item>
                    <el-form-item label="姓名：">
                      {{form.fullName}}
                    </el-form-item>
                    <el-form-item label="身份证号：">
                      {{form.idCardNumber}}
                    </el-form-item>
                    <el-form-item label="国籍简码：">
                      {{form.nationality}}
                    </el-form-item>
                    <el-form-item label="出生日期" prop="birthDate">
                      <el-date-picker clearable
                        v-model="form.birthDate"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择出生日期">
                      </el-date-picker>
                    </el-form-item>
                    <el-form-item label="身份证正面照：">
                      {{form.idCardFrontImagePath}}
                    </el-form-item>
                    <el-form-item label="身份证反面照：">
                      {{form.idCardBackImagePath}}
                    </el-form-item>
                    <el-form-item label="手持身份证照：">
                      {{form.userWithIdCardImagePath}}
                    </el-form-item>
                    <el-form-item label="生物识别信息：">
                      {{form.biometrics}}
                    </el-form-item>
                    <el-form-item label="认证时间" prop="authenticationDate">
                      <el-date-picker clearable
                        v-model="form.authenticationDate"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择认证时间">
                      </el-date-picker>
                    </el-form-item>
                    <el-form-item label="认证失败原因：">
                      {{form.failureReason}}
                    </el-form-item>
                  </el-form>
                  <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel">关 闭</el-button>
                  </div>
        </el-dialog>
  </div>
</template>

<script>
import { listUserVerification, getUserVerification, delUserVerification, addUserVerification, updateUserVerification } from "@/api/business/userVerification";

export default {
  name: "UserVerification",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实名认证表格数据
      userVerificationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        fullName: undefined,
        idCardNumber: undefined,
        gender: undefined,
        nationality: undefined,
        birthDate: undefined,
        idCardFrontImagePath: undefined,
        idCardBackImagePath: undefined,
        userWithIdCardImagePath: undefined,
        biometrics: undefined,
        status: undefined,
        authenticationDate: undefined,
        failureReason: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        idCardNumber: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        fullName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        birthDate: [
          { required: true, message: "出生日期不能为空", trigger: "blur" }
        ],
        nationality: [
          { required: true, message: "国籍简码不能为空", trigger: "blur" }
        ],
        idCardFrontImagePath: [
          { required: true, message: "身份证正面照不能为空", trigger: "blur" }
        ],
        idCardBackImagePath: [
          { required: true, message: "身份证反面照不能为空", trigger: "blur" }
        ],
        userWithIdCardImagePath: [
          { required: true, message: "手持身份证照不能为空", trigger: "blur" }
        ],
        biometrics: [
          { required: true, message: "生物识别信息不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询实名认证列表 */
    getList() {
      this.loading = true;
      listUserVerification(this.queryParams).then(response => {
        this.userVerificationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detailOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        fullName: undefined,
        idCardNumber: undefined,
        gender: undefined,
        nationality: undefined,
        birthDate: undefined,
        idCardFrontImagePath: undefined,
        idCardBackImagePath: undefined,
        userWithIdCardImagePath: undefined,
        biometrics: undefined,
        status: undefined,
        authenticationDate: undefined,
        failureReason: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form.userId = row.userId;
      this.open = true;
      this.title = "添加实名认证";
    },
    /** 详情按钮操作 */
    handleDetail(row){
        this.reset();
        this.form = row;
        this.detailOpen = true;
        this.title = "实名认证";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getUserVerification(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改实名认证";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateUserVerification(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addUserVerification(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实名认证编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delUserVerification(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/userVerification/export', {
        ...this.queryParams
      }, `userVerification_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
