<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="140px">
      <el-row>
        <el-form-item label="办税人姓名" prop="bsryxm">
          <el-input
            v-model="queryParams.bsryxm"
            placeholder="请输入办税人姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="登录身份" prop="dlsf">
          <el-select v-model="queryParams.dlsf" placeholder="请选择登录身份" clearable size="small">
            <el-option v-for="dict in dict.type.login_dlsf" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="登录方式" prop="dlfs">
          <el-select v-model="queryParams.dlfs" placeholder="请选择登录方式" clearable size="small">
            <el-option v-for="dict in dict.type.login_dlfs" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="登录账号" prop="dlzh">
          <el-input
            v-model="queryParams.dlzh"
            placeholder="请输入登录账号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="纳税人名称" prop="nsrmc">
          <el-input
            v-model="queryParams.nsrmc"
            placeholder="请输入纳税人名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="纳税人识别号" prop="nsrsbh">
          <el-input
            v-model="queryParams.nsrsbh"
            placeholder="请输入纳税人识别号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="身份证件号码" prop="bsrysfzjhm">
          <el-input
            v-model="queryParams.bsrysfzjhm"
            placeholder="请输入办税人员身份证件号码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="bsrysjhm">
          <el-input
            v-model="queryParams.bsrysjhm"
            placeholder="请输入办税人员手机号码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="企业ID" prop="identity">
          <el-input
            v-model="queryParams.identity"
            placeholder="请输入企业ID"
            clearable
            size="small"
            @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-row>
    </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="['invoiceall:invoiceallyhdj: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="['invoiceall:invoiceallyhdj: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="['invoiceall:invoiceallyhdj: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="['invoiceall:invoiceallyhdj:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="invoiceallyhdjList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false" />
      <el-table-column label="办税人员姓名" align="center" prop="bsryxm" />
      <el-table-column label="登录密码" align="center" prop="dlsfmm" />
      <el-table-column label="登录身份" align="center" prop="dlsf" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.login_dlsf" :value="scope.row.dlsf"/>
        </template>
      </el-table-column>
      <el-table-column label="办税人员登录密码" align="center" prop="dlsfmm" />
      <el-table-column label="登录方式" align="center" prop="dlfs">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.login_dlfs" :value="scope.row.dlfs"/>
        </template>
      </el-table-column>
      <el-table-column label="登录密码" align="center" prop="dlmm" />
      <el-table-column label="登录账号" align="center" prop="dlzh" />
      <el-table-column label="纳税人名称" align="center" prop="nsrmc" />
      <el-table-column label="纳税人识别号" align="center" prop="nsrsbh" />
      <el-table-column label="身份证件号" align="center" prop="bsrysfzjhm" />
      <el-table-column label="手机号码" align="center" prop="bsrysjhm" />
      <el-table-column label="中间号码" align="center" prop="zjh" />
      <el-table-column label="企业ID" align="center" prop="identity" />
      <el-table-column label="地区编码" align="center" prop="dqbm" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="registered(scope.row)"
            v-hasPermi="['invoiceall:invoiceallyhdj:edit']"
            v-if="scope.row.dqbm != null
            && scope.row.dqbm != ''
            && scope.row.dlfs != null
            && scope.row.dlfs != ''
            && scope.row.dlsfmm != null
            && scope.row.dlsfmm != ''
            && scope.row.dlmm != null
            && scope.row.dlmm != ''
            && scope.row.dlzh != null
            && scope.row.dlzh != ''
            && scope.row.nsrmc != null
            && scope.row.nsrmc != ''
            && scope.row.nsrsbh != null
            && scope.row.nsrsbh != ''
            && scope.row.bsrysfzjhm != null
            && scope.row.bsrysfzjhm != ''
            && scope.row.identity != null
            && scope.row.identity != ''"
          >登记</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="qrcode(scope.row)"
            v-hasPermi="['invoiceall:invoiceallyhdj:remove']"
            v-show="scope.row.dqbm != null
            && scope.row.dqbm != ''
            && scope.row.nsrsbh != null
            && scope.row.nsrsbh != ''
            && scope.row.bsrysfzjhm != null
            && scope.row.bsrysfzjhm != ''
            && scope.row.identity != null
            && scope.row.identity != ''"
          >人脸识别</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" :close-on-click-modal="false" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="办税人员姓名" prop="bsryxm">
          <el-input v-model="form.bsryxm" placeholder="请输入办税人员姓名" />
        </el-form-item>
        <el-form-item label="登录身份" prop="dlsf">
          <el-select v-model="form.dlsf" placeholder="请选择登录身份" clearable size="small">
            <el-option v-for="dict in dict.type.login_dlsf" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
<!--          <el-input v-model="form.dlsf" placeholder="请输入登录身份" />-->
        </el-form-item>
        <el-form-item label="办税人员登录密码" prop="dlsfmm">
          <el-input v-model="form.dlsfmm" placeholder="请输入办税人员登录密码" />
        </el-form-item>
        <el-form-item label="登录方式" prop="dlfs">
          <el-select v-model="form.dlfs" placeholder="请选择登录方式" clearable size="small">
            <el-option v-for="dict in dict.type.login_dlfs" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
<!--          <el-input v-model="form.dlfs" placeholder="请输入登录方式" />-->
        </el-form-item>
        <el-form-item label="登录密码" prop="dlmm">
          <el-input v-model="form.dlmm" placeholder="请输入登录密码" />
        </el-form-item>
        <el-form-item label="登录账号" prop="dlzh">
          <el-input v-model="form.dlzh" placeholder="请输入登录账号" />
        </el-form-item>
        <el-form-item label="纳税人名称" prop="nsrmc">
          <el-input v-model="form.nsrmc" placeholder="请输入纳税人名称" />
        </el-form-item>
        <el-form-item label="纳税人识别号" prop="nsrsbh">
          <el-input v-model="form.nsrsbh" placeholder="请输入纳税人识别号" />
        </el-form-item>
        <el-form-item label="身份证件号" prop="bsrysfzjhm">
          <el-input v-model="form.bsrysfzjhm" placeholder="请输入办税人员身份证件号码" />
        </el-form-item>
        <el-form-item label="手机号码" prop="bsrysjhm">
          <el-input v-model="form.bsrysjhm" placeholder="请输入办税人员手机号码" />
        </el-form-item>
        <el-form-item label="中间号码" prop="zjh">
          <el-input v-model="form.zjh" placeholder="请输入中间号码" />
        </el-form-item>
        <el-form-item label="企业ID" prop="identity">
          <el-input v-model="form.identity" placeholder="请输入企业ID" />
        </el-form-item>
        <el-form-item label="地区编码" prop="dqbm">
          <el-input v-model="form.dqbm" placeholder="请输入地区编码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </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 {
  listInvoiceallyhdj,
  getInvoiceallyhdj,
  delInvoiceallyhdj,
  addInvoiceallyhdj,
  updateInvoiceallyhdj,
  registeredMethod,
  getCompanyservice,
  qrcodeMethod,
  encrypt
} from "@/api/digital/registered";

export default {
  name: "Invoiceallyhdj",
  dicts: ['login_dlsf','login_dlfs'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      should : false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 金四-用户登记表格数据
      invoiceallyhdjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bsryxm: null,
        dlsf: null,
        dlsfmm: null,
        dlfs: null,
        dlmm: null,
        dlzh: null,
        nsrmc: null,
        nsrsbh: null,
        bsrysfzjhm: null,
        bsrysjhm: null,
        zjh: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        identity: [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        bsryxm : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        dqbm : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        dlsf : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        dlsfmm : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        dlmm : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        dlzh : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        nsrmc : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        nsrsbh : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        bsrysfzjhm : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ],
        bsrysjhm : [
          { required: true, message: "属性不能为空", trigger: "blur" }
        ]
      },
      //登记参数
      registeredParams: {
        appkey: "935248bf224f6c8431cdc1969f029519",
        async: false,
        bsrysfzjhm: "",
        serviceId: "yhdj",
        dqbm: "",
        nsrsbh: "",
        jsonData: {
          bsryxm: "",
          dlsf: "",
          dlsfmm: "",
          dlfs: "",
          dlmm: "",
          dlzh: "",
          nsrmc: "",
          nsrsbh: "",
          bsrysfzjhm: "",
          bsrysjhm: "",
          zjh: "",
          deviceType: "",
          userExt: {
          }
        }

      },

      //获取人脸二维码参数
      qrcodeParams : {
        appkey: "935248bf224f6c8431cdc1969f029519",
        async: true,
        bsrysfzjhm:"",
        dqbm: "",
        nsrsbh: "",
        jsonData: {
          rlsblx: ""
        },
        serviceId: "hqrlsbewm"
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询金四-用户登记列表 */
    getList() {
      this.loading = true;
      listInvoiceallyhdj(this.queryParams).then(response => {
        this.invoiceallyhdjList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bsryxm: null,
        dlsf: null,
        dlsfmm: null,
        dlfs: null,
        dlmm: null,
        dlzh: null,
        nsrmc: null,
        nsrsbh: null,
        bsrysfzjhm: null,
        bsrysjhm: null,
        zjh: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        identity: null,
        dqbm: null,
        remark: null
      };
      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() {
      this.reset();
      this.open = true;
      this.title = "添加用户登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInvoiceallyhdj(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改金四-用户登记";
      });
    },
    //登记方法
    registered(row) {
      this.reset();

      getCompanyservice(row.identity).then(resp=>{
        //请求成功
        if(resp.code==200 && resp.data != null && resp.data != ""){
          this.convertRegistedParam(row,true)
          const requestBody = {
            key: resp.data.secret,
            order: JSON.stringify(this.registeredParams),
          }
          encrypt(requestBody).then(res => {
            if (res.code == 200 && res.data != null && res.data != "") {
              registeredMethod(res.data,row.identity).then(response => {
                if (response.code == 200){
                  this.$message.success('请求登记成功')
                }
              });
            }else if (res.data == null || res.data == ""){
              this.$message.error('报文加密失败')
            }
          })
        }else if(resp.data == null || resp.data == "") {
          this.$message.error('请检查企业ID配置')
        }

      })
    },
    /** 人脸识别 */
    qrcode(row){
      this.reset();
      getCompanyservice(row.identity).then(resp=>{
        //请求成功
        if(resp.code==200 && resp.data != null && resp.data != ""){
          this.convertRegistedParam(row,false)
          const requestBody = {
            key: resp.data.secret,
            order: JSON.stringify(this.qrcodeParams),
          }
          encrypt(requestBody).then(res => {
            if (res.code == 200 && res.data != null && res.data != "") {
              qrcodeMethod(res.data,row.identity).then(response => {
                if (response.code == 200 && response.data != null && response.data != ""){

                  if (response.data.ewm != null && response.data.ewm != ""){
                    this.$message.success('登记成功')
                  }else {
                    this.$message.error('登记失败')
                  }
                }else {
                  this.$message.error('登记失败')
                }
              });
            }else if (res.data == null || res.data == ""){
              this.$message.error('报文加密失败')
            }
          })
        }else if (resp.data == null || resp.data == ""){
          this.$message.error('请检查企业ID配置')
        }

      })
    },
    convertRegistedParam(row,flag){
      if (flag){
        this.registeredParams.bsrysfzjhm = row.bsrysfzjhm;
        this.registeredParams.dqbm = row.dqbm;
        this.registeredParams.nsrsbh = row.nsrsbh;
        this.registeredParams.jsonData.bsryxm = row.bsryxm;
        this.registeredParams.jsonData.dlsf = row.dlsf;
        this.registeredParams.jsonData.dlsfmm = row.dlsfmm;
        this.registeredParams.jsonData.dlfs = row.dlfs;
        this.registeredParams.jsonData.dlmm = row.dlmm;
        this.registeredParams.jsonData.dlzh = row.dlzh;
        this.registeredParams.jsonData.nsrmc = row.nsrmc;
        this.registeredParams.jsonData.nsrsbh = row.nsrsbh;
        this.registeredParams.jsonData.bsrysjhm = row.bsrysjhm;
        this.registeredParams.jsonData.zjh = row.zjh;
        this.registeredParams.jsonData.bsrysfzjhm = row.bsrysfzjhm;
      }else {
        this.qrcodeParams.bsrysfzjhm = row.bsrysfzjhm;
        this.qrcodeParams.dqbm = row.dqbm;
        this.qrcodeParams.nsrsbh = row.nsrsbh;
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInvoiceallyhdj(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvoiceallyhdj(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除金四-用户登记编号为"' + ids + '"的数据项？').then(function() {
        return delInvoiceallyhdj(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('invoiceall/invoiceallyhdj/export', {
        ...this.queryParams
      }, `invoiceallyhdj_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
