<template>
  <div class="bulkWarehousingOrderEdit-wrap">
    <!-- 内容 -->
    <a-page-header :ghost="false" :backIcon="false" class="bulkWarehousingOrderEdit-cont" >
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="bulkWarehousingOrderEdit-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
      </template>
      <!-- 操作区，位于 title 行的行尾 -->
      <template slot="extra">
        <a-button key="2" id="bulkWarehousingOrderEdit-preview-btn">打印预览</a-button>
        <a-button key="1" type="primary" id="bulkWarehousingOrderEdit-print-btn">快速打印</a-button>
      </template>
    </a-page-header>
    <!-- 基础信息 -->
    <a-card size="small" :bordered="false" class="bulkWarehousingOrderEdit-cont">
      <a-collapse :activeKey="['1']">
        <a-collapse-panel key="1">
          <template slot="header">
            基础信息
          </template>
          <a-descriptions :column="3">
            <a-descriptions-item label="供应商">{{ (productTotal&&productTotal.supplierName) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="入库类型">{{ (productTotal&&productTotal.sparePartsTypeDictValue) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="入库单号">{{ (productTotal&&productTotal.sparePartsNo) || '--' }}</a-descriptions-item>
          </a-descriptions>
        </a-collapse-panel>
      </a-collapse>
    </a-card>
    <a-card size="small" :bordered="false" class="bulkWarehousingOrderEdit-cont">
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button id="bulkWarehousingOrderEdit-add" type="primary" class="button-error" @click="handleEdit()">新增产品</a-button>
      </div>
      <!-- 总计 -->
      <a-alert type="info" showIcon style="margin-bottom:15px">
        <div slot="message">入库数量 <strong>{{ (productTotal&&productTotal.totalQty) || 0 }}</strong> ，入库金额 <strong>{{ (productTotal&&productTotal.totalPrice) || 0 }}</strong></div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            @click="handleEdit(record)"
            class="button-info"
            id="bulkWarehousingOrderList-edit-btn">编辑</a-button>
          <a-button
            size="small"
            type="link"
            @click="handleDel(record)"
            class="button-error"
            id="bulkWarehousingOrderList-del-btn">删除</a-button>
        </template>
      </s-table>
    </a-card>
    <a-affix :offset-bottom="0">
      <div style="text-align: center;width: 100%;background-color: #fff;padding: 12px 0;box-shadow: 0 0 20px #dcdee2;">
        <a-button
          type="primary"
          id="bulkWarehousingOrderEdit-submit"
          size="large"
          class="button-primary"
          @click="handleBack"
          style="padding: 0 60px;">保存并返回</a-button>
      </div>
    </a-affix>
    <spare-parts-product-modal :openModal="openModal" :itemSn="itemSn" :nowData="productTotal" @ok="$refs.table.refresh(true)" @close="openModal=false" />
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { getOperationalPrecision } from '@/libs/tools.js'
import sparePartsProductModal from './productModal.vue'
import { sparePartsDetailList, sparePartsPageCount, sparePartsDeleteDetail } from '@/api/spareParts'
export default {
  components: { STable, VSelect, sparePartsProductModal },
  data () {
    return {
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位', dataIndex: 'unit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库数量', dataIndex: 'productQty', width: 100, align: 'center' },
        { title: '入库单价', dataIndex: 'productCost', width: 100, align: 'center' },
        { title: '小计', dataIndex: 'subtotal', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 120, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return sparePartsDetailList(Object.assign(parameter, { sn: this.$route.params.sn })).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
            // 小计  由于数据库内小数位数为4位，页面则需显示2位。因此会做小数运算精度处理
            data.list[i].subtotal = getOperationalPrecision(data.list[i].productCost, data.list[i].productQty)
          }
          this.getDetailCount(Object.assign(parameter, { sn: this.$route.params.sn }))
          return data
        })
      },
      openModal: false, // 选择基本信息弹框是否显示
      basicInfoData: null, //  基本信息
      productTotal: null, //  合计
      itemSn: ''
    }
  },
  methods: {
    //  添加/编辑
    handleEdit (row) {
      this.itemSn = (row && row.sparePartsDetailSn) ? row.sparePartsDetailSn : null
      this.openModal = true
    },
    //  删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除后不可恢复，确定要进行删除吗？',
        centered: true,
        onOk () {
          sparePartsDeleteDetail({ sn: row.sparePartsDetailSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/purchasingManagement/bulkWarehousingOrder/list' })
    },
    // 合计
    getDetailCount (params) {
      sparePartsPageCount(params).then(res => {
        if (res.status == 200) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.openModal = false
    })
  }
}
</script>

<style lang="less">
  .bulkWarehousingOrderEdit-wrap{
    .bulkWarehousingOrderEdit-cont{
      margin-bottom: 10px;
    }
  }
</style>
