<template>
  <div>
    <div class="gatewrap">
      <!-- 头部导航 -->
      <div class="gate-header">
        <div class="gate-left">
          <span @click='openList' class="today-visitors">今日访客</span>
        </div>
        <div class="gate-right">
          <div class="gate-info">
            <span class="gate-name">{{gateName}}</span>
            <span @click="gateShow = true" class="switch-gate">切换</span>
          </div>
          <div class="fullscreen-btn" @click='screen'>
            {{fullscreen?'取消全屏':'打开全屏'}}
          </div>
        </div>
      </div>
      
      <!-- 主要内容区域 -->
      <div class="gate-content">
        <div class="form-container">
          <div class="form-item">
            <label class="form-label">拜访日期：</label>
            <el-date-picker 
              v-model="selectRange" 
              @change="selectRangeM" 
              value-format="yyyy-MM-dd" 
              type="daterange"
              range-separator="至" 
              start-placeholder="开始日期" 
              end-placeholder="结束日期"
              class="date-picker"
              :popper-class="'mobile-date-picker'"
              append-to-body>
            </el-date-picker>
          </div>
          
          <div class="form-item">
            <label class="form-label">核验销码：</label>
            <el-input 
              clearable 
              ref='codeInput' 
              placeholder="请输入核销码或扫码" 
              v-model="select.code"
              class="form-input" 
              @change='getList'>
            </el-input>
          </div>
          
          <div class="form-item">
            <label class="form-label">身份证件：</label>
            <div class="id-input-group">
              <el-input 
                clearable 
                ref='idInput' 
                placeholder="请输入身份证" 
                v-model="select.idcard"
                class="form-input id-input">
              </el-input>
              <el-button type="primary" @click='getIdcard' class="id-btn">查询身份证</el-button>
            </div>
          </div>
          
          <div class="form-item query-btn-container">
            <el-button class="query-btn" type="primary" @click='getList'>查询</el-button>
          </div>
        </div>
      </div>
    </div>

    <showVisit ref="showVisit" @refresh='clearCode'></showVisit>
    <list ref='list'></list>
    <!-- 选择门岗人员 -->
    <el-dialog title="请先选择门岗人员" :visible.sync="gateShow" width="60%" :close-on-click-modal='false' :show-close='false'>
      <el-radio-group v-model="gateAdminId">
        <el-radio v-for="item in gateData" :label="item.id" border>{{item.name}}</el-radio>
      </el-radio-group>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmGate">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
  import Cookies from 'js-cookie'
  import axios from 'axios'
  import showVisit from '@/views/visit/component/showVisit'
  import list from '@/views/gate/components/list.vue'
  import {
    getList,
    getUserList
  } from '@/api/gate'
  export default {
    components: {
      showVisit,
      list

    },
    data() {
      return {
        fullscreen: false,
        gateShow: false,
        gateAdminId: '',
        gateName: "",
        gateData: [],
        gateUser: {},
        selectRange: [],
        select: {
          page: 1,
          rows: 10,
          keyword: "",
          audit_status: '',
          start_date: "",
          end_date: "",
          is_export: 0,
          code: "",
          idcard: ''
        },
        data: [],

      }
    },
    computed: {},
    mounted() {
      // this.screen()
    },
    created() {
      // this.getIdcard()
      this.getUserList()
      this.getToday()
      // this.enterfullscreen()
    },
    methods: {
      openList(){
        this.$refs.list.listShow = true
      },
      getToday() {
        let now = new Date()
        let nowDay = this.$moment(now).format("YYYY-MM-DD")
        this.select.start_date = nowDay
        this.select.end_date = nowDay
        this.selectRange = [nowDay, nowDay]
      },
      selectRangeM(val) {
        console.log(val)
        if (val) {
          this.select.start_date = val[0]
          this.select.end_date = val[1]
        } else {
          this.select.start_date = ""
          this.select.end_date = ""
        }
      },
      clearCode() {
        this.select.code = ''
        this.select.idcard = ''
        this.$nextTick(() => {
          this.$refs.codeInput.focus()
        })
      },
      async getList() {
        if (this.select.code == '' && this.select.idcard == '') {
          this.$successMessage("请输入核销码或身份证件", '', 'warning')
          return
        }
        let res = await getList(this.select)
        this.data = res.data
        if (this.data.length > 0) {
          for (var k of this.data) {
            if (k.audit_status == 1 || k.audit_status == 3) {
              this.$refs['showVisit'].form = k
              this.$refs['showVisit'].formDataType = 'coderecord'
              this.$refs['showVisit'].gateAdminId = this.gateAdminId
              this.$refs['showVisit'].isShow = true
              return
            } else {
              this.$successMessage(k.audit_status_text, '', 'success')
            }
          }
        } else {
          this.$successMessage("未查询到记录", '', 'warning')
        }
        this.select.code = ''
        this.select.idcard = ''
      },
      async getUserList() {
        this.gateUser = Cookies.get("gateUser") ? JSON.parse(Cookies.get("gateUser")) : ''

        console.log(this.gateUser)
        let res = await getUserList()
        this.gateData = res
        if (this.gateUser.gateAdminId) {
          this.gateAdminId = this.gateUser.gateAdminId
          this.gateName = this.gateUser.gateName
          this.gateShow = false
          return
        } else {
          this.gateShow = true
        }

      },
      confirmGate() {
        if (!this.gateAdminId) {
          this.$successMessage("请先选择门岗", '', 'warning')
          return
        }
        this.gateData.map(item => {
          if (this.gateAdminId == item.id) {
            this.gateName = item.name
          }
        })
        Cookies.set('gateUser', {
          gateName: this.gateName,
          gateAdminId: this.gateAdminId
        })
        this.$nextTick(() => {
          this.$refs.codeInput.focus()
        })
        this.gateShow = false
      },
      getIdcard() {
        let that = this
        axios.get('https://127.0.0.1:24011/ZKIDROnline/ScanReadIdCardInfo?OP-DEV=1&CMD-URL=4&REPEAT=1&READTYPE=1',{
          'headers':{
            "Content-Type":'application/json'
          }
        })
        .then(res=>{
            console.log(res)
            console.log(res.data)
            if(!res.data){
              return
            }
            let data1 = res.data?res.data.split('"IDNumber"'):''
            let data2= data1[1].split(",")
            let data3 =data2[0].replace(/[^\d]/g, "")
            that.select.idcard = data3
            that.getList()
        }).catch(err=>{
          console.log(err)
               this.$successMessage(err.statusText, '', 'warning')
        })
      },
      screen() {
        let element = document.documentElement;
        if (this.fullscreen) {
          if (document.exitFullscreen) {
            document.exitFullscreen();
          } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
          } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
          } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
          }
        } else {
          if (element.requestFullscreen) {
            element.requestFullscreen();
          } else if (element.webkitRequestFullScreen) {
            element.webkitRequestFullScreen();
          } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen();
          } else if (element.msRequestFullscreen) {
            // IE11
            element.msRequestFullscreen();
          }
        }
        this.fullscreen = !this.fullscreen;
      },
      enterfullscreen() { //进入全屏
        var docElm = document.documentElement
        //W3C
        if (docElm.requestFullscreen) {
          docElm.requestFullscreen()
        }
        //FireFox
        else if (docElm.mozRequestFullScreen) {
          docElm.mozRequestFullScreen()
        }
        //Chrome等
        else if (docElm.webkitRequestFullScreen) {
          docElm.webkitRequestFullScreen()
        }
        //IE11
        else if (elem.msRequestFullscreen) {
          elem.msRequestFullscreen()
        }
      },
      //退出全屏
      exitfullscreen() {
        if (document.exitFullscreen) {
          document.exitFullscreen()
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen()
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen()
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen()
        }
      }

    },
  }
</script>

<style scoped>
/* 基础容器样式 */
.gatewrap {
  background-color: #fff;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 头部导航样式 */
.gate-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #f0f0f0;
  background: #fafafa;
  
  @media (max-width: 767px) {
    padding: 10px 15px;
    flex-direction: column;
    gap: 10px;
    align-items: stretch;
  }
}

.gate-left {
  .today-visitors {
    color: #004593;
    font-size: 18px;
    cursor: pointer;
    
    @media (max-width: 767px) {
      font-size: 16px;
    }
    
    &:hover {
      text-decoration: underline;
    }
  }
}

.gate-right {
  display: flex;
  align-items: center;
  gap: 20px;
  
  @media (max-width: 767px) {
    justify-content: space-between;
    gap: 10px;
  }
}

.gate-info {
  display: flex;
  align-items: center;
  gap: 5px;
  
  .gate-name {
    font-size: 16px;
    color: #333;
    
    @media (max-width: 767px) {
      font-size: 14px;
    }
  }
  
  .switch-gate {
    color: #004593;
    text-decoration: underline;
    cursor: pointer;
    font-size: 14px;
    
    @media (max-width: 767px) {
      font-size: 12px;
    }
  }
}

.fullscreen-btn {
  color: #666;
  cursor: pointer;
  font-size: 14px;
  
  @media (max-width: 767px) {
    font-size: 12px;
  }
  
  &:hover {
    color: #004593;
  }
}

/* 主要内容区域 */
.gate-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  
  @media (max-width: 767px) {
    align-items: flex-start;
    padding: 20px 15px;
  }
}

.form-container {
  width: 100%;
  max-width: 500px;
  
  @media (min-width: 768px) {
    max-width: 600px;
  }
}

.form-item {
  margin-bottom: 25px;
  
  @media (max-width: 767px) {
    margin-bottom: 20px;
  }
  
  &:last-child {
    margin-bottom: 0;
  }
}

.form-label {
  display: block;
  font-size: 16px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 500;
  
  @media (max-width: 767px) {
    font-size: 14px;
    margin-bottom: 6px;
  }
  
  @media (min-width: 768px) {
    font-size: 20px;
    margin-bottom: 10px;
  }
}

.form-input {
  width: 100%;
}

.date-picker {
  width: 100%;
}

.id-input-group {
  display: flex;
  gap: 10px;
  
  @media (max-width: 767px) {
    flex-direction: column;
    gap: 8px;
  }
}

.id-input {
  flex: 1;
}

.id-btn {
  @media (max-width: 767px) {
    width: 100%;
  }
  
  @media (min-width: 768px) {
    min-width: 120px;
  }
}

.query-btn-container {
  text-align: center;
  margin-top: 30px;
  
  @media (max-width: 767px) {
    margin-top: 25px;
  }
}

.query-btn {
  width: 100%;
  height: 50px;
  font-size: 18px;
  font-weight: 600;
  
  @media (max-width: 767px) {
    height: 44px;
    font-size: 16px;
  }
  
  @media (min-width: 768px) {
    height: 60px;
    font-size: 24px;
  }
}

/* Element UI 组件样式覆盖 */
::v-deep .el-input__inner {
  height: 44px;
  font-size: 16px;
  
  @media (max-width: 767px) {
    height: 40px;
    font-size: 14px;
  }
  
  @media (min-width: 768px) {
    height: 50px;
    font-size: 18px;
  }
}

::v-deep .el-button {
  height: 44px;
  font-size: 14px;
  
  @media (max-width: 767px) {
    height: 40px;
    font-size: 13px;
  }
  
  @media (min-width: 768px) {
    height: 50px;
    font-size: 16px;
  }
}

::v-deep .el-date-editor {
  width: 100%;
}

::v-deep .el-range-editor .el-range-input {
  font-size: 14px;
  
  @media (min-width: 768px) {
    font-size: 16px;
  }
}

::v-deep .el-date-editor .el-range-separator {
  font-size: 14px;
  
  @media (min-width: 768px) {
    font-size: 16px;
  }
}

::v-deep .el-date-editor .el-range__icon,
::v-deep .el-date-editor .el-range__close-icon {
  font-size: 14px;
}

/* 对话框样式优化 */
::v-deep .el-dialog {
  @media (max-width: 767px) {
    width: 90% !important;
    margin-top: 15vh !important;
  }
}

::v-deep .el-radio {
  @media (max-width: 767px) {
    width: 100%;
    margin-bottom: 10px;
  }
}


/* 日期选择器输入框在移动端的样式调整 */
::v-deep .el-range-editor {
  @media (max-width: 767px) {
    .el-range-input {
      width: 35% !important;
      font-size: 13px !important;
    }
    
    .el-range-separator {
      width: 15% !important;
      font-size: 12px !important;
      text-align: center;
    }
    
    .el-range__icon,
    .el-range__close-icon {
      font-size: 12px !important;
    }
  }
}
</style>

<!-- 不带scoped的样式，专门处理日期选择器弹出层 -->
<style>
/* 移动端日期选择器弹出层优化 */
@media (max-width: 767px) {
  .mobile-date-picker.el-picker-panel {
    position: fixed !important;
    left: 10px !important;
    right: 10px !important;
    top: 50% !important;
    transform: translateY(-50%) !important;
    width: auto !important;
    max-width: none !important;
    min-width: auto !important;
    margin: 0 !important;
    z-index: 9999 !important;
  }
  
  .mobile-date-picker.el-date-range-picker {
    min-width: auto !important;
    width: 100% !important;
  }
  
  .mobile-date-picker .el-picker-panel__body {
    display: flex !important;
    flex-direction: column !important;
    min-width: auto !important;
    width: 100% !important;
  }
  
  .mobile-date-picker .el-picker-panel__content {
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
  }
  
  .mobile-date-picker .el-picker-panel__content .el-date-table {
    margin: 0 5px 10px 5px !important;
    width: calc(100% - 10px) !important;
  }
  
  .mobile-date-picker .el-date-range-picker__time-header {
    display: flex !important;
    flex-direction: column !important;
    gap: 5px !important;
    padding: 5px !important;
  }
  
  .mobile-date-picker .el-picker-panel__sidebar {
    width: 100% !important;
    padding: 5px !important;
  }
  
  .mobile-date-picker .el-picker-panel__sidebar .el-picker-panel__shortcut {
    display: inline-block !important;
    margin: 2px !important;
    padding: 3px 8px !important;
    font-size: 12px !important;
  }
  
  .mobile-date-picker .el-picker-panel__footer {
    text-align: center !important;
    padding: 10px !important;
  }
}
</style>
