<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="keyWords3"
      >
        <el-select v-model="form.keyWords3">
          <el-option
            value=""
            label="全部"
          ></el-option>
          <el-option
            v-for="t in ticketTypeList"
            :key="t.id"
            :value="t.id"
            :label="t.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="票状态"
        prop="keyWords9"
      >
        <el-select
          v-model="form.keyWords9"
          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="keyWords4">
        <ReaderInput
          type="ID"
          v-model="form.keyWords4">
        </ReaderInput>
      </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="keyWords6"
      >
        <el-input v-model="form.keyWords6"></el-input>
      </el-form-item>

      <el-form-item
        label="改签类型"
        prop="keyWords10"
      >
        <el-select
          v-model="form.keyWords10"
          @change="handleTypeChange">
          <el-option
            value="PLAYDATE"
            label="日期">
          </el-option>
          <el-option
            value="BATCH"
            label="场次">
          </el-option>
        </el-select>
      </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>

      <template v-if="changeType==='PLAYDATE'">
        <el-form-item
          label="新日期"
          prop="keyWords7"
        >
          <el-date-picker
            type="date"
            value-format="yyyy-MM-dd"
            v-model="form.keyWords7"
            placeholder="请选择日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item
          label="原日期"
          prop="keyWords8"
        >
          <el-date-picker
            type="date"
            v-model="form.keyWords8"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </template>

      <template v-else>
        <el-form-item
          label="新场次名"
          prop="keyWords7"
        >
          <el-select v-model="form.keyWords7">
            <el-option
              v-for="item in batchList"
              :key="item.id"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="原场次名"
          prop="keyWords8"
        >
          <el-select v-model="form.keyWords8">
            <el-option
              v-for="item in batchList"
              :key="item.id"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
      </template>

      <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
        ref="table"
        :data="tableData"
        v-loading="loading"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form
              label-position="right"
              inline
              class="table-expand"
              label-width="120px"
            >
              <el-form-item
                v-if="props.row.guestName"
                label="预订人"
              >
                <span>{{ props.row.guestName }}</span>
              </el-form-item>
              <el-form-item
                v-if="props.row.guestPhone"
                label="预订人手机"
              >
                <span>{{ props.row.guestPhone }}</span>
              </el-form-item>
              <el-form-item
                v-if="props.row.guestIdentify"
                label="预订人证件"
              >
                <span>{{ props.row.guestIdentify }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column
          label="#"
          type="index"
          width="50"
        >
        </el-table-column>
        <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
          width="140"
          label="改签时间"
        >
          <template slot-scope="props">
            <span>{{ props.row.change_time | formatTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="改签类型"
        >
          <template slot-scope="props">
            <el-tag :type="props.row.change_type==='BATCH'?'warning':'primary'">
              {{ props.row.change_type | changeType }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="新值"
        >
          <template slot-scope="props">
            <span>{{ props.row.new_value }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="原值"
        >
          <template slot-scope="props">
            <span>{{ props.row.old_value }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="100px"
          prop="creator"
          label="售票员"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="票种"
        >
        </el-table-column>
        <el-table-column
          width="80"
          prop="price"
          label="票价"
        >
        </el-table-column>
        <el-table-column
          width="80"
          label="状态"
        >
          <template slot-scope="props">
            <span>{{ props.row.status | ticketStatus }}</span>
          </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 { apiOrder } from '@/api/order'
import { getTicketTypeList } from '@/api/ticketType'
import { ticketStatusDic } from '@/assets/staticData'
import ReaderInput from '@/components/ReaderInput'
import moment from 'moment'

export default {
  name: 'rescheduleQuery',
  components: {
    ReaderInput
  },
  computed: {
    batchList () {
      return this.$store.state.app.batchList
    }
  },
  data () {
    return {
      ticketStatusDic: ticketStatusDic,
      ticketTypeList: [],
      changeType: 'PLAYDATE',
      form: {
        keyWords1: '', // 订单号
        keyWords2: '', // 票号
        keyWords3: '', // 票种id
        keyWords4: '', // 游客证件
        keyWords5: '', // 游客手机号
        keyWords6: '', // 游客姓名
        keyWords7: '', // 新日期
        keyWords8: '', // 旧日期
        keyWords9: '', // 票状态
        keyWords10: 'PLAYDATE', // 票状态
        pageNum: 1,
        pageSize: 10,
        startDate: new Date(moment().startOf('day').valueOf()),
        endDate: new Date(moment().endOf('day').valueOf())
      },
      tableData: [],
      total: 0,
      loading: false
    }
  },
  filters: {
    changeType (value) {
      const strMap = {
        PLAYDATE: '游玩时间',
        BATCH: '场次'
      }
      return strMap[value]
    }
  },
  mounted () {
    const searchParams = JSON.parse(sessionStorage.getItem('统计表-改签记录-查询条件'))
    if (searchParams) {
    // 如果存在查询条件，则设置到组件的数据中
      this.form = searchParams // 记住上次搜索条件
    }
    this.getTicketTypeList()
    this.getList()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    handleTypeChange () {
      this.form.keyWords7 = ''
      this.form.keyWords8 = ''
    },
    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)
      apiOrder('getModifyOrderList', this.form).then(res => {
        this.total = res.total
        this.tableData = res.list
      }).finally(() => {
        this.loading = false
        sessionStorage.setItem('统计表-改签记录-查询条件', JSON.stringify(this.form)) // 下次记住
      })
    }
  }
}
</script>
