<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">
        <el-input v-model="form.keyWords1"></el-input>
      </el-form-item>
      <el-form-item
        label="票号"
        prop="keyWords2">
        <QRReader v-model="form.keyWords2"></QRReader>
      </el-form-item>
      <el-form-item
        label="取票码"
        prop="keyWords9">
        <el-input v-model="form.keyWords9"></el-input>
      </el-form-item>
      <el-form-item
        label="游客姓名"
        prop="keyWords7">
        <el-input v-model="form.keyWords7"></el-input>
      </el-form-item>
      <el-form-item
        label="游客手机"
        prop="keyWords6">
        <el-input v-model="form.keyWords6"></el-input>
      </el-form-item>
      <el-form-item
        label="身份证号"
        prop="keyWords5">
        <ReaderInput
          type="ID"
          v-model="form.keyWords5"></ReaderInput>
      </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>
      <el-form-item
        label="票状态"
        prop="keyWords4">
        <el-select
          v-model="form.keyWords4"
          clearable
          placeholder="请选择">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in ticketStatusDic"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="票种"
        prop="keyWords3">
        <el-select
          v-model="form.keyWords3"
          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="keyWords10">
        <el-select
          v-model="form.keyWords10"
          clearable
          placeholder="请选择">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in salesList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getList(true)">
          搜索
        </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="160"
          prop="orderNo"
          label="订单号">
        </el-table-column>
        <el-table-column
          width="160"
          prop="ticketNo"
          label="票号">
        </el-table-column>
        <el-table-column
          prop="selfservice_code"
          label="取票码">
        </el-table-column>
        <el-table-column
          prop="ticketTypeName"
          label="票种">
        </el-table-column>
        <el-table-column
          prop="guestName"
          label="游客姓名">
        </el-table-column>
        <el-table-column
          prop="guestPhone"
          label="手机号码">
        </el-table-column>
        <el-table-column
          width="160"
          prop="guestIdentify"
          label="证件号码">
          <template slot-scope="scope">
            <!-- {{ scope.row.guestIdentify | formatIDCard }} -->
            <!-- 但若与三目表达式结合起来，|过滤器会失效 https://blog.csdn.net/z858466/article/details/113050625 vue的filter在三元表达式中的问题 -->
            {{ isPapersIdDesensitize ? $options.filters.formatIDCard(scope.row.guestIdentify) : scope.row.guestIdentify }}
          </template>
        </el-table-column>
        <el-table-column
          width="160"
          label="取票时间">
          <template slot-scope="scope">
            {{ scope.row.print_time | formatTime }}
          </template>
        </el-table-column>

        <el-table-column
          prop="print_manager_nickName"
          label="操作人">
        </el-table-column>

        <el-table-column
          label="状态">
          <template slot-scope="scope">
            {{ scope.row.status | ticketStatus }}
          </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>
  </div>
</template>

<script>
import { getTicketPrintList } from '@/api/printOnlineTicket'
import { getTicketTypeList } from '@/api/ticketType'
import { ticketStatusDic } from '@/assets/staticData'

import ReaderInput from '@/components/ReaderInput'
import moment from 'moment'

export default {
  name: 'printOlQuery',
  components: {
    ReaderInput
  },
  computed: {
    isPapersIdDesensitize () {
      const value = this.$store.state.app.systemConfig.find(i => i.configKey === 'papers_id_desensitize')?.configValue
      return value === '1'
    },
    salesList () {
      return this.$store.state.user.salesList
    }
  },
  data () {
    return {
      ticketStatusDic: ticketStatusDic,
      ticketTypeList: [],
      form: {
        startDate: new Date(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')),
        endDate: new Date(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')),
        keyWords1: '', // 订单号
        keyWords2: '', // 票号
        keyWords3: '', // 票种id
        keyWords4: '', // 票状态
        keyWords5: '', // 游客证件号
        keyWords6: '', // 游客手机
        keyWords7: '', // 游客姓名
        keyWords8: '', // 导游姓名
        keyWords9: '', // 取票码
        keyWords10: '', // 取票码
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      loading: false
    }
  },
  created () {
    this.getTicketTypeList()
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-取票记录-查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getList()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    getList (goFirst) {
      this.loading = true
      goFirst && (this.form.pageNum = 1)

      let params = JSON.parse(JSON.stringify(this.form))

      params.keyWords1 = moment(params.keyWords1).format('YYYY-MM-DD HH:mm:ss')
      params.keyWords2 = moment(params.keyWords2).format('YYYY-MM-DD HH:mm:ss')

      getTicketPrintList(this.form).then(res => {
        this.total = res.total
        this.tableData = res.list
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-取票记录-查询条件', JSON.stringify(this.form)) // 下次记住
      })
    }
  }
}
</script>

<style scoped>

</style>
