<template>
  <a-card size="small" :bordered="false" class="satelliteWarehouseInventoryList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form-model
          id="satelliteWarehouseInventoryList-form"
          ref="ruleForm"
          :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" @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" @click="handleReplenishment">补货清单</a-button>
      </div>
      <!-- 列表 -->
      <a-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data-source="loadData"
        :pagination="paginationProps"
        :loading="loading"
        :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>
        </template>
      </a-table>
    </a-spin>
    <!-- 补货单 -->
    <satellite-warehouse-inventory-add-modal :openModal="openModal" :nowData="nowData" @close="closeModal" />
  </a-card>
</template>

<script>
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 {
  components: { STable, VSelect, satelliteWarehouseInventoryAddModal, custSatelliteList },
  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' }
      ],
      loadData: [],
      pageNo: 1, //  分页页码
      pageSize: 10, //  分页 每页多少条
      paginationProps: {
        showSizeChanger: true, //  是否可以改变 pageSize
        total: 0, //  分页总条数
        current: 1,
        onShowSizeChange: (current, pageSize) => this.changePageSize(current, pageSize),
        onChange: (current) => this.changePage(current)
      },
      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
    },
    // 列表
    getStockList (pageNo) {
      this.pageNo = pageNo || this.pageNo
      this.disabled = true
      if (this.tableHeight == 0) {
        this.tableHeight = window.innerHeight - 380
      }
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize
      }
      this.loading = true
      satelliteWHStockList(Object.assign(params, this.queryParam)).then(res => {
        this.loading = false
        if (res.status == 200) {
          const data = res.data
          this.paginationProps.total = Number(res.data.count) || 0
          this.paginationProps.current = data.pageNo
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.loadData = data.list
          this.disabled = false
        } else {
          this.paginationProps.total = 0
          this.paginationProps.current = 1
          this.loadData = []
        }
      })
    },
    //  分页  一页多少条change
    changePageSize (current, pageSize) {
      this.pageNo = current
      this.pageSize = pageSize
      this.getStockList()
    },
    //  分页 页码change
    changePage (current) {
      this.pageNo = current
      this.getStockList()
    },
    //  查询
    handleSearch () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.getStockList(1)
        } 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.paginationProps.total = 0
      this.paginationProps.current = 1
      this.loadData = []
      this.getStockList(1)
    },
    //  补货清单
    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
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {

    })
  }
}
</script>
