<template>
  <a-card size="small" :bordered="false" class="satelliteWarehouseInventoryList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form-model
          id="satelliteWarehouseInventoryList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam"
          :rules="rules"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          @keyup.enter.native="handleSearch" >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户名称" prop="customer.customerSn">
                <custSatelliteList ref="custSatelliteList" :verify="true" @change="custSatelliteChange"></custSatelliteList>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="配件名称">
                <a-input id="satelliteWarehouseInventoryList-name" v-model.trim="queryParam.dealerProduct.name" allowClear placeholder="请输入配件名称" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="配件编码">
                <a-input id="satelliteWarehouseInventoryList-queryWord" v-model.trim="queryParam.dealerProduct.queryWord" allowClear placeholder="请输入产品编码或产品原厂编码" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="handleSearch" :disabled="disabled" id="satelliteWarehouseInventoryList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="satelliteWarehouseInventoryList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button v-if="$hasPermissions('B_satelliteWarehouseInventoryDetail')" id="satelliteWarehouseInventoryList-replenishment" type="primary" class="button-error" @click="handleReplenishment">补货清单</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :defaultLoadData="false"
        :scroll="{ x: 1210, y: tableHeight }"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button size="small" v-if="$hasPermissions('B_satelliteWarehouseInventoryAdd')" type="link" @click="handleAdd(record)" id="satelliteWarehouseInventoryList-add-btn">加入补货单</a-button>
          <span v-if="!$hasPermissions('B_satelliteWarehouseInventoryAdd')">--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 补货单 -->
    <satellite-warehouse-inventory-add-modal :openModal="openModal" :nowData="nowData" @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import satelliteWarehouseInventoryAddModal from './addModal.vue'
import custSatelliteList from '@/views/common/custSatelliteList.vue'
import { satelliteWHStockList, satelliteWHSupplyInsert } from '@/api/satelliteWH'
export default {
  name: 'SatelliteWHStockList',
  components: { STable, VSelect, satelliteWarehouseInventoryAddModal, custSatelliteList },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      tableHeight: 0,
      queryParam: {
        customer: {
          customerSn: undefined
        },
        dealerProduct: {
          name: '',
          queryWord: ''
        }
      },
      rules: {
        'customer.customerSn': [{ required: true, message: '请选择客户名称', trigger: ['blur', 'change'] }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      loading: false, //  表格加载中
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '客户名称', dataIndex: 'customer.customerName', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '配件名称', dataIndex: 'dealerProduct.name', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'dealerProduct.code', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'dealerProduct.origCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '当前库存', dataIndex: 'currentStockQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 150, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return satelliteWHStockList(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
        })
      },
      openModal: false,
      nowData: null
    }
  },
  methods: {
    custSatelliteChange (val) {
      this.queryParam.customer.customerSn = val.key
      this.nowData = {
        customerName: '',
        customerSn: ''
      }
      this.nowData.customerName = val.label
      this.nowData.customerSn = val.key
    },
    //  查询
    handleSearch () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.table.refresh(true)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.customer.customerSn = undefined
      this.queryParam.dealerProduct.name = ''
      this.queryParam.dealerProduct.queryWord = ''
      this.$refs.custSatelliteList.resetForm()
      this.nowData = null
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    //  补货清单
    handleReplenishment () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.openModal = true
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  加入补货单
    handleAdd (row) {
      const params = {
        sn: row.customerSn,
        satelliteWarehouseSupply: {
          productSn: row.dealerProduct.productSn,
          productCode: row.dealerProduct.code,
          productQty: 1
        }
      }
      this.spinning = true
      satelliteWHSupplyInsert(params).then(res => {
        if (res.status == 200) {
          this.$message.success('添加成功')
          this.spinning = false
        } else {
          this.spinning = false
        }
      })
    },
    //  关闭弹框
    closeModal () {
      this.openModal = false
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
