<template>
  <div class="p-2">
    <zw ref="mychild"></zw>
    <div class="searvh">
      <el-select v-model="roomFirst" placeholder="请选择房源" clearable size="small" class="w-2 mb-2" @change="getRoomMethods(1)">
        <el-option v-for="item in firstAuthList" :key="item.code" :label="item.value" :value="item.code">
        </el-option>
      </el-select>
      <el-select v-model="roomSecond" placeholder="请选择楼宇" clearable size="small" class="w-2 mb-2" @change="getRoomMethods(2)">
        <el-option v-for="item in secondAuthList" :key="item.code" :label="item.value" :value="item.code">
        </el-option>
      </el-select>
      <el-select v-model="roomThree" placeholder="请选择单元" clearable size="small" class="w-2 mb-2" @change="getRoomMethods(3)">
        <el-option v-for="item in threeAuthList" :key="item.code" :label="item.value" :value="item.code">
        </el-option>
      </el-select>
      <el-select v-model="roomFour" placeholder="请选择楼层" clearable size="small" class="w-2 mb-2" @change="getRoomMethods(4)">
        <el-option v-for="item in fourAuthList" :key="item.code" :label="item.value" :value="item.code">
        </el-option>
      </el-select>
      <el-select v-model="roomFive" placeholder="请选择房间" clearable size="small" class="w-2 mb-2"  @change="getRoomMethods(5)">
        <el-option v-for="item in romeLockerList" :key="item.code" :label="item.value" :value="item.code">
        </el-option>
      </el-select>
      <el-button type="primary" clearable size="small" class="mb-2" @click.native="addNfc" style="background: #FA8E00; border: none;">读卡</el-button>
      <el-button type="primary" clearable size="small" class="mb-2" @click="showDialogMethods" style="background: #FA8E00; border: none;">写卡</el-button>
    </div>
    <div>
      <el-table :data="tableData" stripe style="width: 100%" height="600">
        <el-table-column type="index" width="80" label="序号">
        </el-table-column>
        <el-table-column prop="cardNum" label="卡号">
        </el-table-column>
        <el-table-column prop="authName" label="权限名称">
        </el-table-column>
        <el-table-column prop="startTime" label="开始时间">
        </el-table-column>
        <el-table-column prop="endTime" label="结束时间">
        </el-table-column>
        <el-table-column prop="useCount" label="使用次数">
          <template #default="scope">
            <span v-if="scope.row.useCount==255">{{scope.row.useCountStr}}</span>
            <span v-else>{{scope.row.useCount}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间">
        </el-table-column>
        <el-table-column prop="useCount" label="操作">
          <template #default="scope">
            <span @click="deleteCardMethods(scope.$index,scope.row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="发放离线卡" :visible.sync="dialogVisible" width="50%">
      <div class="formdata">
        <el-date-picker v-model="submitData.startTime" clearable size="middle d-block" class="mb-2" type="datetime"
          placeholder="选择开始日期时间">
        </el-date-picker>
        <el-date-picker v-model="submitData.endTime" clearable size="middle d-block" class="mb-2" type="datetime"
          placeholder="选择结束日期时间">
        </el-date-picker>
        <el-input v-model="submitData.num" clearable size="middle" class="" placeholder="请输入使用次数,默认永久"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="writeNfc">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    httpPost
  } from "../../api";
  import zw from "../zw.vue"
  import moment from 'moment'
  export default {
    inject: ['reload'],
    name: "offInCardOver",
    data() {
      return {
        dialogVisible:false,
        firstAuthList: [],
        secondAuthList: [],
        threeAuthList: [],
        fourAuthList: [],
        romeLockerList: [],
        options: [],
        roomFirst:"",
        roomSecond:"",
        roomThree:"",
        roomFour:"",
        roomFive:"",
        submitData: {
          cardNo:"",
          num: "",
          endTime: "",
          startTime: ""
        },
        tableData:[],
        romeFiveAuth:[],
        FiveAuthList:{
          communcationKey:"",
          firstAuth:"",
          secondAuth:"",
          threeAuth:"",
          fourAuth:"",
        },
        privateKey:"",
        privateKeyType:""
      }
    },
    components: {zw},
    created() {
      this.getFirstAuthListMethods()
    },
    methods: {
      // 删除
      deleteCardMethods(index,row){
        var data3 = {
          code: "zg/auth/clearCardAuth",
          data: {
            cardNo:row.cardNum
          },
        };
        httpPost(data3, this.$api.sendCommand).then((res) => {
          if(res.code==200){
            this.getCardBindAuthList()
          }
        });
      },
      // 房源改变
      getRoomMethods(e){
        if(e==1){
          if(this.roomFirst!=''){
             this.getSecondAuthList();this.privateKeyType=1;
          }else{
            this.roomSecond=""
            this.roomThree=""
            this.roomFour=""
            this.roomFive=""
            this.privateKeyType=""
          }
        }else if(e==2){
          if(this.roomSecond!=''){
             this.getThreeAuthList();this.privateKeyType=2;
          }else{
            this.roomThree=""
            this.roomFour=""
            this.roomFive=""
            this.privateKeyType=1
          }
        }else if(e==3){
          if(this.roomThree!=''){
             this.getFourAuthList();this.privateKeyType=3;
          }else{
            this.roomFour=""
            this.roomFive=""
            this.privateKeyType=2
          }
        }else if(e==4){
          if(this.roomFour!=''){
             this.getRomeLockerList();this.privateKeyType=4;
          }else{
            this.roomFive=""
            this.privateKeyType=3
          }
        }else if(e==5){
          if(this.roomFive!=''){
             this.privateKeyType=5;
          }else{
            this.privateKeyType=4
          }
        }
        console.log(this.privateKeyType)
        // switch(e){
        //   case 1: this.getSecondAuthList();this.privateKeyType=1; break;
        //   case 2: this.getThreeAuthList();this.privateKeyType=2; break;
        //   case 3: this.getFourAuthList();this.privateKeyType=3; break;
        //   case 4: this.getRomeLockerList();this.privateKeyType=4; break;
        //   case 5: this.privateKeyType=5; break;
        // }
      },
      // 获取一级房源
      getFirstAuthListMethods(){
        var data3 = {
          code: "zg/auth/firstAuthList",
          data: {
          },
        };
        httpPost(data3, this.$api.sendCommand).then((res) => {
          if(res.code==200){
            this.firstAuthList=res.data
          }
        });
      },
      // 获取二级房源
      getSecondAuthList(){
        var data3 = {
          code: "zg/auth/secondAuthList",
          data: {
            parentId:this.roomFirst
          },
        };
        httpPost(data3, this.$api.sendCommand).then((res) => {
          if(res.code==200){
            this.secondAuthList=res.data
          }
        });
      },
      // 获取三级房源
      getThreeAuthList(){
        var data3 = {
          code: "zg/auth/threeAuthList",
          data: {
            parentId:this.roomSecond
          },
        };
        httpPost(data3, this.$api.sendCommand).then((res) => {
          if(res.code==200){
            this.threeAuthList=res.data
          }
        });
      },
      // 获取四级房源
      getFourAuthList(){
        var data3 = {
          code: "zg/auth/fourAuthList",
          data: {
            parentId:this.roomThree
          },
        };
        httpPost(data3, this.$api.sendCommand).then((res) => {
          if(res.code==200){
            this.fourAuthList=res.data
          }
        });
      },
      // 获取五级房源
      getRomeLockerList(){
        var data3 = {
          code: "zg/auth/getRomeLockerList",
          data: {
            parentId:this.roomFour
          },
        };
        httpPost(data3, this.$api.sendCommand).then((res) => {
          if(res.code==200){
            this.romeLockerList=res.data
          }
        });
      },
      showDialogMethods(){
        this.dialogVisible=true
        var roomId="",type=""
        if(this.roomFive!=''){
          roomId=this.roomFive
          type=5
        }else if(this.roomFour!=''){
          roomId=this.roomFour
          type=4
        }else if(this.roomThree!=''){
          roomId=this.roomThree
          type=3
        }else if(this.roomSecond!=''){
          roomId=this.roomSecond
          type=2
        }else if(this.roomFirst!=''){
          roomId=this.roomFirst
          type=1
        }
        this.getromeFiveAuth(roomId,type)
        this.addNfc()
      },
      getromeFiveAuth(roomId,type){
        if(type==1){
          var result=this.firstAuthList.filter(item=> item.code==roomId)[0]
          if(result){
            this.getPrivateKey(result.value,type)
          }
        }else if(type==2){
          var result=this.secondAuthList.filter(item=> item.code==roomId)[0]
          if(result){
            this.getPrivateKey(result.value,type)
          }
        }else if(type==3){
          var result=this.threeAuthList.filter(item=> item.code==roomId)[0]
          if(result){
            this.getPrivateKey(result.value,type)
          }
        }else if(type==4){
          var result=this.fourAuthList.filter(item=> item.code==roomId)[0]
          if(result){
            this.getPrivateKey(result.value,type)
          }
        }else if(type==5){
          var result=this.romeLockerList.filter(item=> item.code==roomId)[0]
          if(result){
            this.getPrivateKey(result.value,type)
          }
        }
      },
      getPrivateKey(authName,type){
        var data={
          code: "zg/auth/findAuthCodeInfo",
          data: {
            authName:authName,
            type:type
          },
        }
        httpPost(data,this.$api.sendCommand).then((res) => {
          if(res.code==200){
            this.privateKey=res.data
          }
        });
      },
      setVoucherNfc(data){
        this.submitData.cardNo=data.cardNo
        this.getCardBindAuthList()
      },
      getCardBindAuthList(){
        var data3 = {
          code: "zg/auth/findCardBindAuth",
          data: {
            cardNo:this.submitData.cardNo
          },
        };
        httpPost(data3, this.$api.sendCommand).then((res4) => {
          this.tableData=res4.data
        });
      },
      addNfc() {
        var _this = this
        this.$refs.mychild.cardFun(0x8010, function(data) {
          if (data.status == 0) {
            console.log(data)
            _this.setVoucherNfc(data)
          } else {
            _this.$message.error("读卡错误，请检查读卡器是否正常！")
          }
        })
      },
      writeNfc() {
        var _this = this
        if(this.privateKeyType==''){
            _this.$message.error("请选择房源")
          return
        }
        if(this.submitData.startTime=='' || this.submitData.endTime==''){
            _this.$message.error("请选择开始结束时间")
          return
        }
        if(this.submitData.cardNo==''){
            _this.$message.error("请放置卡片在读卡器上")
          return
        }
        var cardNo=this.submitData.cardNo
        var startTime=moment(this.submitData.startTime).format("X").toString(16)
        var endTime=moment(this.submitData.endTime).format("X").toString(16)
        var useCountLimit=this.submitData.num.toString(16)
        var privateKey= this.privateKey
        if(this.submitData.num==''){
          useCountLimit='255'
        }
        useCountLimit=useCountLimit.toString(16)
         var keyType=""
        if(this.privateKeyType==1){
            keyType=0x01
        }else if(this.privateKeyType==2){
            keyType=0x02
        }else if(this.privateKeyType==3){
            keyType=0x03
        }else if(this.privateKeyType==4){
            keyType=0x04
        }else if(this.privateKeyType==5){
            keyType=0xff
        }
        this.$refs.mychild.writeCard(0x8011,cardNo,startTime,endTime,useCountLimit,privateKey,keyType,(data)=>{
          if (data.status == 0) {
            console.log(data)
            var data3 = {
              code: "zg/auth/reportCardAuthInfo",
              data: {
                authCode: privateKey,
                cardNo: cardNo,
                endTime: moment(this.submitData.endTime).format("X"),
                startTime: moment(this.submitData.startTime).format("X"),
                useCount: useCountLimit
              },
            };
            httpPost(data3, this.$api.sendCommand).then((res4) => {
              if(res4.code==200){
                _this.dialogVisible=false
                _this.$message.success("上报成功")
              }
            });
          } else {
            console.log(data)
            _this.$message.error("写卡错误，请检查读卡器是否正常！")
          }
        })
      },

    }
  }
</script>

<style scoped>
  .formdata .d-block{
    width: 100%;
  }
</style>
