<template>
  <a-drawer
    title="新增"
    placement="right"
    :visible="isShow"
    width="80%"
    :get-container="false"
    :wrap-style="{ position: 'absolute' }"
    @close="isShow = false"
    wrapClassName="subsidyAdd-modal jg-page-wrap"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="searchBox" class="table-page-search-wrapper">
        <a-form-model
          ref="ruleForm"
          class="form-model-con"
          layout="inline">
          <a-row :gutter="5" type="flex" justify="center">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="产品编码">
                <a-input id="subsidyAdd-code" ref="searchProductCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="原厂编码">
                <a-input id="subsidyAdd-productOrigCode" ref="productOrigCode" v-model.trim="queryParam.productOrigCode" allowClear placeholder="请输入原厂编码"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="产品名称">
                <a-input id="subsidyAdd-name" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="产品分类">
                <productType id="subsidyAdd-productType" :isDealer="true" @change="changeProductType" v-model="productType"></productType>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="产品品牌">
                <productBrand id="subsidyAdd-productBrandSn" v-model="queryParam.productBrandSn"></productBrand>
              </a-form-model-item>
            </a-col>
            <a-col flex="auto" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" id="subsidyAdd-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" id="subsidyAdd-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="subsidyAdd-modal-table">
        <div class="table-operator">
          <span class="subsidyRequired">*</span>产品抵扣<a-input-number
            id="subsidyAdd-name"
            v-model.trim="subsidyAmount"
            style="width: 20%;margin:0 10px;"
            :min="0.01"
            :max="99999999"
            :precision="2"
            :step="1"
            placeholder="请输入"/>元
          <a-button id="subsidyAdd-handlePlAdd" style="margin:0 10px;" type="primary" @click="handlePlAdd">批量添加</a-button>
          <span style="margin-left: 10px;" v-if="selectTotal">已选{{ selectTotal }}项</span>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :style="{ height: 580+'px' }"
          :scroll="{ y: 530 }"
          :defaultLoadData="false"
          :row-selection="{ columnWidth: 40 }"
          @rowSelection="rowSelectionFun"
          bordered>
          <!-- 产品分类 -->
          <template slot="productType" slot-scope="text, record">
            <span v-if="record.productTypeName2 || record.productTypeName3">{{ record.productTypeName2 }} {{ record.productTypeName3 ? '>' : '' }} {{ record.productTypeName3 }}</span>
            <span v-else>--</span>
          </template>
          <!-- 包装数 -->
          <template slot="baozh" slot-scope="text, record">
            {{ record.packQty||'--' }}{{ record.packQty ? record.unit : '' }}/{{ record.packQtyUnit||'--' }}
          </template>
        </s-table>
      </div>
    </a-spin>
  </a-drawer>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
// 组件
import productType from '@/views/common/productType.js'
import productBrand from '@/views/common/productBrand.js'
// 接口
import { queryProductPageList } from '@/api/shopProductSubsidy'
export default {
  name: 'SubsidyAddModal',
  components: { STable, productType, productBrand },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    const _this = this
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      productType: [], // 产品分类
      category: [], // 商城类目val
      // 查询条件
      queryParam: {
        productName: '', // 产品名称
        productCode: '', // 产品编码
        productOrigCode: '', // 原厂编码
        productBrandSn: undefined, //  产品品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '' //  产品三级分类
      },
      subsidyAmount: undefined, // 产品抵扣
      rowSelectionInfo: null, // 已选产品
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '20%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '商城售价', dataIndex: 'price', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '终端价', dataIndex: 'terminalPrice', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '品牌', dataIndex: 'productBrandName', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品分类', width: '15%', align: 'center', scopedSlots: { customRender: 'productType' } },
        { title: '包装数', width: '10%', align: 'center', scopedSlots: { customRender: 'baozh' } },
        { title: '单位', dataIndex: 'unit', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const queryParams = Object.assign(parameter, this.queryParam)
        return queryProductPageList(queryParams).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
        })
      }
    }
  },
  computed: {
    // 已选数量
    selectTotal () {
      return this.rowSelectionInfo && this.rowSelectionInfo.selectedRows.length
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam.productTypeSn3 = val[2] ? val[2] : ''
    },
    // 批量添加
    handlePlAdd () {
      if (!this.selectTotal) {
        this.$message.info('请选择产品')
        return
      }
      if (!this.subsidyAmount) {
        this.$message.info('产品抵扣金额不能为空')
        return
      }
      const chooseList = JSON.parse(JSON.stringify(this.rowSelectionInfo.selectedRows))
      const newList = chooseList.map(item => {
        item.subsidyAmount = this.subsidyAmount
        delete item.id
        return item
      })
      this.$emit('plAdd', newList)
    },
    //  重置
    resetSearchForm () {
      this.queryParam.productName = ''
      this.queryParam.productCode = ''
      this.queryParam.productOrigCode = ''
      this.queryParam.productBrandSn = undefined
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.subsidyAmount = undefined
      this.productType = []
      this.category = []
      this.rowSelectionInfo = null
      this.$refs.table.clearSelected()
      this.$refs.table.refresh()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.resetSearchForm()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.subsidyAdd-modal {
	.ant-drawer-body {
		padding: 10px 20px;
    .subsidyAdd-modal-table{
      padding-top: 10px;
    }
	}
  .table-operator{
    .subsidyRequired{
      margin-right: 5px;
      color:#ED1c24;
    }
  }
}
</style>
