<template>
  <div class="form-wrap">
    <el-form
      class="searchBox"
      ref="form"
      :model="form"
      :inline="true"
      label-width="70px"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="检票起始时间"
        prop="startDate"
        label-width="150px"
      >
        <el-date-picker
          v-model="form.startDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="检票截止时间"
        prop="endDate"
        label-width="150px"
      >
        <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="startDate1"
        label-width="150px"
      >
        <el-date-picker
          v-model="form.startDate1"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="售票截止时间"
        prop="endDate1"
        label-width="150px"
      >
        <el-date-picker
          default-time="23:59:59"
          v-model="form.endDate1"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="票种"
        prop="keyWords1"
      >
        <el-select
          v-model="form.keyWords1"
          clearable
          placeholder="请选择"
        >
          <el-option
            value=""
            label="全部"></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="keyWords2"
      >
        <el-input v-model="form.keyWords2"></el-input>
      </el-form-item>
      <el-form-item
        label="票号"
        prop="keyWords3"
      >
        <QRReader v-model="form.keyWords3"></QRReader>
      </el-form-item>
      <el-form-item
        label="身份证号"
        prop="keyWords13">
        <ReaderInput v-model="form.keyWords13"></ReaderInput>
      </el-form-item>
      <el-form-item
        label="游客姓名"
        prop="keyWords4"
      >
        <el-input v-model="form.keyWords4"></el-input>
      </el-form-item>
      <el-form-item
        label="游客手机"
        prop="keyWords5"
      >
        <el-input v-model="form.keyWords5"></el-input>
      </el-form-item>
      <el-form-item
        label="景点"
        prop="keyWords8"
      >
        <el-select
          v-model="form.keyWords8"
          @change="getChannelGroup"
        >
          <el-option
            value=""
            label="全部"
          >
          </el-option>
          <el-option
            v-for="item in userScenic"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="通道组"
        prop="keyWords10"
      >
        <el-select
          v-model="form.keyWords10"
          @change="getChannel"
        >
          <el-option
            value=""
            label="全部"
          >
          </el-option>
          <el-option
            v-for="item in channelGroupList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="通道"
        prop="keyWords9"
      >
        <el-select
          v-model="form.keyWords9"
          @change="getDevice"
        >
          <el-option
            value=""
            label="全部"
          >
          </el-option>
          <el-option
            v-for="item in channelList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="检票设备"
        prop="keyWords6"
      >
        <el-select
          clearable
          v-model="form.keyWords6"
        >
          <el-option
            value=""
            label="全部"
          >
          </el-option>
          <el-option
            v-for="item in deviceList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="查询方式"
        prop="keyWords7"
      >
        <el-select
          v-model="form.keyWords7"
          placeholder="请选择"
        >
          <el-option
            label="按票"
            value="ticket"
          >
          </el-option>
          <el-option
            label="按订单"
            value="order"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="检票类型"
        prop="keyWords11"
      >
        <el-select v-model="form.keyWords11">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            label="闸机检票"
            value="CHECKER">
          </el-option>
          <el-option
            label="人工检票"
            value="PERSON">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="检票员"
        prop="keyWords12">
        <el-select v-model="form.keyWords12">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in salesList"
            :key="item.id"
            :label="item.nick_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="getCheckList(true)"
        >
          搜索
        </el-button>
      </div>
    </el-form>
    <!-- 注意，不应当在@click的getCheckList(true)加个分号然后再写Storage的相关操作，实测可能会因为第一句未执行成功导致第二句不执行 -->
    <div class="tableBox">
      <div class="block-title">
        检票列表
      </div>
      <el-table
        border
        stripe
        v-loading="loading"
        :data="orderTableData"
        key="orderTable"
        v-if="queryType === 'order'"
      >
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-table :data="scope.row.check_info">
              <el-table-column
                prop="ticket_no"
                label="票号"
              ></el-table-column>
              <el-table-column
                prop="scenic_name"
                label="景区"
              ></el-table-column>
              <el-table-column
                prop="checker_channel_group_name"
                label="通道组"
              ></el-table-column>
              <el-table-column
                prop="checker_channel_name"
                label="通道"
              ></el-table-column>
              <el-table-column
                prop="checker_name"
                label="检票设备"
              ></el-table-column>
              <el-table-column
                prop="ip"
                label="设备IP"
              ></el-table-column>
              <el-table-column label="检票时间">
                <template slot-scope="checkScope">
                  {{ checkScope.row.check_time | formatTime }}
                </template>
              </el-table-column>
              <el-table-column
                prop="has_checked_num"
                label="已检次数"
              ></el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column
          width="160"
          prop="order_no"
          label="订单号"
        >
        </el-table-column>
        <el-table-column
          prop="buyer_name"
          label="预订人"
        >
        </el-table-column>
        <el-table-column
          prop="buyer_phone"
          label="手机号"
        >
        </el-table-column>
        <el-table-column
          prop="buyer_identify"
          label="身份证号"
        >
        </el-table-column>
        <el-table-column
          width="100"
          prop="ticket_type_name"
          label="票型"
        >
        </el-table-column>
        <el-table-column
          width="80"
          prop="sum_count"
          label="总票数"
        >
        </el-table-column>
        <el-table-column
          width="80"
          prop="check_count"
          label="已检"
        >
        </el-table-column>
        <el-table-column
          width="80"
          prop="cancel_count"
          label="已退"
        >
        </el-table-column>
      </el-table>

      <el-table
        v-loading="loading"
        :data="ticketTableData"
        key="ticketTable"
        v-if="queryType === 'ticket'"
      >
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form
              inline
              class="table-expand"
            >
              <el-form-item label="游客姓名">
                <span>{{ scope.row.guestName }}</span>
              </el-form-item>
              <el-form-item label="游客电话">
                <span>{{ scope.row.guestPhone }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column
          prop="order_no"
          label="订单号"
        >
        </el-table-column>
        <el-table-column
          prop="ticket_no"
          label="票号"
        >
        </el-table-column>
        <el-table-column
          prop="scenic_name"
          label="景点"
        >
        </el-table-column>
        <el-table-column
          prop="checker_channel_group_name"
          label="通道组"
        >
        </el-table-column>
        <el-table-column
          prop="checker_channel_name"
          label="通道"
        >
        </el-table-column>
        <el-table-column
          prop="ticket_type_name"
          label="票型"
        >
        </el-table-column>
        <el-table-column
          prop="check_type"
          label="检票类型"
          width="110"
        >
          <template slot-scope="scope">
            <el-tag
              type="danger"
              v-if="scope.row.check_type==='PERSON'">
              人工检票
            </el-tag>

            <el-tag
              type="success"
              v-if="scope.row.check_type==='CHECKER'">
              闸机检票
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="check_manager_name"
          label="检票人"
        >
        </el-table-column>
        <el-table-column
          prop="checker_name"
          label="检票设备"
        >
        </el-table-column>
        <el-table-column
          prop="ip"
          label="设备IP"
        >
        </el-table-column>
        <el-table-column
          prop="has_checked_num"
          label="已检次数"
        >
        </el-table-column>
        <el-table-column
          width="160"
          label="最后检票时间"
        >
          <template slot-scope="scope">
            {{ scope.row.check_time | formatTime }}
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page.sync="form.pageNum"
        @current-change="getCheckList()"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getScenic, getTicketTypeList } from '@/api/ticketType'
import { apiChannel, apiChannelGroup, getDeviceList } from '@/api/checker'
import { apiCheckList } from '@/api/order'
import moment from 'moment'
import ReaderInput from '@/components/ReaderInput'

export default {
  name: 'checkQuery',
  components: {
    ReaderInput
  },
  computed: {
    salesList () {
      return this.$store.state.user.salesList
    },
    userScenic () {
      return this.$store.state.app.account.manager_scenic_matrix ? this.$store.state.app.account.manager_scenic_matrix : this.scenicList
    }
  },
  data () {
    return {
      form: {
        keyWords1: '', // 票种ID
        keyWords2: '', // 订单号
        keyWords3: '', // 票号
        keyWords4: '', // 游客姓名
        keyWords5: '', // 游客手机
        keyWords6: '', // 检票设备
        keyWords7: 'ticket',
        keyWords8: '', // 景点
        keyWords9: '', // 通道
        keyWords10: '', // 通道组
        keyWords11: '', // 检票类型
        keyWords12: '', // 检票类型
        keyWords13: '', // 身份证号码
        startDate: new Date(moment().startOf('day').valueOf()),
        endDate: new Date(moment().endOf('day').valueOf()),
        startDate1: new Date(moment().startOf('day').valueOf()),
        endDate1: new Date(moment().endOf('day').valueOf()),
        pageNum: 1,
        pageSize: 10
      },
      scenicList: [],
      channelGroupOri: [],
      channelGroupList: [],
      channelList: [],
      deviceList: [],
      ticketTypeList: [],
      orderTableData: [],
      ticketTableData: [],
      queryType: 'order', // 按订单还是按票
      total: 0,
      loading: false
    }
  },
  filters: {
    limitTimes (input) {
      return input === -1 ? '不限' : input
    }
  },
  created () {
    this.getScenicList()
    this.initChannelGroup()
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-检票记录-の查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getTicketTypeList().then(() => {
      this.getCheckList()
    })
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取景点列表
    getScenicList () {
      getScenic().then(res => {
        this.scenicList = res?.data.children.map(item => item.data)
      })
    },
    initChannelGroup () {
      apiChannelGroup('getCheckerChannelGroupList', { pageNum: 1, pageSize: 10000 }).then(res => {
        this.channelGroupOri = res.list
      })
    },
    // 根据景点获取通道组
    getChannelGroup (id) {
      if (id) {
        this.channelGroupList = this.channelGroupOri.filter(item => item.scenics.some(v => v.scenic_id === id))
      } else {
        this.channelGroupList = []
      }
      this.channelList = []
      this.deviceList = []
      this.form.keyWords10 = ''
      this.form.keyWords9 = ''
      this.form.keyWords6 = ''
    },
    getChannel (id) {
      if (id) {
        apiChannel('getCheckerChannelList', {
          keyWords1: '',
          keyWords2: '',
          keyWords3: id,
          pageNum: 1,
          pageSize: 1000
        }).then(res => {
          this.channelList = res.list
        })
      } else {
        this.channelList = []
      }
      this.deviceList = []
      this.form.keyWords9 = ''
      this.form.keyWords6 = ''
    },
    getDevice (id) {
      if (id) {
        getDeviceList({
          keyWords6: id,
          pageNum: 1,
          pageSize: 10000
        }).then(res => {
          this.deviceList = res.list
        })
      } else {
        this.deviceList = []
      }
      this.form.keyWords6 = ''
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    getCheckList (goFirst) {
      this.loading = true
      goFirst && (this.form.pageNum = 1)
      apiCheckList(this.form).then(res => {
        this.total = res.total
        this.queryType = this.form.keyWords7
        if (this.queryType === 'order') {
          this.orderTableData = res.list
        } else if (this.queryType === 'ticket') {
          this.ticketTableData = res.list
        }
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-检票记录-の查询条件', JSON.stringify(this.form)) // 下次记住
      })
    }
    /**
     * 如果 goFirst 为 true，那么这个条件语句会执行，并且会将 this.form.pageNum 的值设置为 1，也就是将当前页码设置为 1。
     * 假设这段代码的作用是用于实现分页功能，其中 goFirst 表示是否需要跳转到第一页，this.form.pageNum 表示当前页码。在某些情况下，可能需要在执行一些操作后，强制返回到第一页重新加载数据，那么可以设置 goFirst 参数为 true，使得页面先跳转到第一页，然后再执行其它操作。这样可以确保数据始终从第一页开始加载，避免了因为操作产生的历史遗留问题。
     * 总之，如果 goFirst 为 true，那么这段代码将会影响当前页码的值，使其变为 1。如果 goFirst 为 false，这段代码不会有任何影响，当前页码的值仍然保持原来的状态。
     */
  }
}
</script>

<style scoped>

</style>
