<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="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="idcard">
        <ReaderInput v-model="form.idcard"></ReaderInput>
      </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>
      </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="idcard"
          label="身份证"
        >
        </el-table-column>
        <el-table-column
          label="检票时间"
        >
          <template slot-scope="prop">
            {{ prop.row.checkTime | formatTime }}
          </template>
        </el-table-column>
        <el-table-column
          prop="checkerName"
          label="检票设备"
        >
        </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>
      <el-pagination
        background
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getFreeTicketList()"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { apiFreeList, deleteFreeRecord } 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()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getFreeTicketList()
    },
    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')
      }
      apiFreeList(this.form).then(res => {
        this.total = res.data.total
        this.tableData = res.data.records || []
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-免票记录-查询条件', JSON.stringify(this.form)) // 下次记住
      })
    },
    getDeviceList () {
      getDeviceList({ pageSize: -1, pageNum: 0 }).then((res) => {
        this.deviceList = res?.data.records
      })
    },
    deleteRecord (id) {
      this.$confirm('是否删除该记录？', '提示', {
        type: 'warning'
      }).then(() => {
        deleteFreeRecord(id).then(() => {
          this.$message.success('删除成功')
          this.getFreeTicketList()
        })
      }).catch(() => {
        // 取消删除
      })
    }
  }
}
</script>

<style scoped>

</style>
