<template>
  <el-dialog
    title="选择订单"
    :visible.sync="visible"
    width="80%"
    @close="visible=false">
    <div class="form-wrap">
      <el-form
        class="search-box"
        ref="form"
        :model="form"
        :inline="true"
        label-width="100px">
        <div class="block-title">
          查询条件
        </div>
        <el-form-item
          class="is-required"
          label="手机号"
          prop="keyWords3">
          <el-input v-model="form.keyWords3"></el-input>
        </el-form-item>
        <el-form-item
          class="is-required"
          label="取票码"
          prop="keyWords15">
          <el-input
            v-model="form.keyWords15"
            clearable></el-input>
        </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="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="keyWords1">
          <el-input v-model="form.keyWords1"></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="keyWords8">
          <el-input v-model="form.keyWords8"></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="keyWords14">
          <el-select v-model="form.keyWords14">
            <el-option
              value=""
              label="全部"></el-option>
            <el-option
              :value="1"
              label="是"></el-option>
            <el-option
              :value="0"
              label="否"></el-option>
          </el-select>
        </el-form-item>
        <div class="btn-wrap">
          <el-button
            type="default"
            @click="reset">
            重置
          </el-button>
          <el-button
            type="primary"
            @click="getList">
            查询
          </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
            prop="otaOrderNo"
            label="OTA订单号">
          </el-table-column>
          <el-table-column
            prop="otaSourceName"
            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"
            prop="tickets.length"
            label="票数量">
          </el-table-column>
          <el-table-column
            width="100"
            prop="buyerName"
            label="预订人">
          </el-table-column>
          <el-table-column
            width="120"
            prop="buyerPhone"
            label="预订手机">
          </el-table-column>
          <el-table-column
            label="证件号码">
            <template slot-scope="scope">
              {{ scope.row.buyerIdentifyType | paperType }}
              &nbsp;
              {{ scope.row.buyerIdentify }}
            </template>
          </el-table-column>
          <el-table-column
            width="150"
            label="售票时间">
            <template slot-scope="scope">
              {{ scope.row.createTime | formatTime }}
            </template>
          </el-table-column>
          <el-table-column
            width="80"
            label="总价">
            <template slot-scope="scope">
              {{ scope.row.tickets.length * scope.row.tickets[0].price }}
            </template>
          </el-table-column>
          <el-table-column
            width="100"
            label="状态">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status | orderStatusColor">
                {{ scope.row.status | orderStatus }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            width="120"
            fixed="right"
            label="门票列表">
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click="$refs.ticketList.show(scope.row)">
                查看门票
              </el-link>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            width="100"
            label="操作">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.tickets.some(ticket => !ticket.isPrint)&&scope.row.tickets.length===1"
                type="primary"
                size="small"
                @click="handleSelect(scope.row)">
                选择
              </el-button>

              <span
                v-if="!scope.row.tickets.some(ticket => !ticket.isPrint)"
                style="color:red">
                已取票
              </span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-if="total > 10"
          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>
    <span slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="visible=false">确定</el-button>
    </span>

    <TicketList
      ref="ticketList"
      @selectTicket="selectTicket"></TicketList>
  </el-dialog>
</template>

<script>
import { getOrderListForPrint } from '@/api/printOnlineTicket'
import { getMemberType } from '@/api/member'
import ReaderInput from '@/components/ReaderInput'
import moment from 'moment'
import TicketList from './TicketList'

export default {
  props: {
    memberType: {
      type: Object,
      default: () => {}
    }
  },
  components: {
    TicketList,
    ReaderInput
  },
  data () {
    return {
      visible: false,
      memberList: [],
      form: {
        startDate: new Date(moment().startOf('day').valueOf()),
        endDate: new Date(moment().endOf('day').valueOf()),
        keyWords1: '', // 订单号
        keyWords2: '', // 游客姓名
        keyWords3: '', // 游客手机
        keyWords4: '', // 游客身份证
        keyWords5: '', // 状态
        keyWords6: '', // 团散
        keyWords7: '', // 票号
        keyWords8: '', // 是否已取票
        keyWords9: '', // 取票码
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      orderDialogVisible: false,
      ticketDialogVisible: false,
      currentItem: {},
      loading: false,
      dialogTicketType: {},
      dialogOrderInfo: {}
    }
  },
  created () {
    this.getMemberTypeList()
  },
  methods: {
    show () {
      this.visible = true

      this.$nextTick(() => {
        // this.reset()

        // this.form.keyWords11 = (targetTicketType && targetTicketType.id) || ''

        this.getList()
      })
    },
    // 获取会员类型
    getMemberTypeList () {
      this.memberList = []
      getMemberType({
        pageNum: 0,
        pageSize: -1,
        keyWords6: 1,
        keyWords8: 1
      }).then(res => {
        res.list.forEach(item => {
          item.valid = moment().isBetween(item.saleDateStart, item.saleDateEnd)
        })
        this.$nextTick(() => {
          this.memberList = res.list.filter(item => item.valid)
          // this.$refs.memberTable.setCurrentRow(this.memberList[0])
        })
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    getList () {
      this.loading = true
      getOrderListForPrint(this.form).then(res => {
        this.total = res.total
        this.tableData = res.list
      }).finally(() => {
        this.loading = false
      })
    },
    handleSelect (order) {
      // 查找线上会员票种
      const targetTicketType = this.memberList.find(i => i.id === (order.tickets[0] && order.tickets[0].memberTypeId))

      if (!targetTicketType) {
        return this.$message.error('未找到该订单办理的会员类型')
      }

      this.$emit('select', order)

      this.visible = false
    },
    selectTicket (obj) {
      const { ticketId, order } = obj

      const targetTicketType = this.memberList.find(i => i.id === (order.tickets[0] && order.tickets[0].memberTypeId))

      if (!targetTicketType) {
        return this.$message.error('未找到该订单办理的会员类型')
      }

      this.$emit('select', order)
      this.$emit('selectTicketId', ticketId)

      console.log(ticketId, order.tickets.find(v => v.id === ticketId).upgrade_id)
      const targetTicket = order.tickets.find(v => v.id === ticketId)
      if (targetTicket.upgrade_id) {
        this.$emit('upgradeId', targetTicket.upgrade_id)
      }

      this.visible = false
    }
  },

  computed: {
    ticketTypeList () {
      return this.$store.state.app.ticketTypeList
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog .el-form-item{
display: inline-block;
}
</style>
