<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="70px"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="票号"
        prop="keyWords1"
      >
        <QRReader v-model="form.keyWords1"></QRReader>
      </el-form-item>
      <el-form-item
        label="票种"
        prop="keyWords2"
      >
        <el-select
          v-model="form.keyWords2"
          clearable
          placeholder="请选择"
        >
          <el-option
            label="全部"
            value=""
          ></el-option>
          <el-option
            v-for="item in ticketTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="绑定设备"
        prop="keyWords3"
      >
        <el-input v-model="form.keyWords3"></el-input>
      </el-form-item>
      <el-form-item
        label="起始时间"
        prop="startDate"
      >
        <el-date-picker
          v-model="form.startDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="截止时间"
        prop="endDate"
      >
        <el-date-picker
          default-time="23:59:59"
          v-model="form.endDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getList"
        >
          搜索
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        统计信息
      </div> -->
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData">
        <el-table-column
          width="190"
          prop="orderNo"
          label="订单号"
        >
        </el-table-column>
        <el-table-column
          width="190"
          prop="ticketNo"
          label="票号"
        >
        </el-table-column>
        <el-table-column
          prop="ticketTypeName"
          label="票种"
        >
        </el-table-column>
        <el-table-column
          width="100"
          label="游玩日期"
        >
          <template slot-scope="props">
            <span>{{ props.row.valid_start_date | formatDate }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="160"
          label="绑定时间"
        >
          <template slot-scope="props">
            <span>{{ props.row.bind_time | formatTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="绑定类型"
        >
          <template slot-scope="props">
            <span>{{ props.row.bind_type | bindType }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="bind_device"
          label="绑定设备"
        >
        </el-table-column>
        <el-table-column
          label="状态"
        >
          <template slot-scope="props">
            <span>{{ props.row.status | ticketStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="160"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="getCheckRecord(scope.row)"
            >
              记录
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="getGuest(scope.row)"
            >
              游客
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getList()"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <ElDialog
      title="二次入园记录"
      width="800px"
      v-model="checkDialogVisible"
    >
      <CheckRecord :data="recordData"></CheckRecord>
    </ElDialog>
    <ElDialog
      title="登记信息"
      width="600px"
      v-model="guestDialogVisible"
    >
      <GuestDialog :info="guestData"></GuestDialog>
    </ElDialog>
  </div>
</template>

<script>
import { secondQuery, secondCheck } from '@/api/secondEnter'
import { getTicketTypeList } from '@/api/ticketType'
import ElDialog from '@/components/Dialog'
import CheckRecord from './secondQuery/CheckRecord'
import GuestDialog from './secondQuery/GuestDialog'
import moment from 'moment'
export default {
  name: 'secondQuery',
  components: {
    ElDialog,
    CheckRecord,
    GuestDialog
  },
  data () {
    return {
      ticketTypeList: [],
      loading: false,
      form: {
        startDate: new Date(moment().startOf('day').valueOf()),
        endDate: new Date(moment().endOf('day').valueOf()),
        keyWords1: '', // ticketNo
        keyWords2: '', // ticketTypeId
        keyWords3: '', // bind_device
        pageNum: 1,
        pageSize: 10
      },
      checkParams: {
        pageNum: 1,
        pageSize: 10000,
        startDate: '',
        endDate: '',
        keyWords1: '', // checker_device_id
        keyWords2: '', // checkType
        keyWords3: '', // check_direction
        keyWords4: '' // ticketNo
      },
      tableData: [],
      total: 0,
      recordData: [],
      checkDialogVisible: false,
      guestData: [],
      guestDialogVisible: false
    }
  },
  filters: {
    bindType (input) {
      switch (input) {
        case 'finger':
          return '指纹'
        case 'face':
          return '人脸'
      }
    }
  },
  created () {
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-二次入园记录-查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getTicketTypeList().then(() => {
      this.getList()
    })
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取票种列表
    getTicketTypeList () {
      this.loading = true
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    getList () {
      this.loading = true
      secondQuery('getTicketBindLogList', this.form).then(res => {
        this.total = res.total
        res.list.forEach(item => {
          let type = this.ticketTypeList.find(t => t.id === item.ticketTypeId)
          if (type) {
            item.ticketTypeName = type.name
          }
        })
        this.tableData = res.list
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-二次入园记录-查询条件', JSON.stringify(this.form)) // 下次记住
      })
    },
    getCheckRecord (item) {
      let params = { ...this.checkParams, ...{ keyWords4: item.ticketNo } }
      this.loading = true
      secondCheck('getTicketBindCheckLogList', params).then(res => {
        this.recordData = res.list
        this.checkDialogVisible = true
      }).finally(() => {
        this.loading = false
      })
    },
    getGuest (item) {
      this.loading = true
      secondQuery('getTicketBindLog', { ticketNo: item.ticketNo }).then(res => {
        this.guestData = res[0]
        this.guestDialogVisible = true
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
