<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="供应商">{{ (basicInfoData&&basicInfoData.supplierName) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="入库类型">{{ (basicInfoData&&basicInfoData.sparePartsTypeDictValue) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="入库仓库">{{ (basicInfoData&&basicInfoData.warehouseName) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="入库单号">{{ (basicInfoData&&basicInfoData.sparePartsNo) || '--' }}</a-descriptions-item>
          </a-descriptions>
        </a-collapse-panel>
      </a-collapse>
      <!-- 总计 -->
      <a-alert type="info" style="margin:10px 0">
        <div slot="message">
          入库数量 <strong>{{ (productTotal&&(productTotal.productTotalQty || productTotal.productTotalQty==0)) ? productTotal.productTotalQty : '--' }}</strong> ，
          <span v-if="$hasPermissions('B_sparePartsDetail_costPrice')">入库金额 <strong>{{ (productTotal&&(productTotal.productTotalCost || productTotal.productTotalCost==0)) ? toThousands(productTotal.productTotalCost) : '--' }}</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 { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { getOperationalPrecision } from '@/libs/tools.js'
import { sparePartsDetailList, sparePartsPageCount, queryDetailCount } from '@/api/spareParts'
export default {
  name: 'BulkWarehousingOrderDetailModal',
  mixins: [commonMixin],
  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 }))
            this.getBasicInfoData()
            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, //  统计
      basicInfoData: null// 基础信息
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '15%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '产品名称', scopedSlots: { customRender: 'productName' }, width: '40%', align: 'left', 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 : '--') } }
      ]
      if (this.$hasPermissions('B_sparePartsDetail_costPrice')) { //  成本价权限
        arr.splice(5, 0, { title: '入库单价', dataIndex: 'productCost', width: '10%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
        arr.splice(6, 0, { title: '小计', dataIndex: 'subtotal', width: '12%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(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
        }
      })
    },
    // 获取基础信息数据
    getBasicInfoData () {
      queryDetailCount({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$store.state.app.curActionPermission = ''
      } else {
        this.$store.state.app.curActionPermission = 'B_sparePartsDetail'
      }
    },
    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>
