<template>
  <a-drawer
    :zIndex="zIndex"
    placement="right"
    :visible="isShow"
    :width="width"
    :get-container="false"
    :wrap-style="{ position: 'absolute' }"
    @close="onCancel"
    wrapClassName="outStock-modal"
  >
    <div slot="title">
      <strong>采购缺货产品明细</strong>
      <span style="color: #ed1c24;;margin-left:10px;font-size: 12px;" v-if="purchaseTargetType&&(purchaseTargetType=='DEALER_UP'||purchaseTargetType=='DEALER_SUPPLIER')">注意：仅可添加本供应商有权限的产品</span>
      <span style="color: #ed1c24;;margin-left:10px;font-size: 12px;" v-else-if="purchaseTargetType&&purchaseTargetType=='SUPPLIER_SYS'">注意：仅可添加本供应商有经销权且总部已上线的产品</span>
    </div>
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="outStock-con jg-drawer-wrap">
        <div>
          <div ref="searchBox" class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchForm">
              <a-row type="flex" :gutter="12" justify="start">
                <a-col flex="auto">
                  <a-form-item label="采购单号">
                    <a-input id="productInfoList-purchaseBillNo" v-model.trim="purchaseBillNo" placeholder="请输入采购单号" allowClear />
                  </a-form-item>
                </a-col>
                <a-col flex="auto">
                  <a-form-item label="产品编码">
                    <a-input id="productInfoList-code" v-model.trim="queryParam.code" allowClear placeholder="请输入产品编码"/>
                  </a-form-item>
                </a-col>
                <a-col flex="auto">
                  <a-form-item label="产品名称">
                    <a-input id="productInfoList-name" v-model.trim="queryParam.name" allowClear placeholder="请输入产品名称"/>
                  </a-form-item>
                </a-col>
                <a-col flex="250px">
                  <a-form-item label="产品分类">
                    <ProductType id="productInfoList-productType" placeholder="请选择产品分类" :isDealer="true" @change="changeProductType" v-model="productType"></ProductType>
                  </a-form-item>
                </a-col>
                <a-col flex="250px">
                  <a-form-item label="产品品牌">
                    <ProductBrand id="productInfoList-productBrandSn" placeholder="请选择产品品牌" v-model="queryParam.productBrandSn"></ProductBrand>
                  </a-form-item>
                </a-col>
                <a-col flex="auto">
                  <a-button type="primary" @click="searchForm" :disabled="disabled" id="productInfoList-refresh">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productInfoList-reset">重置</a-button>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <div class="actions-buttons">
            <a-button
              type="primary"
              ghost
              id="outStock-save"
              @click="handleSave"
              style="padding: 0 20px;">批量添加</a-button>
            <span style="font-weight: bold;margin-left:15px;" v-if="rowSelectionInfo&&rowSelectionInfo.selectedRowKeys.length">已选：{{ rowSelectionInfo&&rowSelectionInfo.selectedRowKeys.length }}个</span>
          </div>
          <!-- 列表 -->
          <s-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.id"
            :row-selection="{ columnWidth: 40 ,getCheckboxProps:record=>({props:{disabled:(purchaseTargetType&&purchaseTargetType=='SUPPLIER_SYS'&&record.onlineFalg == 0)||record.productPowerFlag==0}})}"
            :scroll="{ y:tableHeight, x: 1000 }"
            @rowSelection="rowSelectionFun"
            :data="loadData"
            :defaultLoadData="false"
            :columns="columns"
            :pageSize="30"
            bordered>
            <!-- 产品编码 -->
            <template slot="productCode" slot-scope="text, record">
              {{ record.productCode }}
              <a-tag v-if="record.onlineFalg == 0">下架</a-tag>
              <a-tag color="red" v-if="record.productPowerFlag== 0">无权限</a-tag>
            </template>
          </s-table>
        </div>
      </div>
    </a-spin>
  </a-drawer>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { outOfStockDetailList } from '@/api/oos'
import { importOosDetail } from '@/api/purchaseDetail'
import ProductType from '../../common/productType.js'
import ProductBrand from '../../common/productBrand.js'
export default {
  name: 'OutStockModal',
  components: { STable, ProductType, ProductBrand },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsSn: {
      type: String,
      default: ''
    },
    chooseData: {
      type: Array,
      default: () => {
        return []
      }
    },
    width: {
      type: [String, Number],
      default: '70%'
    },
    zIndex: {
      type: Number,
      default: 100
    },
    // １向总部采购的提示语：注意：仅可添加本供应商有经销权且总部已上线的产品
    // ２向上级与省仓采购的提示语：注意：仅可添加本供应商有权限的产品
    purchaseTargetType: {
      type: String,
      default: null
    }
  },
  data () {
    const _this = this
    return {
      isShow: this.openModal, //  是否打开弹框
      tableHeight: 0,
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 20 }
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '8%', align: 'center' },
        { title: '采购单号', dataIndex: 'purchaseBillNo', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '18%', align: 'center', scopedSlots: { customRender: 'productCode' } },
        { title: '产品名称', dataIndex: 'productName', width: '30%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '缺货数量', dataIndex: 'qty', width: '15%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货金额', dataIndex: 'totalAmount', width: '15%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '单位', dataIndex: 'unit', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return outOfStockDetailList(Object.assign(parameter, { purchaseBillSnList: this.chooseData, newPurchaseBillSn: _this.paramsSn, purchaseBillNo: this.purchaseBillNo, dealerProduct: 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
        })
      },
      spinning: false,
      rowSelectionInfo: null,
      productType: [],
      queryParam: { //  查询条件
        code: '', //  产品编码
        name: '', //  产品名称
        productBrandSn: undefined, //  产品品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '' //  产品三级分类
      },
      purchaseBillNo: ''
    }
  },
  methods: {
    setTableH () {
      this.$nextTick(() => {
        const searchBoxH = this.$refs.searchBox.offsetHeight
        this.tableHeight = window.innerHeight - searchBoxH - 340
      })
    },
    //  产品分类  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] : ''
    },
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 保存
    handleSave () {
      const _this = this
      if (!this.rowSelectionInfo || (this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys.length < 1)) {
        this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      const arr = []
      _this.rowSelectionInfo.selectedRows.forEach(item => {
        const obj = {
          productSn: item.productSn,
          productCode: item.productCode,
          qty: item.qty
        }
        arr.push(obj)
      })
      importOosDetail({ purchaseBillSn: _this.paramsSn, arr }).then(res => {
        if (res.status == 200) {
          _this.$emit('ok')
        }
      })
    },
    onCancel () {
      this.$emit('close')
      this.$refs.table.clearSelected() // 清空表格选中项
    },
    searchForm () {
      this.$refs.table.refresh(true)
    },
    resetSearchForm () {
      this.queryParam = {
        productBrandSn: undefined,
        code: '', //  产品编码
        name: '', //  产品名称
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '' //  产品三级分类
      }
      this.purchaseBillNo = ''
      this.productType = []
      this.$refs.table.refresh(true)
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    },
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.onCancel()
      } else {
        setTimeout(() => {
          this.setTableH()
        }, 200)
        this.resetSearchForm()
      }
    }
  }
}
</script>

<style lang="less">
  .outStock-modal{
    .ant-drawer-header{
      padding: 13px 20px;
    }
    .ant-drawer-body{
      padding: 15px 20px;
      height: calc(100% - 50px);
    }
    .outStock-con{
      .actions-buttons{
        padding: 10px 0;
      }
    }

  }
</style>
