<template>
  <a-card size="small" :bordered="false" class="jg-page-wrap couponDetails-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)" id="couponDetails-form">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="券名称/券副标题">
                <a-input id="couponDetails-queryWord" v-model.trim="queryParam.queryWord" allowClear placeholder="请输入券名称/券副标题"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="客户名称">
                <selectCust ref="custList" id="couponDetails-customerSn" :isValidateEnabled="true" @change="custChange" v-model="queryParam.customerSn"></selectCust>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="券状态">
                <v-select
                  id="couponDetails-ticketState"
                  v-model="queryParam.ticketState"
                  ref="sourceType"
                  code="TICKET_STATE"
                  placeholder="请选择优惠券使用状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="couponDetails-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="couponDetails-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 有效期 -->
        <template slot="dateBox" slot-scope="text, record">
          <span v-if="record.validStartDate&&record.validEndDate">
            {{ record.validStartDate }}-{{ record.validEndDate }}
          </span>
          <span v-else>--</span>
        </template>
        <!-- 优惠券状态 ticketState  已使用USED   未使用  UNUSED  已过期 DATED -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            v-if="record.ticketState==='UNUSED'&&$hasPermissions('B_checkTicketBtn')"
            type="link"
            class="button-success"
            @click="handleWriteoff(record)"
            :id="'couponDetails-cancel-btn'+record.id">核销</a-button>
          <span v-else>--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 核销 -->
    <writeOffModal :itemObj="writeOffObj" :openModal="openModal" @ok="$refs.table.refresh()" @close="closeModal"/>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import selectCust from '@/views/common/selectCust.vue'
import writeOffModal from './writeOffModal.vue'
// 接口
import { ticketQueryPage } from '@/api/ticket'

export default {
  name: 'CouponDetails',
  components: { STable, VSelect, writeOffModal, selectCust },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      openModal: false, // 核销弹窗
      disabled: false, //  查询、重置按钮是否可操作
      //  查询条件
      queryParam: {
        queryWord: undefined, // 券名称/券副标题
        customerSn: undefined, // 客户名称sn
        ticketState: undefined// 券状态
      },
      writeOffObj: null, // 核销弹窗回显数据
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '60px', align: 'center' },
        { title: '销售单号', dataIndex: 'usedBizNo', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'customerName', width: '20%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '货架名称', dataIndex: 'shelfName', width: '20%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '券名称', dataIndex: 'ruleName', width: '20%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '券副标题', dataIndex: 'ruleTitle', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '券金额', dataIndex: 'ticketValue', width: '10%', align: 'right', customRender: text => { return text || text == 0 ? this.toThousands(text, 2) : '--' } },
        { title: '促销名称', dataIndex: 'promoActiveName', width: '20%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '有效期', scopedSlots: { customRender: 'dateBox' }, width: '20%', align: 'center' },
        { title: '券状态', dataIndex: 'ticketStateDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '核销时间', dataIndex: 'usedBizDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '8%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        // 获取优惠券列表数据  有分页
        return ticketQueryPage(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 核销
    handleWriteoff (row) {
      this.writeOffObj = {
        customerName: row.customerName,
        ticketSn: row.ticketSn
      }
      this.openModal = true
    },
    // 关闭核销弹窗
    closeModal () {
      this.writeOffObj = null
      this.openModal = false
    },
    // 客户名称
    custChange (obj) {
      const _this = this
      if (obj && obj.key) {
        const cust = obj.row
        _this.queryParam.customerSn = cust.customerSn
      } else {
        _this.$refs.custList.resetForm()
      }
    },
    //  重置
    resetSearchForm () {
      this.queryParam.queryWord = undefined
      this.queryParam.customerSn = undefined
      this.queryParam.ticketState = undefined
      this.$refs.custList.resetForm()
      this.$refs.table.refresh(true)
    },
    // 初始化
    pageInit () {
      const _this = this
      _this.resetSearchForm()
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 203
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
    // 只刷新列表
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .couponDetails-wrap{
    .red{
      color: #ed1c24;
    }
    .green{
      color: #19be6b;
    }
    .font1{
     color:#39f;
    }
    .desc{
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }
  }
</style>
