<template>
  <div>
    <el-card class="box-card">
      <div class="operation">
        <el-select v-model="selectVcalue" size="small" placeholder="请选择凭证类型" clearable>
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
        <!-- <el-select v-model="selectVoucherStatus" size="small" placeholder="请选择凭证状态" clearable>
          <el-option v-for="item in voucherStatusList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select> -->
        <el-button type="primary" size="small" @click="serach">搜索</el-button>
        <!-- <el-button type="primary" size="mini">批量下发</el-button> -->
        <el-button type="primary" size="small" @click="deleAll">批量删除</el-button>
      </div>
      <el-table :data="tableData" border class="table" height="700" ref="multipleTable"
        header-cell-class-name="table-header" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="cardName" label="人员名称" width="100"></el-table-column>
        <el-table-column prop="pzname" label="凭证名称" width="100">
          <template #default="scope">
            <span v-if="scope.row.type == 26">{{scope.row.name}}</span>
            <span v-else>{{scope.row.pzname}}</span>
          </template>
        </el-table-column>
        <el-table-column label="类型" width="60">
          <template #default="scope">
            <span v-if="scope.row.type == 0">人脸</span>
            <span v-if="scope.row.type == 1">指纹</span>
            <span v-if="scope.row.type == 2">身份证</span>
            <span v-if="scope.row.type == 3">NFC</span>
            <span v-if="scope.row.type == 4">密码</span>
            <span v-if="scope.row.type == 26">用户</span>
          </template>
        </el-table-column>
        <el-table-column label="下发状态" prop="statusText" width="100"></el-table-column>
        <el-table-column prop="number" width="400" label="有效期">
          <template #default="scope">
            <span>{{scope.row.startTimeText}} - {{scope.row.endTimeText}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="deviceNum" label="设备号" width="180"></el-table-column>
        <el-table-column prop="createAt" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="250" align="center" fixed="right">
          <template #default="scope">
            <!-- <el-button type="text" @click="modifyCrai(scope.$index, scope.row)">编辑</el-button> -->
            <!-- <el-button type="text" @click="Issue(scope.$index, scope.row)" v-if="!scope.row.status">下发凭证</el-button> -->
            <el-button type="text" class="red" @click="deletefun1(scope.$index, scope.row)">删除</el-button>
            <el-button type="text" class="red" @click="deleteCertificates(scope.$index, scope.row)">删除记录</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagepg">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
          :page-sizes="[10,30,100,200]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="total" ref="pagination">
        </el-pagination>
      </div>
      <el-dialog title="编辑" :visible.sync="certificatestatus" width="30%">
        <el-form ref="formvoucher1" :model="certificatesContent" label-width="80px" class="temporary">
          <!-- <el-form-item label="用户名称">
          <el-input v-model="certificatesContent.username"></el-input>
        </el-form-item> -->
          <el-form-item label="凭证名称">
            <el-input v-model="certificatesContent.pzname" :clearable="true"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="documentType1" placeholder="请选择" class="documentType" disabled>
              <el-option label="人脸" :value="0"></el-option>
              <el-option label="指纹" :value="1"></el-option>
              <el-option label="身份证" :value="2"></el-option>
              <el-option label="NFC" :value="3"></el-option>
              <el-option label="密码" :value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="上传照片" v-if="documentType1 == 0">
            <div v-if="documentType1 == 0">
              <el-upload action="#" list-type="picture-card" :auto-upload="false" :modal-append-to-body="false">
                <i slot="default" class="el-icon-plus"></i>
                <div slot="file" slot-scope="{ file }">
                  <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
                  <span class="el-upload-list__item-actions">
                    <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                      <i class="el-icon-zoom-in"></i>
                    </span>
                    <span v-if="!disabled1" class="el-upload-list__item-delete" @click="handleDownload(file)">
                      <i class="el-icon-download"></i>
                    </span>
                    <span v-if="!disabled1" class="el-upload-list__item-delete" @click="handleRemove(file)">
                      <i class="el-icon-delete"></i>
                    </span>
                  </span>
                </div>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible" :modal-append-to-body="false">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </div>
          </el-form-item>
          <el-form-item label="有效期">
            <el-date-picker v-model="modifyvalue2" v-if="modifyStatus" type="datetimerange" align="right"
              start-placeholder="开始日期" end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="certificatestatus = false">取 消</el-button>
            <el-button type="primary" @click="modifyCraiFun">确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
  import ICINLocker from "../utils/equipment.js"
  import {
    httpGet,
    httpPost
  } from "../api";
  import {
    timestampToTime,
    dec2hex,
    group,
    backDevice,
    deleteZgVoucher,
    modifyCraiAllFun
  } from "../utils/index";
  import utils from "../utils/util.js";
  import moment from 'moment'
  import {
    timeTotimeimp
  } from "../utils/index"
  export default {
    inject: ['reload'],
    name: "VoucherList",
    data() {
      return {
        selectVoucherStatus: '',
        voucherStatusList: [{
            label: '下发中',
            value: '0'
          },
          {
            label: '下发成功',
            value: '1'
          },
          {
            label: '发送失败',
            value: '702'
          },
          {
            label: '已作废',
            value: '703'
          }
        ],
        selectoption: [],
        selectoptionValue: '',
        tableData: [],
        currentPage: 1,
        total: 0,
        form: {
          password: "",
          password1: ""
        },
        row: {},
        index: 0,
        startdate: "",
        documentType1: "",
        certificatestatus: false,
        enddate: "",
        editVisiblelist: false,
        certificatesContent: {
          pzname: ""
        },
        dialogImageUrl: "",
        modifyStatus: true,
        multipleSelection: [],
        modifyvalue2: [],
        dialogVisible: false,
        modifyrow: {},
        cardId: "",
        pageSize: 10,
        selectVcalue: '',
        options: [{
          value: '0',
          label: '人脸'
        }, {
          value: '1',
          label: '指纹'
        }, {
          value: '2',
          label: '身份证'
        }, {
          value: '3',
          label: 'NFC'
        }, {
          value: '4',
          label: '密码'
        }],
        cardList: [],
        userInfo: {
          type: ''
        }
      };
    },
    props: ['deviceNum'],
    created() {
      // 获取凭证列表/v1/device/certificate/list
      // var tenantId = JSON.parse(localStorage.getItem('tenantId'))
      // if (tenantId == 400) {
      // this.$store.commit('defaultsettenantid')
      // }
      this.userInfo = JSON.parse(localStorage.getItem('info'))
      //this.$store.commit('getCardname')
      this.getcertificate();
      // this.cardList = this.$store.state.cartList
      // this.selectoption=JSON.parse(localStorage.getItem('selectoption'))
      // this.selectoptionValue=localStorage.getItem('tenantId')
    },
    methods: {
      deleteCertificates(index, row) {
        var data = {
          ids: [row.id],
        };
        httpPost(data, this.$api.deleteCertificates).then((
          res) => {
          if (res.success) {
            this.$message.success("删除成功！");
            this.getcertificate();
          }
        });
      },
      // 获取指纹id
      getfiId1(row, callback) {
        var data = {
          q: JSON.stringify({
            serial: row.serial
          }),
          pageNum: 1,
          pageSize: 10,
        };
        httpPost(data, this.$api.getDeviceResult).then((res) => {
          let datalist=utils.zlib_unzip(res.data)
          callback(datalist);
        });
      },
      // 批量删除凭证
      deleAll() {
        // console.log(this.multipleSelection3)
        // return
        var multipleSelection3 = this.multipleSelection
        this.$confirm("确定要删除吗？", "提示", {
          type: "warning",
        }).then(() => {
          multipleSelection3.map((li, index) => {
            if (li.status == 1) {
              this.deletefun1(index, li)
            } else {
              var data = {
                ids: [li.id],
              };
              httpPost(data, this.$api.deleteCertificates).then((
                res) => {
                if (res.success) {
                  this.getcertificate();
                  this.$message.success("删除成功！");
                }
              });
            }
          })
        });
      },
      // 删除凭证操作
      deletefun1(index, row) {
        // 二次确认删除
        var ids = this.roomtable;
        var that = this;
        var str = {};
        var q = {
          deviceNum: row.deviceNum,
          imei: row.imei,
          lockType: 1
        };
        var data = {
          q: JSON.stringify(q),
          pageNum: 1,
          pageSize: 10,
        };
        httpPost(data, this.$api.getDeviceItems).then((res) => {
          if (res.data.list.length > 0) {
            var cardName = row.name + '-' + JSON.parse(row.content).pzname
            if (res.data.list[0].manufactureId == 201|| res.data.list[0].manufactureId == 221) {
              if (row.type == 1) {
                this.getfiId1(row, function(data) {
                  if (data.length > 0) {
                    ICINLocker.delFingerPrints(row.deviceNum, row.imei,
                      JSON.parse(row.content).userPassword, 2, data[
                        0].face, 0, row.startTime, row.endTime, cardName, res.data.list[0].roomName, (
                        callback) => {
                        if (callback.success) {
                          // this.$message.success(callback.msg)
                          var data = {
                            ids: [row.id],
                          };
                          httpPost(data, that.$api
                            .deleteCertificates).then((
                            res) => {
                            if (res.success) {
                              that.$message.success(
                                "删除成功！");
                            }
                          });
                        } else {
                          that.$message.error(callback.msg)
                        }
                      })
                  } else {
                    var data = {
                      ids: [row.id],
                    };
                    httpPost(data, that.$api.deleteCertificates).then((
                      res) => {
                      if (res.success) {
                        that.$message.success("删除成功！");
                      }
                    });
                  }
                })
              } else if (row.type == 2) {
                this.getfiId1(row, function(data) {
                  if (data.length > 0) {
                    if (JSON.parse(row.content).password.length != 16) {
                      this.$confirm("卡格式错误，是否删除重新录入", "提示", {
                          type: "warning",
                        })
                        .then(() => {
                          var data = {
                            ids: [row.id],
                          };
                          httpPost(data, that.$api
                            .deleteCertificates).then((
                            res) => {
                            if (res.success) {
                              that.$message.success(
                                "删除成功！");
                            }
                          });
                        })
                    } else {
                      ICINLocker.delCardRegister(row.deviceNum, row.imei,
                        JSON.parse(row.content).userPassword, 2, 1,
                        JSON.parse(row.content).password, 36, row
                        .startTime, row.endTime, 6212, data[0]
                        .face, cardName, res.data.list[0].roomName, (callback) => {
                          if (callback.success) {
                            var data = {
                              ids: [row.id],
                            };
                            httpPost(data, that.$api
                              .deleteCertificates).then((
                              res) => {
                              if (res.success) {
                                that.$message
                                  .success(
                                    "删除成功！");
                              }
                            });
                          } else {
                            that.$message.error(callback.msg)
                          }
                        })
                    }
                  } else {
                    var data = {
                      ids: [row.id],
                    };
                    httpPost(data, that.$api.deleteCertificates).then((
                      res) => {
                      if (res.success) {
                        that.$message.success("删除成功！");
                      }
                    });
                  }
                })
              } else if (row.type == 3) {
                this.getfiId1(row, function(data) {
                  console.log(data)
                  if (data.length > 0) {
                    if (JSON.parse(row.content).password.length != 8) {
                      this.$confirm("卡格式错误，是否删除重新录入", "提示", {
                          type: "warning",
                        })
                        .then(() => {
                          var data = {
                            ids: [row.id],
                          };
                          httpPost(data, that.$api
                            .deleteCertificates).then((
                            res) => {
                            if (res.success) {
                              that.$message.success(
                                "删除成功！");
                            }
                          });

                        })
                    } else {
                      ICINLocker.delCardRegister(row.deviceNum, row.imei,
                        JSON.parse(row.content).userPassword, 2, 1,
                        JSON.parse(row.content).password, 50, row
                        .startTime, row.endTime, 6212, data[0]
                        .face, cardName, res.data.list[0].roomName, (callback) => {
                          if (callback.success) {
                            // this.$message.success(callback.msg)
                            var data = {
                              ids: [row.id],
                            };
                            httpPost(data, that.$api
                              .deleteCertificates).then((
                              res) => {
                              if (res.success) {
                                that.$message
                                  .success(
                                    "删除成功！");
                              }
                            });
                          } else {
                            that.$message.error(callback.msg)
                          }
                        })
                    }
                  } else {
                    var data = {
                      ids: [row.id],
                    };
                    httpPost(data, that.$api.deleteCertificates).then((
                      res) => {
                      if (res.success) {
                        that.$message.success("删除成功！");
                      }
                    });
                  }
                })
              } else if (row.type == 4) {
                this.getfiId1(row, function(data) {
                  console.log(data)
                  if (data.length > 0) {
                    ICINLocker.delForeverPassword(row.deviceNum, row.imei,
                      JSON.parse(row.content).userPassword, 2, JSON
                      .parse(row.content).password, 255, row
                      .startTime, row.endTime, 6209, data[0].face, cardName, res.data.list[0].roomName, (
                        callback) => {
                        if (callback.success) {
                          // this.$message.success(callback.msg)
                          var data = {
                            ids: [row.id],
                          };
                          httpPost(data, that.$api
                            .deleteCertificates).then((
                            res) => {
                            if (res.success) {
                              that.$message.success(
                                "删除成功！");
                            }
                          });
                        } else {
                          that.$message.error(callback.msg)
                        }
                      })
                  } else {
                    var data = {
                      ids: [row.id],
                    };
                    httpPost(data, that.$api.deleteCertificates).then((
                      res) => {
                      if (res.success) {
                        that.$message.success("删除成功！");
                      }
                    });
                  }
                })
              }else if(row.type==26){
                ICINLocker.addUserToLock(row.deviceNum, row.imei,JSON.parse(row.content).lockerUserId,0, 2, 6203, cardName, res.data.list[0].roomName, (callback) => {
                  if (callback.success) {
                    // this.$message.success(callback.msg)
                    var data = {
                      ids: [row.id],
                    };
                    httpPost(data, that.$api.deleteCertificates).then((res) => {
                      if (res.success) {
                        that.$message.success("删除成功！");
                      }
                    });
                  } else {
                    that.$message.error(callback.msg)
                  }
                })
              }
              this.getcertificate();
            } else if (res.data.list[0].manufactureId == 101) {
              if (row.type == 1) {
                this.getfiId(row, function(data) {
                  if (data.length > 0) {
                    var fpid = "";
                    if (data[0].face) {
                      fpid = data[0].face.toString(16);
                      if (fpid.length != 2) {
                        fpid = "0" + fpid;
                      }
                    } else {
                      if (data[0].face == 0) {
                        fpid = data[0].face.toString(16);
                        fpid = "0" + fpid;
                      }
                    }
                    str = {
                      imei: row.imei,
                      deviceNum: row.deviceNum,
                      userTelephone: JSON.parse(row.content).userTelephone,
                      operateMode: 18,
                      order: 6218,
                      password: fpid,
                      userPassword: JSON.parse(row.content).userPassword,
                      is: false,
                      passwordStartTime: JSON.parse(row.content).passwordStartTime,
                      passwordEndTime: JSON.parse(row.content).passwordEndTime,
                      cardName: cardName,
                      roomName: res.data.list[0].roomName
                    };
                    if (JSON.parse(row.content).pass) {
                      str['pass'] = true
                    } else {
                      str['pass'] = false
                    }
                    var data = {
                      ids: [row.id],
                    };
                    httpPost(data, that.$api.deleteCertificates).then((
                      res) => {
                      if (res.success) {
                        that.$message.success("删除成功！");
                      }
                    });
                    httpPost(str, that.$api.getDeviceOperateUserInfo).then(
                      (res1) => {
                        if (res1.success) {}
                      });
                  } else {
                    var data = {
                      ids: [row.id],
                    };
                    httpPost(data, that.$api.deleteCertificates).then((
                      res) => {
                      if (res.success) {
                        that.$message.success("删除成功！");
                      }
                    });
                  }
                });
                return false;
              } else if (row.type == 2) {
                str = {
                  imei: row.imei,
                  deviceNum: row.deviceNum,
                  userTelephone: JSON.parse(row.content).userTelephone,
                  operateMode: 15,
                  order: 6215,
                  password: JSON.parse(row.content).password,
                  userPassword: JSON.parse(row.content).userPassword,
                  is: false,
                  passwordStartTime: JSON.parse(row.content).passwordStartTime,
                  passwordEndTime: JSON.parse(row.content).passwordEndTime,
                  cardName: cardName,
                  roomName: res.data.list[0].roomName
                };
              } else if (row.type == 3) {
                str = {
                  imei: row.imei,
                  deviceNum: row.deviceNum,
                  userTelephone: JSON.parse(row.content).userTelephone,
                  operateMode: 12,
                  order: 6212,
                  password: JSON.parse(row.content).password,
                  userPassword: JSON.parse(row.content).userPassword,
                  is: false,
                  passwordStartTime: JSON.parse(row.content).passwordStartTime,
                  passwordEndTime: JSON.parse(row.content).passwordEndTime,
                  cardName: cardName,
                  roomName: res.data.list[0].roomName
                };
              } else if (row.type == 4) {
                str = {
                  imei: row.imei,
                  deviceNum: row.deviceNum,
                  userTelephone: JSON.parse(row.content).userTelephone,
                  operateMode: 9,
                  order: 6209,
                  userPassword: JSON.parse(row.content).userPassword,
                  password: JSON.parse(row.content).password,
                  is: false,
                  passwordStartTime: JSON.parse(row.content).passwordStartTime,
                  passwordEndTime: JSON.parse(row.content).passwordEndTime,
                  cardName: cardName,
                  roomName: res.data.list[0].roomName
                };
              }else if(row.type==26){
                ICINLocker.LDDeleteUser(row.deviceNum, row.imei,JSON.parse(row.content).userTelephone,JSON.parse(row.content).userPassword,cardName, res.data.list[0].roomName, (callback) => {
                  if (callback.success) {
                    // this.$message.success(callback.msg)
                    var data = {
                      ids: [row.id],
                    };
                    httpPost(data, that.$api.deleteCertificates).then((res) => {
                      if (res.success) {
                        that.$message.success("删除成功！");
                      }
                    });
                  } else {
                    that.$message.error(callback.msg)
                  }
                })
              }
              if (JSON.parse(row.content).pass) {
                str['pass'] = true
              } else {
                str['pass'] = false
              }
              var data = {
                ids: [row.id],
              };
              httpPost(data, this.$api.deleteCertificates).then((res) => {
                if (res.success) {
                  this.$message.success("删除成功！");
                }
              });
              httpPost(str, this.$api.getDeviceOperateUserInfo).then((res1) => {
                if (res1.success) {}
              });
              this.getcertificate();
            }
          } else {
            var data = {
              ids: [row.id],
            };
            httpPost(data, that.$api.deleteCertificates).then((
              res) => {
              if (res.success) {
                that.getcertificate();
                that.$message.success("删除成功！");
              }
            });
          }
        })
      },
      selectTenantId(e) {
        localStorage.setItem('tenantId', e)
        this.reload()
      },
      // 搜索
      serach() {
        var data = {
            pageNum: 1,
            pageSize: this.pageSize,
            q: JSON.stringify({
              deviceNum: this.deviceNum,
              // status: this.selectVoucherStatus,
              status:1,
              // types:"0,1,2,3,4",
              types: this.selectVcalue || "0,1,2,3,4"
            }),
            orderBy: "create_at desc",
          };
        httpGet(data, this.$api.getCertificates).then((res) => {
          let dataOne=utils.zlib_unzip(res.data)
         dataOne.map((li) => {
            li.createAt = this.time(+new Date(li.createAt));
            li.userTelephone = JSON.parse(li.content).userTelephone;
            li['pzname'] = JSON.parse(li.content).pzname
            // var cardli = this.cardList.filter((item) => item.id == li.cardId)
            // if (cardli.length == 0) {
            //   li['cardName'] = ''
            // } else {
            //   li['cardName'] = cardli[0].name
            // }
            li['statusText'] = utils.statusCode(li.status)
            li['startTimeText'] = utils.formatTime(li.startTime)
            li['endTimeText'] = utils.formatTime(li.endTime)
            if (li.name) {
              li['cardName'] = li.name
            } else {
              li['cardName'] = JSON.parse(li.content).cardName
            }
          });
          this.tableData = dataOne;
          this.total = res.data.total;
        });
      },
      // input视图刷新
      changesr(e) {
        this.$forceUpdate();
      },
      // 修改凭证
      modifyCraiFun() {
        var that = this;
        var row = this.modifyrow;
        var ids = this.roomtable;
        var content = JSON.parse(row.content);
        this.documentType1 = row.type;
        var data = {};
        var userTelephone = content.userTelephone;
        var userPassword = content.userPassword;
        var startTime = new Date(this.modifyvalue2[0]).getTime() / 1000
        var endTime = new Date(this.modifyvalue2[1]).getTime() / 1000
        modifyCraiAllFun(row, this.modifyvalue2, this.certificatesContent, function(data) {
          if (data.success) {
            var updatedata = {
              id: row.id,
              serial: data.data.serialNumber,
              startTime: startTime,
              endTime: endTime,
              status: 0,
            };
            httpPost(updatedata, that.$api.updateCertificates).then((res2) => {
              if (res2.success) {
                that.getcertificate();
                that.certificatestatus = false;
              }
            });
            that.$message.success("修改成功");
          } else {
            that.$message.error("修改失败");
          }
        })
        return
        if (row.type == 1) {
          this.getfiId(row, function(data) {
            if (data.length > 0) {
              var fpid = "";
              fpid =
                data[0].face.toString(16).length != 2 ?
                "0" + data[0].face.toString(16) :
                data[0].face.toString(16);
              var str = {
                imei: row.imei,
                deviceNum: row.deviceNum,
                pzname: that.certificatesContent.pzname,
                cardId: row.cardId,
                password: fpid,
                operateMode: 17,
                order: 6217,
                userPassword: userPassword,
                userTelephone: userTelephone,
                passwordStartTime: timestampToTime(that.modifyvalue2[0]),
                passwordEndTime: timestampToTime(that.modifyvalue2[1]),
                is: false,
              };
              httpPost(str, that.$api.getDeviceOperateUserInfo).then((res1) => {
                if (res1.success) {
                  content.passwordStartTime = str.passwordStartTime;
                  content.passwordEndTime = str.passwordEndTime;
                  content.pzname = str.pzname;
                  var updatedata = {
                    id: row.id,
                    content: JSON.stringify(content),
                    serial: res1.data.serialNumber,
                    status: 0,
                  };
                  httpPost(updatedata, that.$api.updateCertificates).then(
                    (res2) => {
                      that.certificatestatus = false;
                      that.getcertificate();
                      that.$message.success("修改成功");
                    }
                  );
                } else {
                  that.certificatestatus = false;
                  that.getcertificate();
                  that.$message.success("修改失败");
                }
              });
            }
          });
          return false;
        } else if (row.type == 2) {
          var arr2 = dec2hex(this.certificatesContent.idcard);
          data = {
            imei: row.imei,
            deviceNum: row.deviceNum,
            userTelephone: userTelephone,
            pzname: this.certificatesContent.pzname,
            cardId: row.cardId,
            userPassword: userPassword,
            password: arr2.toUpperCase(),
            operateMode: 14,
            order: 6214,
            passwordStartTime: timestampToTime(this.modifyvalue2[0]),
            passwordEndTime: timestampToTime(this.modifyvalue2[1]),
            is: false,
          };
          if (arr2 == "") {
            this.$message.error("请填写完整信息");
            return false;
          }
        } else if (row.type == 3) {
          var array3 = dec2hex(this.certificatesContent.nfc);
          var arr4 = group(array3, 2).reverse();
          var nfc = "";
          arr4.map((li) => {
            nfc += li;
          });
          data = {
            imei: row.imei,
            deviceNum: row.deviceNum,
            userTelephone: userTelephone,
            pzname: this.certificatesContent.pzname,
            cardId: row.cardId,
            userPassword: userPassword,
            password: arr4.toUpperCase(),
            operateMode: 11,
            order: 6211,
            passwordStartTime: timestampToTime(this.modifyvalue2[0]),
            passwordEndTime: timestampToTime(this.modifyvalue2[1]),
            is: false,
          };
          if (arr4 == "") {
            this.$message.error("请填写完整信息");
            return false;
          }
        } else if (row.type == 4) {
          data = {
            imei: row.imei,
            deviceNum: row.deviceNum,
            userTelephone: userTelephone,
            pzname: this.certificatesContent.pzname,
            cardId: row.cardId,
            userPassword: userPassword,
            password: this.certificatesContent.password,
            operateMode: 8,
            order: 6208,
            passwordStartTime: timestampToTime(this.modifyvalue2[0]),
            passwordEndTime: timestampToTime(this.modifyvalue2[1]),
            is: false,
          };
          if (this.certificatesContent.password == "") {
            this.$message.error("请填写完整信息");
            return false;
          }
        }
        httpPost(data, this.$api.getDeviceOperateUserInfo).then((res) => {
          if (res.success) {
            var updatedata = {
              id: row.id,
              content: JSON.stringify(data),
              serial: res.data.serialNumber,
              status: 0,
            };
            httpPost(updatedata, this.$api.updateCertificates).then((res2) => {
              if (res2.success) {
                this.getcertificate();
                this.certificatestatus = false;
              }
            });
            this.$message.success("修改成功");
          }
        });
      },
      modifyCrai(index, row) {
        var that = this;
        this.getItems(row.imei, function(data) {
          console.log(data)
          if (data.list.length > 0) {
            if (data.list[0].manufactureId == 201||data.list[0].manufactureId == 221) {
              that.modifyrow = row;
              var content = JSON.parse(row.content);
              that.documentType1 = row.type;
              that.certificatesContent.username = content.username;
              that.certificatesContent.pzname = content.pzname;
              that.certificatesContent.password = content.password;
              var passwordStartTime = moment(row.startTime * 1000).format('YYYY-MM-DD HH:mm:ss')
              var passwordEndTime = moment(row.endTime * 1000).format('YYYY-MM-DD HH:mm:ss')
              that.modifyStatus = false;
              that.$nextTick(function() {
                that.modifyvalue2[0] = passwordStartTime;
                that.modifyvalue2[1] = passwordEndTime;
                that.modifyStatus = true;
              });
              that.certificatestatus = true;
            } else if (data.list[0].manufactureId == 101) {
              that.modifyrow = row;
              var content = JSON.parse(row.content);
              that.documentType1 = row.type;
              that.certificatesContent.username = content.username;
              that.certificatesContent.pzname = content.pzname;
              that.certificatesContent.password = content.password;
              if (content.passwordStartTime && content.passwordEndTime) {
                var passwordStartTime = that.insertStr(content.passwordStartTime, "-");
                var passwordEndTime = that.insertStr(content.passwordEndTime, "-");
              }
              that.modifyStatus = false;
              that.$nextTick(function() {
                that.modifyvalue2[0] = passwordStartTime;
                that.modifyvalue2[1] = passwordEndTime;
                that.modifyStatus = true;
              });
              that.certificatestatus = true;
            }
          } else {
            that.$message.error('未发现对应设备')
          }
        })
      },
      getItems(imei, callback) {
        var data = {
          q: JSON.stringify({
            imei: imei,
            lockType: 1
          }),
          pageNum: 0,
          pageSize: 0,
        };
        httpPost(data, this.$api.getDeviceItems).then((res) => {
          if (res.success) {
            callback(res.data)
          } else {
            this.$message.error("查询设备失败")
          }
        });
      },
      insertStr(soure, newStr) {
        return (
          soure.slice(0, 4) +
          newStr +
          soure.slice(4, 6) +
          newStr +
          soure.slice(6, 8) +
          " " +
          soure.slice(8, 10) +
          ":" +
          soure.slice(10, 12) +
          ":" +
          soure.slice(12, 14)
        );
      },
      // 下发凭证
      Issue(index, row) {
        var data = {};
        var userTelephone = JSON.parse(row.content).userTelephone;
        var userPassword = JSON.parse(row.content).userPassword;
        var passwordEndTime = JSON.parse(row.content).passwordEndTime;
        var passwordStartTime = JSON.parse(row.content).passwordStartTime;
        if (row.type == 1) {
          var fpId = new Date();
          var fingerprint = JSON.parse(row.content).fingerprint;
          var str = {
            imei: row.imei,
            deviceNum: row.deviceNum,
            userTelephone: userTelephone,
            userPassword: userPassword,
            password: JSON.parse(row.content).password,
            length: fingerprint.length / 2,
            fpTimeId: fpId.getTime(),
            operateMode: 16,
            order: 6216,
            fingerprint: fingerprint,
            passwordStartTime: passwordStartTime,
            passwordEndTime: passwordEndTime,
          };
          httpPost(str, this.$api.fingerprintTransfer).then((res1) => {
            if (res1.success) {
              var str1 = JSON.parse(row.content);
              str1.fpTimeId = str.fpTimeId;
              var updatedata = {
                id: row.id,
                content: JSON.stringify(str1),
                serial: res1.data.serialNumber,
              };
              httpPost(updatedata, this.$api.updateCertificates).then(
                (res2) => {}
              );
              this.$message.success("下发成功");
            } else {
              this.$message.error("下发失败");
            }
          });
          this.$message.success("操作成功");
          return false;
        } else if (row.type == 2) {
          data = {
            imei: row.imei,
            deviceNum: row.deviceNum,
            userTelephone: userTelephone,
            cardId: row.cardId,
            userPassword: userPassword,
            password: JSON.parse(row.content).password,
            operateMode: 13,
            order: 6213,
            passwordStartTime: passwordStartTime,
            passwordEndTime: passwordEndTime,
            is: false,
          };
        } else if (row.type == 3) {
          data = {
            imei: row.imei,
            deviceNum: row.deviceNum,
            userTelephone: userTelephone,
            cardId: row.cardId,
            userPassword: userPassword,
            password: JSON.parse(row.content).password,
            operateMode: 10,
            order: 6210,
            passwordStartTime: passwordStartTime,
            passwordEndTime: passwordEndTime,
            is: false,
          };
        } else if (row.type == 4) {
          data = {
            imei: row.imei,
            deviceNum: row.deviceNum,
            userTelephone: userTelephone,
            cardId: row.cardId,
            userPassword: userPassword,
            password: JSON.parse(row.content).password,
            operateMode: 7,
            order: 6207,
            passwordStartTime: passwordStartTime,
            passwordEndTime: passwordEndTime,
            is: false,
          };
        }
        httpPost(data, this.$api.getDeviceOperateUserInfo).then((res) => {
          if (res.success) {
            var updatedata = {
              id: row.id,
              serial: res.data.serialNumber,
            };
            httpPost(updatedata, this.$api.updateCertificates).then((res) => {});
            this.$message.success("下发成功");
          } else {
            this.$message.error("下发失败");
          }
        });
      },
      // 时间处理
      time(time = +new Date()) {
        var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
        return date.toJSON().substr(0, 19).replace("T", " ");
      },
      // 凭证列表获取
      getcertificate() {
        var data = {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          // orderType: "desc",
          orderBy: "create_at desc",
          q: JSON.stringify({
            type: this.selectVcalue,
            // status: this.selectVoucherStatus,
              status:1,
              types:"0,1,2,3,4",
            deviceNum: this.deviceNum
          })
        };
        httpGet(data, this.$api.getCertificates).then((res) => {
          let dataOne=utils.zlib_unzip(res.data)
          dataOne.map((li) => {
            li.createAt = this.time(+new Date(li.createAt));
            li.userTelephone = JSON.parse(li.content).userTelephone;
            // var cardli = this.cardList.filter((item) => item.id == li.cardId)
            // if (cardli.length == 0) {
            //   li['cardName'] = ''
            // } else {
            //   li['cardName'] = cardli[0].name
            // }
            li['pzname'] = JSON.parse(li.content).pzname
            li['statusText'] = utils.statusCode(li.status)
            li['startTimeText'] = utils.formatTime(li.startTime)
            li['endTimeText'] = utils.formatTime(li.endTime)
            if (li.name) {
              li['cardName'] = li.name
            } else {
              li['cardName'] = JSON.parse(li.content).cardName
            }
          });
          this.tableData = dataOne;
          this.total = res.data.total;
        });
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.$refs.pagination.internalCurrentPage = 1;
        this.getlistNum(0, val);
      },
      handleCurrentChange(val) {
        this.getlistNum(val, 10);
      },
      getlistNum(val, size) {
        var data = {
            pageNum: val,
            pageSize: size,
            q: JSON.stringify({
              cardId: this.cardId,
              types: this.selectVcalue || "0,1,2,3,4",
              // status: this.selectVoucherStatus,
              status:1,
              // types:"0,1,2,3,4",
              deviceNum: this.deviceNum
            }),
            orderBy: "create_at desc",
          };
        httpGet(data, this.$api.getCertificates).then((res) => {
          let dataOne=utils.zlib_unzip(res.data)
          dataOne.map((li) => {
            li.createAt = this.time(+new Date(li.createAt));
            li.userTelephone = JSON.parse(li.content).userTelephone;
            // var cardli = this.cardList.filter((item) => item.id == li.cardId)
            // if (cardli.length == 0) {
            //   li['cardName'] = ''
            // } else {
            //   li['cardName'] = cardli[0].name
            // }
            li['pzname'] = JSON.parse(li.content).pzname
            li['statusText'] = utils.statusCode(li.status)
            li['startTimeText'] = utils.formatTime(li.startTime)
            li['endTimeText'] = utils.formatTime(li.endTime)
            if (li.name) {
              li['cardName'] = li.name
            } else {
              li['cardName'] = JSON.parse(li.content).cardName
            }
          });
          this.tableData = dataOne;
          this.total = res.data.total;
        });
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      // 编辑操作
      handleEdit(index, row) {
        this.row = row;
        this.index = index;
        this.startdate = row.startTime;
        this.enddate = row.endTime;
        this.editVisiblelist = true;
      },
      // 保存编辑
      saveEdit() {
        if (
          this.form.password == this.form.password1 &&
          this.startdate != "" &&
          this.enddate != ""
        ) {
          var data = {
            id: this.row.id,
            content: JSON.stringify({
              password: this.form.password
            }),
            startTime: Number(this.startdate),
            endTime: Number(this.enddate),
          };
          httpPost(data, this.$api.updateCertificates).then((res) => {
            this.$message.success("修改成功");
            this.editVisiblelist = false;
          });
        } else {
          this.$message.error("两次密码输入不一致！");
        }
      },
      alldelete() {
        var that = this
        // 二次确认删除
        if (this.multipleSelection.length == 0) {
          this.$message.error("请选择删除数据！");
          return false;
        } else {
          this.$confirm("确定要删除吗？", "提示", {
            type: "warning",
          }).then(() => {
            this.multipleSelection.map((row) => {
              backDevice(row, function(data1) {
                console.log(data1)
                if (data1.data.list.length > 0) {
                  if (data1.data.list[0].manufactureId == 201 || data1.data.list[0].manufactureId == 221) {
                    deleteZgVoucher(row, function(data) {
                      if (data.success) {
                        that.deleteall(row)
                      } else {
                        that.$confirm('是否删除平台凭证！', data.msg, {
                          type: "warning",
                        }).then(() => {
                          that.deleteall(row);
                        })
                      }
                    })
                  } else if (data1.data.list[0].manufactureId == 101) {
                    var str = {};
                    if (row.type == 1) {
                      this.getfiId(row, function(data) {
                        if (data.length > 0) {
                          var fpid = "";
                          if (data[0].face) {
                            fpid = data[0].face.toString(16);
                            if (fpid.length != 2) {
                              fpid = "0" + fpid;
                            }
                          } else {
                            if (data[0].face == 0) {
                              fpid = data[0].face.toString(16);
                              fpid = "0" + fpid;
                            }
                          }
                          str = {
                            imei: row.imei,
                            deviceNum: row.deviceNum,
                            userTelephone: JSON.parse(row.content).userTelephone,
                            operateMode: 18,
                            order: 6218,
                            password: fpid,
                            userPassword: JSON.parse(row.content).userPassword,
                            is: false,
                            passwordStartTime: JSON.parse(row.content).passwordStartTime,
                            passwordEndTime: JSON.parse(row.content).passwordEndTime,
                          };
                          httpPost(str, that.$api.getDeviceOperateUserInfo).then((res1) => {
                            if (res1.success) {
                              that.deleteall(row);
                            } else {
                              that.$message.error('删除失败')
                            }
                          });
                        } else {
                          that.$message.error("该凭还未下发");
                        }
                      });
                      return false;
                    } else if (row.type == 2) {
                      str = {
                        cardId: row.cardId,
                        imei: row.imei,
                        deviceNum: row.deviceNum,
                        userTelephone: JSON.parse(row.content).userTelephone,
                        userPassword: JSON.parse(row.content).userPassword,
                        operateMode: 13,
                        order: 6213,
                        password: JSON.parse(row.content).password,
                        passwordStartTime: "20210813010101",
                        passwordEndTime: "20680813010101",
                        is: false,
                      };
                    } else if (row.type == 3) {
                      str = {
                        cardId: row.cardId,
                        imei: row.imei,
                        deviceNum: row.deviceNum,
                        userTelephone: JSON.parse(row.content).userTelephone,
                        userPassword: JSON.parse(row.content).userPassword,
                        operateMode: 12,
                        order: 6212,
                        is: false,
                        passwordStartTime: "20210813010101",
                        passwordEndTime: "20680813010101",
                      };
                    } else if (row.type == 4) {
                      str = {
                        cardId: row.cardId,
                        imei: row.imei,
                        deviceNum: row.deviceNum,
                        userTelephone: JSON.parse(row.content).userTelephone,
                        operateMode: 9,
                        order: 6209,
                        userPassword: JSON.parse(row.content).userPassword,
                        password: JSON.parse(row.content).password,
                        is: false,
                        passwordStartTime: "20210813010101",
                        passwordEndTime: "20680813010101",
                      };
                    }
                    httpPost(str, that.$api.getDeviceOperateUserInfo).then((res1) => {
                      if (res1.success) {
                        that.deleteall(row);
                      } else {
                        that.$message.error('删除失败')
                      }
                    });
                  }
                } else {
                  that.deleteall(row);
                }
              })
            });
          });
        }
      },
      deleteall(row) {
        var ids = [];
        // this.multipleSelection.map((row) => {
        ids.push(row.id);
        // if (ids.length == this.multipleSelection.length) {
        var data = {
          ids: ids,
        };
        httpPost(data, this.$api.deleteCertificates).then((res) => {
          if (res.success) {
            this.getcertificate();
            this.$message.success("删除成功！");
          } else {
            this.$message.error('平台删除失败')
          }
        });
        // }
        // });
      },
      // 获取指纹id
      getfiId(row, callback) {
        var content = JSON.parse(row.content);
        var data = {
          q: JSON.stringify({
            fpTimeId: content.fpTimeId,
          }),
          pageNum: 1,
          pageSize: 10,
        };
        httpPost(data, this.$api.getDeviceResult).then((res) => {
          let datalist=utils.zlib_unzip(res.data)
          callback(datalist);
        });
      },
      // 删除操作
      handleDelete(index, row) {
        var that = this
        // 二次确认删除
        this.$confirm("确定要删除吗？", "提示", {
            type: "warning",
          })
          .then(() => {
            backDevice(row, function(data1) {
              if (data1.data.list[0].manufactureId == 201 || data1.data.list[0].manufactureId == 221) {
                deleteZgVoucher(row, function(data) {
                  if (data.success) {
                    that.deleteall(row)
                  } else {
                    that.$confirm('是否删除平台凭证！', data.msg, {
                      type: "warning",
                    }).then(() => {
                      that.deleteall(row);
                    })
                  }
                })

              } else if (data1.data.list[0].manufactureId == 101) {
                var str = {};
                if (row.type == 1) {
                  this.getfiId(row, function(data) {
                    if (data.length > 0) {
                      var fpid = "";
                      if (data[0].face) {
                        fpid = data[0].face.toString(16);
                        if (fpid.length != 2) {
                          fpid = "0" + fpid;
                        }
                      } else {
                        if (data[0].face == 0) {
                          fpid = data[0].face.toString(16);
                          fpid = "0" + fpid;
                        }
                      }
                      str = {
                        imei: row.imei,
                        deviceNum: row.deviceNum,
                        userTelephone: JSON.parse(row.content).userTelephone,
                        operateMode: 18,
                        order: 6218,
                        password: fpid,
                        userPassword: JSON.parse(row.content).userPassword,
                        is: false,
                        passwordStartTime: JSON.parse(row.content).passwordStartTime,
                        passwordEndTime: JSON.parse(row.content).passwordEndTime,
                      };
                      httpPost(str, that.$api.getDeviceOperateUserInfo).then((res1) => {
                        if (res1.success) {
                          that.deleteall(row);
                        } else {
                          that.$message.error('删除失败')
                        }
                      });
                    } else {
                      that.$message.error("该凭还未下发");
                    }
                  });
                  return false;
                } else if (row.type == 2) {
                  str = {
                    cardId: row.cardId,
                    imei: row.imei,
                    deviceNum: row.deviceNum,
                    userTelephone: JSON.parse(row.content).userTelephone,
                    userPassword: JSON.parse(row.content).userPassword,
                    operateMode: 13,
                    order: 6213,
                    password: JSON.parse(row.content).password,
                    passwordStartTime: "20210813010101",
                    passwordEndTime: "20680813010101",
                    is: false,
                  };
                } else if (row.type == 3) {
                  str = {
                    cardId: row.cardId,
                    imei: row.imei,
                    deviceNum: row.deviceNum,
                    userTelephone: JSON.parse(row.content).userTelephone,
                    operateMode: 12,
                    order: 6212,
                    userPassword: JSON.parse(row.content).userPassword,
                    is: false,
                    passwordStartTime: "20210813010101",
                    passwordEndTime: "20680813010101",
                  };
                } else if (row.type == 4) {
                  str = {
                    cardId: row.cardId,
                    imei: row.imei,
                    deviceNum: row.deviceNum,
                    userTelephone: JSON.parse(row.content).userTelephone,
                    operateMode: 9,
                    order: 6209,
                    userPassword: JSON.parse(row.content).userPassword,
                    password: JSON.parse(row.content).password,
                    is: false,
                    passwordStartTime: "20210813010101",
                    passwordEndTime: "20680813010101",
                  };
                }
                httpPost(str, this.$api.getDeviceOperateUserInfo).then((res1) => {
                  if (res1.success) {
                    var data = {
                      ids: [row.id],
                    };
                    httpPost(data, this.$api.deleteCertificates).then((res) => {
                      if (res.success) {
                        this.$message.success("删除成功！");
                        this.getcertificate();
                      }
                    });
                  }
                });
              }
            })
          })
          .catch(() => {
            // this.$message.error("删除失败");
          })
      },
    },
  };
</script>

<style scoped>
  .operation {
    margin-bottom: 20px;
  }

  .pagepg {
    text-align: center;
    margin: 20px 0;
  }

  .red {
    color: red;
  }

  .table>>>.iconsize {
    font-size: 18px;
  }

  .mr10 {
    display: inline-block;
    width: 300px;
  }
</style>
