<template>
  <a-modal
    centered
    class="chooseShelf-modal"
    :footer="null"
    :maskClosable="false"
    :title="mtitle"
    v-model="isShow"
    @cancel="isShow = false"
    width="80%">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-tabs>
        <a-tab-pane key="1" tab="待补货产品" force-render>
          <!-- 搜索条件 -->
          <div class="table-page-search-wrapper">
            <a-form-model
              ref="ruleForm"
              class="form-model-con"
              layout="inline"
              :model="queryParam"
              @keyup.enter.native="$refs.table.refresh(true)" >
              <a-row :gutter="5">
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="产品编码">
                    <a-input id="chooseShelf-code" ref="searchProductCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="产品名称">
                    <a-input id="chooseShelf-name" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="产品分类">
                    <ProductType id="chooseShelf-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="库存情况">
                    <a-checkbox-group :options="options" @change="onStockChange" />
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="12" style="margin-bottom: 10px;">
                  <a-button type="primary" @click="$refs.table.refresh(true)" id="chooseShelf-refresh">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm" id="chooseShelf-reset">重置</a-button>
                </a-col>
              </a-row>
            </a-form-model>
          </div>
          <div class="chooseShelf-modal-table">
            <div class="table-operator">
              <div>
                <a-button type="primary" class="button-error">批量添加</a-button>
                <span v-if="selectTotal">已选{{ selectTotal }}项</span>
              </div>
              <div>
                <a-icon type="info-circle" />
                此界面仅显示货架上需要补货的产品
              </div>
            </div>
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="small"
              :rowKey="(record) => record.id"
              :row-selection="{columnWidth: 40}"
              @rowSelection="rowSelectionFun"
              :columns="columns"
              :data="loadData"
              :scroll="{ y: 450 }"
              :defaultLoadData="false"
              :showPagination="false"
              bordered>
            </s-table>
          </div>
        </a-tab-pane>
        <a-tab-pane key="2" tab="全部货架产品" force-render>
          <!-- 搜索条件 -->
          <div class="table-page-search-wrapper">
            <a-form-model
              ref="ruleForm1"
              class="form-model-con"
              layout="inline"
              :model="queryParam1"
              @keyup.enter.native="$refs.table1.refresh(true)" >
              <a-row :gutter="5">
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="产品编码">
                    <a-input id="chooseShelf-code1" v-model.trim="queryParam1.productCode" allowClear placeholder="请输入产品编码"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="产品名称">
                    <a-input id="chooseShelf-name1" v-model.trim="queryParam1.productName" allowClear placeholder="请输入产品名称"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="产品分类">
                    <ProductType id="chooseShelf-productType1" :isDealer="true" @change="changeProductType1" v-model="productType1"></ProductType>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="12" style="margin-bottom: 10px;">
                  <a-button type="primary" @click="$refs.table1.refresh(true)" id="chooseShelf-refresh">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm1" id="chooseShelf-reset">重置</a-button>
                </a-col>
              </a-row>
            </a-form-model>
          </div>
          <div class="chooseShelf-modal-table">
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table1"
              size="small"
              :rowKey="(record) => record.id"
              :columns="columns1"
              :data="loadData1"
              :scroll="{ y: 450 }"
              :defaultLoadData="false"
              :showPagination="false"
              bordered>
            </s-table>
          </div>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import ProductType from '../../common/productType.js'
import { stockOutDetailList } from '@/api/stockOut'
export default {
  name: 'OutinDetailModal',
  components: { STable, ProductType },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    },
    baseData: {
      type: Object,
      default: function () {
        return null
      }
    }
  },
  data () {
    return {
      spinning: false,
      advanced: false,
      isShow: this.openModal, //  是否打开弹框
      options: [
        { label: '库存充足', value: '1' },
        { label: '部分缺货', value: '2' },
        { label: '全部缺货', value: '3' }
      ],
      productType: [],
      queryParam: {
        productCode: '', //  产品编码
        productName: '', //  产品名称
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '' //  产品三级分类
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        return stockOutDetailList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
              data[i].totalCost = Number(data[i].putCost * data[i].outQty).toFixed(2)
            }
          }
          this.spinning = false
          return data
        })
      },
      rowSelectionInfo: null,
      // -------- 全部产品 --------
      productType1: [],
      queryParam1: {
        productCode: '', //  产品编码
        productName: '', //  产品名称
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '' //  产品三级分类
      },
      // 加载数据方法 必须为 Promise 对象
      loadData1: parameter => {
        this.spinning = true
        return stockOutDetailList(Object.assign(parameter, this.queryParam1)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
              data[i].totalCost = Number(data[i].putCost * data[i].outQty).toFixed(2)
            }
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  computed: {
    mtitle () {
      return '货架产品' + (this.baseData && this.baseData.shelfName ? this.baseData.shelfName : '')
    },
    selectTotal () {
      return this.rowSelectionInfo && this.rowSelectionInfo.selectedRows.length
    },
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '库存批次', dataIndex: 'stockBatchNo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库类型', dataIndex: 'putBizTypeDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '成本价', dataIndex: 'putCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '本次出库数量', dataIndex: 'outQty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        // { title: '成本小计', dataIndex: 'totalCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.$hasPermissions('M_ShowAllCost')) {
        arr.splice(5, 0, { title: '成本价', dataIndex: 'putCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
        arr.splice(7, 0, { title: '成本小计', dataIndex: 'totalCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
      }
      return arr
    },
    columns1 () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '库存批次', dataIndex: 'stockBatchNo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库类型', dataIndex: 'putBizTypeDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '成本价', dataIndex: 'putCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '本次出库数量', dataIndex: 'outQty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        // { title: '成本小计', dataIndex: 'totalCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.$hasPermissions('M_ShowAllCost')) {
        arr.splice(5, 0, { title: '成本价', dataIndex: 'putCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
        arr.splice(7, 0, { title: '成本小计', dataIndex: 'totalCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
      }
      return arr
    }
  },
  methods: {
    cansel () {
      this.isShow = false
    },
    // 表格选中项
    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] : ''
    },
    onStockChange (val) {
      console.log(val)
    },
    resetSearchForm () {
      this.queryParam.productCode = ''
      this.queryParam.productName = ''
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.productType = []
      this.$refs.ruleForm.resetFields()
      this.$refs.table.refresh(true)
    },
    // ---------- 全部产品----------
    //  产品分类  change
    changeProductType1 (val, opt) {
      this.queryParam1.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam1.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam1.productTypeSn3 = val[2] ? val[2] : ''
    },
    resetSearchForm1 () {
      this.queryParam1.productCode = ''
      this.queryParam1.productName = ''
      this.queryParam1.productTypeSn1 = ''
      this.queryParam1.productTypeSn2 = ''
      this.queryParam1.productTypeSn3 = ''
      this.productType1 = []
      this.$refs.ruleForm1.resetFields()
      this.$refs.table1.refresh(true)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.$nextTick(() => {
          this.$refs.table.refresh(true)
        })
      }
    }
  }
}
</script>

<style lang="less">
.chooseShelf-modal {
	.ant-modal-body {
		padding: 0;
    .chooseShelf-modal-table{
      padding: 0 20px 10px;
      .table-operator{
        padding:0 10px 10px 0;
        display:flex;
        justify-content: space-between;
        align-items: center;
      }
    }
    .table-page-search-wrapper{
      padding: 5px 20px 0;
    }
	}
}
</style>
