<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"
          clearable></el-input>
      </el-form-item>
      <el-form-item
        label="票号"
        prop="keyWords7"
      >
        <QRReader v-model="form.keyWords7"></QRReader>
      </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">
        <el-input v-model="form.keyWords3"></el-input>
      </el-form-item>
      <el-form-item
        label="身份证号"
        prop="keyWords4">
        <ReaderInput v-model="form.keyWords4"></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="startDate1"
      >
        <el-date-picker
          v-model="form.startDate1"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="观演截止时间"
        prop="endDate1"
      >
        <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="keyWords5"
      >
        <el-select
          v-model="form.keyWords5"
          clearable
          placeholder="请选择"
        >
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in orderStatusDic"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="销售渠道"
        prop="keyWords8"
      >
        <el-select
          v-model="form.keyWords8"
          placeholder="请选择"
        >
          <el-option
            label="全部"
            :value="null"></el-option>
          <el-option
            v-for="(item,index) in saleChannel"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="团散"
        prop="keyWords6"
      >
        <el-select
          v-model="form.keyWords6"
          placeholder="请选择"
        >
          <el-option
            label="全部"
            value=""
          >
          </el-option>
          <el-option
            label="散客"
            :value="1"
          >
          </el-option>
          <el-option
            label="团体"
            :value="2"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="售票员"
        prop="keyWords9">
        <el-select v-model="form.keyWords9">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in salesList"
            :key="item.id"
            :label="item.nickName"
            :value="item.nickName">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="支付方式"
        prop="keyWords10">
        <el-select v-model="form.keyWords10">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in payChannelOptions"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="票种列表"
        prop="keyWords11"
      >
        <el-select
          v-model="form.keyWords11"
          placeholder="请选择">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            v-for="item in ticketTypeList"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="景点选择"
        prop="keyWords12"
      >
        <el-select
          v-model="form.keyWords12"
          placeholder="请选择">
          <el-option
            value=""
            label="全部">
          </el-option>
          <el-option
            v-for="item in scenicList"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getOrderList(true)"
        >
          搜索
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="$refs.qrcode.show('fastCheck')"
        >
          <svg-icon icon-class="qrcode" />

          快速检票
        </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="180"
          prop="orderNo"
          label="订单号"
        >
        </el-table-column>
        <el-table-column
          label="票种">
          <template slot-scope="scope">
            {{ scope.row.tickets[0].tname }}
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          label="类型">
          <template slot-scope="scope">
            {{ scope.row.groupIndividual | groupIndividual }}
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          label="销售渠道">
          <template slot-scope="scope">
            <ChannelTag :value="scope.row.saleChannelId"></ChannelTag>
          </template>
        </el-table-column>
        <el-table-column
          width="120"
          label="支付方式"
          prop="payChannel">
        </el-table-column>
        <!-- 项目名为'免费游台州'时隐藏售票员 -->
        <el-table-column
          width="120"
          label="售票员"
          v-if="scenicName && scenicName.indexOf('台州') < 0"
        >
          <template slot-scope="scope">
            {{ scope.row.is_otaorder ? scope.row.otaSourceName : scope.row.manager }}
          </template>
        </el-table-column>
        <el-table-column
          width="140"
          label="售票时间"
        >
          <template slot-scope="scope">
            {{ scope.row.createTime | formatTime }}
          </template>
        </el-table-column>

        <el-table-column
          width="80"
          prop="tickets.length"
          label="所含票数"
        >
        </el-table-column>
        <el-table-column
          width="80"
          label="人数"
        >
          <template slot-scope="scope">
            {{ reduceCheckNum(scope.row.tickets) }}
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          label="总价"
        >
          <template slot-scope="scope">
            {{ scope.row.price }}
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          label="状态"
          fixed="right"
        >
          <template slot-scope="scope">
            <OrderStatusTag :value="scope.row.status"></OrderStatusTag>
          </template>
        </el-table-column>
        <el-table-column
          label="支付状态"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-tag
              :type="getPayStatus(scope.row.pay_status,'type')"
              size="normal"
            >
              {{ getPayStatus(scope.row.pay_status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="门票操作"
          width="80"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="$refs.ticketList.show(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="getOrderList()"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <TicketList
      ref="ticketList"
      @updateList="getOrderList()"></TicketList>
    <QRCode
      ref="qrcode"
      @updateList="getOrderList()"
    ></QRCode>
  </div>
</template>

<script>
import { apiOrderList } from '@/api/order'
import { orderStatusDic } from '@/assets/staticData'
import { getPayStatus } from '@/utils/index'
import checkPermission from '@/utils/checkPermission'

import ReaderInput from '@/components/ReaderInput'
import TicketList from './TicketList'

import QRCode from '@/components/GlobalComponents/QRReader/QRCode'
import moment from 'moment'

export default {
  computed: {
    scenicName () {
      return this.$localStore.get('scenicName') || this.$store.state.user.scenicName
    },
    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,
    TicketList,
    QRCode
  },
  data () {
    return {
      orderStatusDic: orderStatusDic,
      ticketNo: '',
      form: {
        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()),
        keyWords1: '', // 订单号
        keyWords2: '', // 游客姓名
        keyWords3: '', // 游客手机
        keyWords4: '', // 游客身份证
        keyWords5: '', // 状态
        keyWords6: '', // 1散客 2团体
        keyWords7: '', // 票号
        keyWords8: null,
        keyWords9: '', // 售票员
        keyWords10: '',
        keyWords11: '',
        keyWords12: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      loading: false
    }
  },
  filters: {
    filterChannel (n) {
      switch (n) {
        case 1:
          return '线下'
        case 2:
          return '线上'
        case 3:
          return '自助机'
      }
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    getPayStatus,
    reset () {
      this.$refs.form.resetFields()
    },
    checkRefundVisible (item) {
      if (item.status !== 'CANCELED' && item.status !== 'COMPLETE' && item.status !== 'PART_USE') return true
      if (item.status === 'CANCELED') return true
      return (item.status === 'COMPLETE' || item.status === 'PART_USE') && checkPermission('ticket_sale:refund_checked')
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getOrderList()
    },
    getOrderList (goFirst) {
      this.loading = true
      if (goFirst) {
        this.form.pageNum = 1
      }
      apiOrderList(this.form).then(res => {
        this.total = res.total
        this.tableData = res.list
      }).finally(() => {
        this.loading = false
      })
    },
    reduceCheckNum (list) {
      return list.reduce((prev, item) => {
        return prev + item.checkNum
      }, 0)
    }
  }
}
</script>

<style scoped>

</style>
