<template>
  <a-modal
    centered
    class="detail-modal"
    :footer="null"
    :maskClosable="false"
    title="详情"
    v-model="isShow"
    @cancel="isShow=false"
    width="70%">
    <a-card size="small" :bordered="false" class="detail-basics-info">
      <!-- 基础信息 -->
      <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.warehouseName) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="采购单号">{{ (basicInfoData&&basicInfoData.lockBizNo) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="客户名称">{{ (basicInfoData&&basicInfoData.lockBizDealerName) || '--' }}</a-descriptions-item>
          </a-descriptions>
        </a-collapse-panel>
      </a-collapse>
      <!-- 总计 -->
      <a-alert type="info" style="margin:10px 0">
        <div slot="message">
          采购数量 <strong>{{ (statisticsObj&&(statisticsObj.productTotalQty || statisticsObj.productTotalQty==0)) ? statisticsObj.productTotalQty : '--' }}</strong> ，
          <span v-if="$hasPermissions('B_purchaseReceiptDetail_costPrice')">采购金额 <strong>{{ (statisticsObj&&(statisticsObj.productTotalCost || statisticsObj.productTotalCost==0)) ? toThousands(statisticsObj.productTotalCost) : '--' }}</strong></span>
        </div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :defaultLoadData="false"
        :scroll="{ y: 300 }"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-tooltip placement="bottom" v-if="record.traceCodeQty*1!=record.productQty*1">
            <template slot="title">
              <span>唯一码数量与采购数量不一致</span>
            </template>
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_purchaseReceiptCode')"
              @click="handleCheck(record)"
              class="button-info"
              id="detail-edit-btn">
              查看
              <a-icon type="exclamation" style="color:#f30;" />
            </a-button>
          </a-tooltip>
          <a-button
            size="small"
            type="link"
            v-if="$hasPermissions('B_purchaseReceiptCode')&&record.traceCodeQty*1===record.productQty*1"
            @click="handleCheck(record)"
            class="button-info"
            id="detail-edit-btn">查看</a-button>
        </template>
      </s-table>
    </a-card>
    <div class="btn-cont">
      <a-button id="detail-basicInfo-modal-back" @click="isShow = false">关闭</a-button>
    </div>
    <!-- 唯一码列表  不分页 -->
    <uniqueCodeModal v-drag :openModal="openUniqueCodeModal" :itemData="itemUniqueCodeData" @close="closeUniqueCodeModal" />
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import uniqueCodeModal from './uniqueCodeModal.vue'

// 接口
import { sparePartsPageCount } from '@/api/spareParts'
import { purchaseDetailPageList, queryDetailCount } from '@/api/purchase'

export default {
  name: 'DetailModal',
  mixins: [commonMixin],
  components: { STable, VSelect, uniqueCodeModal },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {// sn
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      basicInfoData: null, //  基础信息数据
      statisticsObj: null, //  统计数据
      openUniqueCodeModal: false, // 轮胎唯一码弹窗
      itemUniqueCodeData: null,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const params = Object.assign(parameter, { sparePartsSn: this.itemSn })
        return purchaseDetailPageList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            // 获取基础信息数据
            this.getBasicsData()
            // 获取统计数据
            this.getDetailCount()
            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
        })
      }
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '30%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, 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: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        // { title: '采购金额', dataIndex: 'subtotal', width: '10%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') }
      ]
      if (this.$hasPermissions('B_purchaseReceiptDetail_costPrice')) { //  成本价权限
        arr.splice(6, 0, { title: '采购单价', dataIndex: 'productCost', width: '10%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
        arr.splice(7, 0, { title: '采购金额', dataIndex: 'subtotal', width: '10%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
      }
      if (this.basicInfoData && this.basicInfoData.state && this.basicInfoData.state === 'FINISH') {
        arr.push({ title: '唯一码', dataIndex: 'action', width: '10%', align: 'center', scopedSlots: { customRender: 'action' } })
      }
      return arr
    }
  },
  methods: {
    handleCheck (row) {
      this.itemUniqueCodeData = { purchaseNo: row.sparePartsNo, productCode: row.productCode, productSn: row.productSn }
      this.openUniqueCodeModal = true
    },
    closeUniqueCodeModal () {
      this.itemUniqueCodeData = null
      this.openUniqueCodeModal = false
    },
    // 获取基础信息
    getBasicsData () {
      queryDetailCount({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 统计
    getDetailCount (params) {
      sparePartsPageCount({ sparePartsSn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.statisticsObj = res.data
        } else {
          this.statisticsObj = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.$nextTick(() => {
          this.$refs.table.refresh()
        })
      }
    }
  }
}
</script>

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