<template>
  <a-modal
    centered
    class="outStock-modal"
    :footer="null"
    :maskClosable="false"
    title="采购缺货产品明细"
    v-model="isShow"
    @cancel="isShow=false"
    :width="900">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="outStock-con">
        <div>
          <div ref="tableSearch" class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchForm">
              <a-row :gutter="15">
                <a-col :md="8" :sm="24">
                  <a-form-item label="采购单号">
                    <a-input id="productInfoList-purchaseBillNo" v-model.trim="queryParam.purchaseBillNo" placeholder="请输入采购单号" allowClear />
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="产品编码">
                    <a-input id="productInfoList-code" v-model.trim="queryParam.dealerProduct.code" allowClear placeholder="请输入产品编码"/>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="产品名称">
                    <a-input id="productInfoList-name" v-model.trim="queryParam.dealerProduct.name" allowClear placeholder="请输入产品名称"/>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="产品分类">
                    <ProductType id="productInfoList-productType" placeholder="请选择产品分类" :isDealer="true" @change="changeProductType" v-model="productType"></ProductType>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="产品品牌">
                    <ProductBrand id="productInfoList-productBrandSn" placeholder="请选择产品品牌" v-model="queryParam.dealerProduct.productBrandSn"></ProductBrand>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="8" :sm="24" style="margin-bottom: 10px;">
                  <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>
          <p style="font-weight: bold;">当前已选：{{ rowSelectionInfo&&rowSelectionInfo.selectedRowKeys?rowSelectionInfo.selectedRowKeys.length:0 }}个</p>
          <!-- 列表 -->
          <s-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.id"
            :row-selection="{ columnWidth: 40 }"
            @rowSelection="rowSelectionFun"
            :data="loadData"
            :columns="columns"
            :scroll="{ y: 450 }"
            :defaultLoadData="false"
            bordered>
          </s-table>
        </div>
        <!-- 按钮 -->
        <div class="btn-con">
          <a-button
            id="outStock-cancel"
            size="large"
            class="button-cancel"
            @click="isShow=false"
            style="padding: 0 60px;margin-right: 15px;">取消</a-button>
          <a-button
            type="primary"
            id="outStock-save"
            size="large"
            class="button-primary"
            @click="handleSave"
            style="padding: 0 36px;">批量添加</a-button>
        </div>
      </div>
      </div></a-spin>
  </a-modal>
</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 []
      }
    }
  },
  data () {
    const _this = this
    return {
      isShow: _this.openModal, //  是否打开弹框
      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', customRender: function (text) { return text || '--' } },
        { 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
        this.queryParam.purchaseBillSnList = this.chooseData
        return outOfStockDetailList(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
        })
      },
      spinning: false,
      rowSelectionInfo: null,
      productType: [],
      queryParam: { //  查询条件
        purchaseBillSnList: undefined,
        purchaseBillNo: '',
        dealerProduct: {
          code: '', //  产品编码
          name: '', //  产品名称
          productBrandSn: undefined, //  产品品牌
          productTypeSn1: '', //  产品一级分类
          productTypeSn2: '', //  产品二级分类
          productTypeSn3: '' //  产品三级分类
        }
      }
    }
  },
  methods: {
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.dealerProduct.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam.dealerProduct.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam.dealerProduct.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')
        }
      })
    },
    searchForm () {
      this.$refs.table.refresh(true)
    },
    resetSearchForm () {
      this.queryParam.purchaseBillNo = ''
      const info = {
        productBrandSn: undefined,
        code: '', //  产品编码
        name: '', //  产品名称
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '' //  产品三级分类
      }
      this.productType = []
      Object.assign(this.queryParam.dealerProduct, info)
      this.$refs.table.refresh(true)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.table.clearSelected() // 清空表格选中项
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .outStock-modal{
    .outStock-con{
      .btn-con{
        text-align: center;
        margin: 30px 0 20px;
        .button-cancel{
          font-size: 12px;
        }
      }
    }
  }
</style>
