<template>
  <a-card size="small" :bordered="false" class="bulkWarehousingOrderList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="产品编码">
                <a-input id="bulkWarehousingOrderList-productCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="关联单号">
                <a-input id="bulkWarehousingOrderList-relationNo" v-model.trim="queryParam.relationNo" allowClear placeholder="请输入关联单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="bulkWarehousingOrderList-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm()" :disabled="disabled" id="bulkWarehousingOrderList-reset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button v-if="$hasPermissions('B_sparePartsNew')" id="bulkWarehousingOrderList-add" type="primary" class="button-error" @click="handleAdd">新增</a-button>
        <a-button
          v-if="$hasPermissions('B_sparePartsAudit')"
          id="bulkWarehousingOrderList-batchAudit"
          type="primary"
          class="button-warning"
          :loading="loading"
          @click="handleBatchAudit"
          style="margin: 0 15px;">批量审核</a-button>
        <span style="margin-left: 8px">
          <template v-if="hasSelected">{{ `已选 ${selectedRowKeys.length} 项` }}</template>
        </span>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :row-selection="rowSelection"
        :rowKey="(record) => record.sparePartsSn"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1960, y: tableHeight }"
        bordered>
        <!-- 入库单号 -->
        <template slot="sparePartsNo" slot-scope="text, record">
          <span :class="$hasPermissions('B_sparePartsDetail')?'active':'common'" @click="handleDetail(record)">{{ record.sparePartsNo }}</span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            v-if="record.state == 'WAIT_AUDIT' && $hasPermissions('B_sparePartsEdit')"
            @click="handleEdit(record)"
            class="button-info"
            id="bulkWarehousingOrderList-edit-btn">编辑</a-button>
          <a-button
            size="small"
            type="link"
            v-if="record.state == 'WAIT_AUDIT' && $hasPermissions('B_sparePartsDel')"
            @click="handleDel(record)"
            class="button-error"
            id="bulkWarehousingOrderList-del-btn">删除</a-button>
          <span v-if="record.state != 'WAIT_AUDIT'||(!$hasPermissions('B_sparePartsEdit') && !$hasPermissions('B_sparePartsDel'))">--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 选择基本信息弹框 -->
    <basic-info-modal :openModal="openModal" @ok="handleOk" @close="openModal=false" />
    <sparePartsDetailModal :openModal="openDetailModal" :itemSn="itemSn" @close="closeDetailModal" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import basicInfoModal from './basicInfoModal.vue'
import sparePartsDetailModal from './detailModal.vue'
import { sparePartsList, sparePartsDelete, sparePartsAudit } from '@/api/spareParts'
export default {
  components: { STable, VSelect, basicInfoModal, sparePartsDetailModal },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      openModal: false, // 基本信息弹框是否显示
      tableHeight: 0,
      // 查询参数
      queryParam: {
        productCode: '',
        relationNo: ''
      },
      // 表头
      columns: [
        { title: '创建时间', dataIndex: 'createDate', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库单号', scopedSlots: { customRender: 'sparePartsNo' }, width: 220, align: 'center' },
        { title: '商户名称', dataIndex: 'supplierName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '入库数量', dataIndex: 'productTotalQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库成本', dataIndex: 'productTotalCost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库类型', dataIndex: 'sparePartsTypeDictValue', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '关联单号', dataIndex: 'relationNo', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '金蝶单号', dataIndex: 'kingdeeNo', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'stateDictValue', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'remark', width: 200, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 130, align: 'center', fixed: 'right' }
      ],
      selectedRowKeys: [], // Check here to configure the default column
      selectedRows: [],
      loading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 285
        }
        return sparePartsList(Object.assign(parameter, this.queryParam)).then(res => {
          const 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
          return data
        })
      },
      openDetailModal: false,
      itemSn: null
    }
  },
  computed: {
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    rowSelection () {
      if (this.$hasPermissions('B_sparePartsAudit')) {
        return {
			  selectedRowKeys: this.selectedRowKeys,
			  onChange: (selectedRowKeys, selectedRows) => {
			    console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
			    this.onSelectChange(selectedRowKeys)
			  },
			  getCheckboxProps: record => ({
			    props: {
			      disabled: record.state != 'WAIT_AUDIT'
			    }
			  })
        }
      } else {
        return null
      }
    }
  },
  methods: {
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    //  新增
    handleAdd () {
      this.openModal = true
    },
    //  基本信息  保存
    handleOk (row) {
      this.$router.push({ path: `/purchasingManagement/bulkWarehousingOrder/add/${row.id}/${row.sparePartsSn}` })
    },
    //  编辑
    handleEdit (row) {
      this.$router.push({ path: `/purchasingManagement/bulkWarehousingOrder/edit/${row.id}/${row.sparePartsSn}` })
    },
    //  详情
    handleDetail (row) {
      if (!this.$hasPermissions('B_sparePartsDetail')) { return }
      this.itemSn = row.sparePartsSn
      this.openDetailModal = true
    },
    // 关闭弹框
    closeDetailModal () {
      this.itemSn = null
      this.openDetailModal = false
    },
    //  删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除后不可恢复，确定要进行删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          sparePartsDelete({ sn: row.sparePartsSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.productCode = ''
      this.queryParam.relationNo = ''
      this.$refs.table.refresh(true)
    },
    // 批量审核
    handleBatchAudit () {
      const _this = this
      if (_this.selectedRowKeys.length < 1) {
        _this.$message.warning('请在列表勾选后再进行批量操作！')
        return
      }
      this.$confirm({
        title: '提示',
        content: '确认要批量审核吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          sparePartsAudit(_this.selectedRowKeys).then(res => {
            if (res.status == 200) {
              _this.selectedRowKeys = []
              _this.selectedRows = []
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.openModal = false
      vm.openDetailModal = false
      vm.itemSn = null
    })
  }
}
</script>
<style lang="less">
  .bulkWarehousingOrderList-wrap{
    .active{
      color: #ed1c24;
      cursor: pointer;
    }
    .common{
      color: rgba(0, 0, 0, 0.65);
    }
  }
</style>
