<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
    >
      <div class="block-title">
        查询条件
      </div>
      <!-- <el-form-item
        label="身份证号"
        prop="idcard">
        <ReaderInput v-model="form.idcard"></ReaderInput>
      </el-form-item> -->
      <el-form-item
        label="入园开始时间"
        prop="checkTimeBegin"
      >
        <el-date-picker
          v-model="form.checkTimeBegin"
          type="datetime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="入园结束时间"
        prop="checkTimeEnd"
      >
        <el-date-picker
          v-model="form.checkTimeEnd"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="检票闸机"
        prop="checkerId">
        <el-select
          v-model="form.checkerId"
          clearable
          filterable>
          <el-option
            value=""
            label="全部"></el-option>
          <el-option
            v-for="item in deviceList"
            :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="getFreeTicketList(true)"
        >
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="reportExport">
          导出
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        统计信息
      </div> -->
      <el-table
        stripe
        border
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="checkerName"
          label="检票设备"
        >
        </el-table-column>
        <el-table-column
          label="检票次数"
          prop="checkNums"
        >
        </el-table-column>
        <!-- <el-table-column
          label="操作"
          width="120"
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="deleteRecord(scope.row.id)">
              删除
            </el-link>
          </template>
        </el-table-column> -->
      </el-table>
    </div>
  </div>
</template>

<script>
import { getFreeTicketStatistics } from '@/api/order'
import { orderStatusDic, channelList } from '@/assets/staticData'
import { getPayStatus } from '@/utils/index'
import { getDeviceList } from '@/api/checker'
// import ReaderInput from '@/components/ReaderInput'
import moment from 'moment'


export default {
  name: 'freeQuery',
  computed: {
    salesList () {
      return this.$store.state.user.salesList
    },
    saleChannel () {
      return this.$store.state.app.saleChannel
    },
    payChannelOptions () {
      return this.$store.getters.payChannelOptions
    },
    ticketTypeList () {
      return this.$store.state.app.ticketTypeList
    },
    scenicList () {
      return this.$store.state.app.scenicList
    }
  },
  components: {
    // ReaderInput
  },
  data () {
    return {
      channelList,
      orderStatusDic,
      form: {
        idcard: '', // 身份证
        checkTimeBegin: new Date(moment().startOf('day').valueOf()),
        checkTimeEnd: new Date(moment().endOf('day').valueOf()),
        checkerId: '', // 检票闸机
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      loading: false,
      deviceList: []
    }
  },
  created () {
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-免票记录-查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getFreeTicketList()
    this.getDeviceList()
  },
  methods: {
    getPayStatus,
    reset () {
      this.$refs.form.resetFields()
    },
    setTime () {
      if (this.form.checkTimeBegin) {
        this.form.checkTimeBegin = moment(this.form.checkTimeBegin).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.form.checkTimeBegin = ''
      }

      if (this.form.checkTimeEnd) {
        this.form.checkTimeEnd = moment(this.form.checkTimeEnd).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.form.checkTimeEnd = ''
      }
    },
    reportExport () {
      this.setTime()
      if (!this.form.ticketTypeIdList) {
        this.form.ticketTypeIdList = []
      }

      if (!this.form.otaSourceNameList) {
        this.form.otaSourceNameList = []
      }
      this.$confirm(`是否要导出明细数据`, '确认提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        this.handleExport({
          ...this.form,
          export: true,
          exportDetail: true
        })
      }).catch(() => {
        this.handleExport({
          ...this.form,
          export: true,
          exportDetail: false
        })
      })
    },
    handleExport (params) {
      getFreeTicketStatistics(params).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    },
    getFreeTicketList (goFirst) {
      this.loading = true
      goFirst && (this.form.pageNum = 1)
      if (this.form.checkTimeBegin) {
        this.form.checkTimeBegin = moment(this.form.checkTimeBegin).format('YYYY-MM-DD HH:mm:ss')
      }
      if (this.form.checkTimeEnd) {
        this.form.checkTimeEnd = moment(this.form.checkTimeEnd).format('YYYY-MM-DD HH:mm:ss')
      }
      getFreeTicketStatistics(this.form).then(res => {
        this.tableData = res.data || []
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-免票记录-查询条件', JSON.stringify(this.form)) // 下次记住
      })
    },
    getDeviceList () {
      getDeviceList({ pageSize: -1, pageNum: 1 }).then((res) => {
        this.deviceList = res?.data.records
      })
    }
  }
}
</script>

<style scoped>

</style>
