<template>
  <a-modal
    v-model="isShow"
    title="正价产品明细"
    centered
    class="view-normal-product-modal"
    :maskClosable="false"
    width="860px"
    :footer="null"
    @cancel="cancel"
  >
    <a-spin :spinning="spinning" tip="Loading..." v-if="buyGiftsInfo">
      <a-form-model
        id="normalProduct-modal-form"
        ref="ruleForm"
        :model="buyGiftsInfo"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <a-form-model-item label="活动简称">
          {{ buyGiftsInfo.description||'--' }}
        </a-form-model-item>
        <a-form-model-item label="促销规则">
          <span>{{ buyGiftsInfo.accrualFlag==='1'?'数量叠加；':'' }}</span>
          <div v-if="buyGiftsInfo.giveRuleList&&buyGiftsInfo.giveRuleList.length">
            <div v-for="item in buyGiftsInfo.giveRuleList" :key="item.scopeLevel">{{ buyGiftsInfo.giveRuleList.length>1?item.scopeLevel+'、' :'' }}{{ item.regularSameFlag==='1'?'同款':'不同款' }}产品购买满 {{ item.regularUnit==='YUAN'?toThousands(item.regularValue):item.regularValue }} {{ item.regularUnit==='YUAN'?'元':'个' }}正价产品，送 {{ item.promotionValue }} 个促销产品</div>
          </div>
          <span v-else>--</span>
        </a-form-model-item>
      </a-form-model>
      <!-- 筛选条件 -->
      <div class="table-page-search-wrapper">
        <a-form
          layout="inline"
          id="normalProduct-modal-search"
          @keyup.enter.native="$refs.table.refresh(true)"
        >
          <a-row :gutter="15">
            <a-col :md="8" :sm="24">
              <a-form-item label="产品编码">
                <a-input id="normalProduct-productCode" v-model.trim="queryParam.productCode" placeholder="请输入产品编码" allowClear />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="符合规则">
                <v-select
                  v-model="queryParam.regularRuleResult"
                  id="normalProduct-regularRuleResult"
                  code="FLAG"
                  placeholder="请选择是否符合规则"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="normalProduct-search">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="normalProduct-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :scroll="{ y: 200 }"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :showPagination="false"
        :defaultLoadData="false"
        bordered>
      </s-table>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { queryRegularProductShortfallList } from '@/api/salesNew'
export default {
  name: 'TotalProductDetailModal',
  mixins: [commonMixin],
  components: { STable, VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    buyGiftsInfo: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      isShow: this.openModal, // 打开弹窗
      spinning: false, // 页面加载动画
      disabled: false, // 阻止查询重置按钮多次点击事件
      // label 布局设置
      formItemLayout: {
        labelCol: { span: 2 },
        wrapperCol: { span: 20 }
      },
      // 查询参数
      queryParam: {
        productCode: '',
        regularRuleResult: undefined
      },
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        this.queryParam.salesPromoSn = this.buyGiftsInfo.salesPromoSn
        return queryRegularProductShortfallList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            for (var i = 0; i < data.length; i++) {
              data[i].no = i + 1
            }
          }
          this.disabled = false
          this.spinning = false
          return data
        })
      }
    }
  },
  computed: {
    columns () {
      const _this = this
      let newArr = null
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '16%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      // 买赠产品类型 ge按数量累计 yuan按金额累计
      if (_this.buyGiftsInfo.regularUnit === 'GE') {
        newArr = [
          { title: '销售数量', dataIndex: 'qty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '累计数量', dataIndex: 'borrowQty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '合计数量', dataIndex: 'totalQty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '差额', dataIndex: 'shortfall', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
        ]
      } else {
        newArr = [
          { title: '价格级别', dataIndex: 'priceLevelDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '售价', dataIndex: 'promotionPrice', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
          { title: '售价小计', dataIndex: 'promotionAmount', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
          { title: '累计售价', dataIndex: 'borrowPrice', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
          { title: '累计金额', dataIndex: 'borrowAmount', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
          { title: '合计金额', dataIndex: 'totalAmount', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
          { title: '差额', dataIndex: 'subtract', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
        ]
      }
      return arr.concat(newArr)
    }
  },
  methods: {
    cancel () {
      this.isShow = false
    },
    // 重置
    resetSearchForm () {
      this.queryParam.productCode = ''
      this.queryParam.regularRuleResult = undefined
      this.$refs.table.refresh(true)
    }
  },
  watch: {
  //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>
