<template>
  <a-modal
    centered
    class="bulkWarehousingOrder-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="详情"
    v-model="isShow"
    @cancel="isShow=false"
    :width="960">
    <!-- <div style="padding: 0 12px;text-align: right;">
      <a-button id="bulkWarehousingOrderDetail-preview-btn" style="margin-right: 15px;">打印预览</a-button>
      <a-button type="primary" id="bulkWarehousingOrderDetail-print-btn">快速打印</a-button>
    </div> -->
    <!-- 基础信息 -->
    <a-card size="small" :bordered="false" class="bulkWarehousingOrderDetail-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="bulkWarehousingOrderDetail-cont">
      <!-- 总计 -->
      <a-alert type="info" style="margin-bottom:10px">
        <div slot="message">
          入库数量 <strong>{{ (productTotal&&(productTotal.totalQty || productTotal.totalQty==0)) ? productTotal.totalQty : '--' }}</strong> ，
          <span v-if="$hasPermissions('B_isShowCost')">入库金额 <strong>{{ (productTotal&&(productTotal.totalPrice || productTotal.totalPrice==0)) ? productTotal.totalPrice : '--' }}</strong></span>
        </div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        bordered>
        <!-- 产品名称 -->
        <template slot="productName" slot-scope="text, record">
          {{ record.productName }}
          <a-tag color="red" v-if="record.giftFlag==1">赠品</a-tag>
        </template>
      </s-table>
    </a-card>
    <div class="btn-cont">
      <a-button id="bulkWarehousingOrder-basicInfo-modal-back" @click="isShow = false" style="margin-left: 15px;">返回列表</a-button>
    </div>
  </a-modal>
</template>

<script>
import { STable, VSelect } from '@/components'
import { getOperationalPrecision } from '@/libs/tools.js'
import { sparePartsDetailList, sparePartsPageCount } from '@/api/spareParts'
export default {
  name: 'BulkWarehousingOrderDetailModal',
  components: { STable, VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return sparePartsDetailList(Object.assign(parameter, { sparePartsSn: this.itemSn })).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getDetailCount(Object.assign(parameter, { sparePartsSn: this.itemSn }))
            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 || 0, data.list[i].productQty)
            }
          }
          return data
        })
      },
      productTotal: null //  合计
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', scopedSlots: { customRender: 'productName' }, width: '40%', align: 'center', ellipsis: true },
        { title: '单位', dataIndex: 'unit', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库数量', dataIndex: 'productQty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '入库单价', dataIndex: 'productCost',width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '小计', dataIndex: 'subtotal', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.$hasPermissions('B_isShowCost')) { //  成本价权限
        arr.splice(5, 0, { title: '入库单价', dataIndex: 'productCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
      }
      return arr
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/purchasingManagement/bulkWarehousingOrder/list', query: { closeLastOldTab: true } })
    },
    // 合计
    getDetailCount (params) {
      sparePartsPageCount(params).then(res => {
        if (res.status == 200) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        const _this = this
        setTimeout(() => {
          _this.$refs.table.refresh(true)
        }, 200)
      }
    }
  }
}
</script>

<style lang="less">
  .bulkWarehousingOrder-basicInfo-modal{
    .ant-modal-body {
      padding: 10px 10px 20px;
    }
    .btn-cont {
      text-align: center;
      margin: 5px 0 10px;
    }
  }
</style>
