<template>
  <el-dialog title="选择盲票" :visible.sync="selectShow" min-width="800px" :before-close="close">
    <el-form :model="queryParams" ref="queryForm" label-width="80px" size="small">
      <el-row :gutter="10">
        <el-col :span="7">
          <el-form-item label="盲票名称">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入盲票名称"
              clearable
              @change="queryParams.pageNum = 1;getList()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="盲票类型">
            <el-select v-model="queryParams.type" placeholder="请选择盲票类型" clearable @change="queryParams.pageNum = 1;getList()">
              <el-option label="全部" value="" />
              <el-option label="线上票" value="online" />
              <el-option label="线下票" value="offline" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="上架状态">
            <el-select v-model="queryParams.status" placeholder="请选择上架状态" clearable @change="queryParams.pageNum = 1;getList()">
              <el-option label="全部" value="" />
              <!-- <el-option label="待上架" value="init" /> -->
              <el-option label="已上架" value="on" />
              <el-option label="已下架" value="off" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label-width="0">
            <el-button type="primary" icon="el-icon-search" @click="queryParams.pageNum = 1;getList()" >搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="tableData" row-key="boxId" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" :reserve-selection="true" />
      <el-table-column label="盲票ID" prop="boxId" min-width="90" />
      <el-table-column label="盲票图片" prop="picUrl" align="center" width="80">
        <template slot-scope="{row}">
          <a target="_black" v-if="row.picUrl && row.picUrl.split(',').length > 0" :href="`${IMG_URL + row.picUrl}`"><img :src="`${IMG_URL + row.picUrl.split(',')[0]}`" style="max-height: 37px;max-width: 54px"></a>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="盲票名称" prop="title" />
      <el-table-column label="盲票面值" prop="facePrice" >
        <template slot-scope="{row}">
          ¥{{$numberFormat(row.facePrice)}}
        </template>
      </el-table-column>
      <el-table-column label="盲票类型" prop="type">
        <template slot-scope="{row}">
          {{ JSON.parse(row.type).desc }}
        </template>
      </el-table-column>
      <el-table-column label="盲票状态" prop="status">
        <template slot-scope="{row}">
          <el-tag :type="JSON.parse(row.status).value === 'on' ? 'success' : 'info'">{{ JSON.parse(row.status).desc }}</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList()" />
    <span slot="footer">
      <el-button type="info" plain @click="close">取消</el-button>
      <el-button type="primary" @click="update">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getTicketList } from "@/api/business/ticket"
import { publicFileGetUrl } from "@/api/common"
export default {
  name: "SelectTicket",
  props: {
    value: {
      type: Array,
      default: []
    },
    selectShow: {
      type: Boolean,
      default: false,
    },
    excludeBoxIds:{
      type: Array,
      default: []
    },
  },
  data() {
    return {
      IMG_URL: publicFileGetUrl,
      loading: false,
      tableData: [],
      queryParams: {},
      total: 0,
      selection: []
    }
  },
  created() {
    this.getTicketList();
  },
  methods: {
    getTicketList() {
      this.getList(true)
      this.selection = this.value
      // this.$refs.table.toggleRowSelection(this.selection)
    },
    getList(reset) {
      if (this.loading) {
        return
      }
      this.loading = true
      if (reset) {
        this.queryParams = { pageNum: 1, pageSize: 10 }
      }
      if(this.excludeBoxIds){
        this.queryParams.excludeBoxIds = this.excludeBoxIds
      }

      getTicketList('pageNum='+this.queryParams.pageNum + '&pageSize='+this.queryParams.pageSize+'&', this.queryParams).then(res => {
        this.loading = false
        if (res.code === 0) {
          this.tableData = res.rows

          this.total = res.total
        }
      }).catch(() => {
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.selection = val
    },
    update() {
      this.$emit('selectTicket', this.selection);
      this.close()
    },
    close() {
      this.$emit("close");
    },
  }
}
</script>
