<template>
  <div class="detail-container">
    <div v-show="!isShowPrint">
      <el-page-header @back="goBack" content="详情"></el-page-header>

      <div class="main-title">
        <div class="title">{{ orderTypeList[detailData.type - 1].label }}信息</div>
      </div>

      <div class="diy-table-1">
        <el-row>
          <el-col :span="8" class="item">
            <div class="label">发货申请单号</div>
            <div class="value">{{ detailData.id }}</div>
          </el-col>
          <el-col :span="8" class="item">
            <div class="label">申请日期</div>
            <div class="value">{{ detailData.createTime }}</div>
          </el-col>
          <el-col :span="8" class="item">
            <div class="label">单据状态</div>
            <div class="value">{{ detailData.examineStatus | statusFilter }}</div>
          </el-col>
          <el-col :span="8" class="item">
            <div class="label">经销商编号</div>
            <div class="value">{{ detailData.customerNumber }}</div>
          </el-col>
          <el-col :span="16" class="item">
            <div class="label">经销商名称</div>
            <div class="value">{{ detailData.customerName }}</div>
          </el-col>
          <el-col :span="24" class="item">
            <div class="label">仓库</div>
            <div class="value">{{ detailData.correspondName }}</div>
          </el-col>
          <!-- <el-col :span="16" class="item">
            <div class="label">文件编号</div>
            <div class="value">{{detailData.fileNo}}</div>
          </el-col> -->
          <el-col :span="24" class="item">
            <div class="label">备注</div>
            <div class="value">
              <!-- {{detailData.remark}} -->
              <el-input v-model="detailData.remark" placeholder="填写备注" clearable></el-input>
            </div>
          </el-col>

          <el-col :span="24" class="item file">
            <div class="label">附件</div>
            <div class="value">
              <div class="file-list" v-for="item in detailData.salesOrderPictures">
                <div class="file-item">
                  <el-image
                    v-if="checkFileType(item.fileUrl) == 'image'"
                    class="img"
                    :src="imageURL + item.fileUrl"
                    :preview-src-list="[imageURL + item.fileUrl]"
                  ></el-image>
                  <div v-else class="box2" @click="openLink(item.fileUrl)">
                    <img src="@/assets/common/word.png" v-if="checkFileType(item.fileUrl) == 'word'" />
                    <img src="@/assets/common/excel.png" v-if="checkFileType(item.fileUrl) == 'excel'" />
                    <img src="@/assets/common/ppt.png" v-if="checkFileType(item.fileUrl) == 'ppt'" />
                    <img src="@/assets/common/pdf.png" v-if="checkFileType(item.fileUrl) == 'pdf'" />
                    <div class="name ellipsis-3">{{ item.fileName }}</div>
                  </div>
                </div>
              </div>
            </div>
          </el-col>
          <el-col :span="6" class="item">
            <div class="label">制单人</div>
            <div class="value">{{ detailData.createBy }}</div>
          </el-col>
          <el-col :span="6" class="item">
            <div class="label">制单日期</div>
            <div class="value">{{ detailData.createTime }}</div>
          </el-col>
          <el-col :span="6" class="item">
            <div class="label">审核人</div>
            <div class="value">{{ detailData.approvalName }}</div>
          </el-col>
          <el-col :span="6" class="item">
            <div class="label">审核日期</div>
            <div class="value">{{ detailData.approvalTime }}</div>
          </el-col>
          <el-col :span="24" class="item">
            <div class="label">审批说明</div>
            <div class="value">{{ detailData.approvalRemark }}</div>
          </el-col>
        </el-row>
      </div>

      <div class="main-title">
        <div class="title">货品信息</div>
      </div>

      <div class="table" style="margin-top: 20px">
        <el-table
          :data="detailData.orders"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
          show-summary
          :summary-method="$getSummaries"
        >
          <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
          <el-table-column
            align="left"
            label="销售类型"
            prop="saleTypeName"
            min-width="100"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="销售订单号"
            prop="mainOrderId"
            min-width="120"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="物料编码"
            prop="materialCode"
            min-width="120"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="产品编码"
            prop="materialOldNumber"
            min-width="120"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="产品名称"
            prop="materialName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="规格型号"
            prop="specification"
            min-width="350"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="单位"
            prop="unit"
            min-width="100"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="文件编号"
            prop="fileNo"
            min-width="100"
            show-overflow-tooltip
          ></el-table-column>

          <el-table-column align="right" label="含税单价" prop="singlePayPrice" min-width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.singlePayPrice | numToFixed }}
            </template>
          </el-table-column>
          <el-table-column align="right" label="价税合计" prop="payAmount" min-width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.payAmount | numToFixed }}
            </template>
          </el-table-column>
          <el-table-column align="right" label="返利金额" prop="payRebateAmount" min-width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.payRebateAmount | numToFixed }}
            </template>
          </el-table-column>
          <el-table-column align="right" label="格力折扣" prop="totalDiscAmount" min-width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.totalDiscAmount | numToFixed }}
            </template>
          </el-table-column>
          <el-table-column
            align="right"
            label="销售政策名称"
            prop="policyTitle"
            min-width="100"
            show-overflow-tooltip
          ></el-table-column>

          <el-table-column
            align="right"
            label="申请数量"
            prop="invoiceNum"
            min-width="100"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="right"
            label="审批数量"
            prop="approvalNumber"
            min-width="100"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="right"
            label="已退数量"
            prop="retiredQty"
            min-width="100"
            show-overflow-tooltip
            v-if="detailData.type !== 2"
          ></el-table-column>
          <el-table-column
            align="left"
            label="表头备注"
            prop="headerRemark"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <!-- <el-table-column align="left" label="发货申请备注" prop="remark" min-width="160" show-overflow-tooltip></el-table-column> -->
          <el-table-column
            align="left"
            label="表体备注"
            prop="invoiceRemark"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="业务员"
            prop="serviceName"
            min-width="100"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="表头业务员"
            prop="k3ServiceName"
            min-width="100"
            show-overflow-tooltip
          ></el-table-column>
        </el-table>
      </div>

      <div v-if="isExamine">
        <div class="main-title">
          <div class="title">审批记录</div>
        </div>
        <div class="diy-table-1">
          <el-row :gutter="0">
            <el-col :span="12" class="item">
              <div class="label">审批人</div>
              <div class="value">{{ detailData.approvalName }}</div>
            </el-col>
            <el-col :span="12" class="item">
              <div class="label">审批结果</div>
              <div class="value">{{ detailData.examineStatus | statusFilter }}</div>
            </el-col>
            <el-col :span="24" class="item">
              <div class="label">审批说明</div>
              <div class="value">{{ detailData.approvalRemark }}</div>
            </el-col>
          </el-row>
        </div>
      </div>

      <div class="page-footer">
        <div class="footer">
          <el-button
            type="primary"
            icon="el-icon-printer"
            @click="toPrint"
            :disabled="detailData.examineStatus !== 'OK'"
            v-if="$checkBtnRole('print', $route.meta.roles)"
            >打 印</el-button
          >
          <el-popconfirm title="确定关闭吗？" @onConfirm="goBack" style="margin-left: 10px">
            <el-button slot="reference">返回列表</el-button>
          </el-popconfirm>
        </div>
      </div>
    </div>
         <print-preview ref="preView" @initPrint="handleInitPrint" @refreshList="handleRefreshList" :addPrint="addPrint"/>

    <!-- <CommonPrint :printId="printId" :printType="printType" v-if="isShowPrint" @backDetail="backDetail" /> -->
  </div>
</template>

<script>
import print from '@/mixin/print'
import printPreview from '@/views/supply/deliver/components/design/preview.vue'
import { getApplyDetail } from '@/api/supply/apply'
import { getFileUrl } from '@/api/common'
import CommonPrint from '@/views/supply/deliver/components/common_print'

export default {
  name: 'ApplyDetail',
  componentName: 'ApplyDetail',
  props: ['listItem'],
  components: {
    CommonPrint,
    printPreview
  },
  mixins: [print],
  filters: {
    statusFilter(val) {
      const statusList = [
        { label: '已保存', value: 'SAVE' },
        { label: '待审核', value: 'WAIT' },
        { label: '审核通过', value: 'OK' },
        // { label: '审核驳回', value: 'FAIL' },,
        { label: '已关闭', value: 'CLOSE' }
      ]
      let obj = statusList.find(o => o.value == val)
      return obj ? obj.label : ''
    }
  },
  data() {
    return {
      imageURL: this.$imageUrl,
      detailData: {},
      printType: 1,
      printId: '',
      isShowPrint: false,
      orderTypeList: [
        { label: '发货申请单', value: 1 },
        { label: '退货申请单', value: 2 },
        { label: '直调发货单', value: 3 }
      ]
    }
  },

  computed: {
    isExamine() {
      return this.detailData.examineStatus === 'OK' || this.detailData.examineStatus === 'FAIL'
    }
  },

  created() {
    this.getDetail()
  },
  mounted() {
    this.initPrint()
  },
  // activated(){
  //   this.initPrint()
  // },
  methods: {
    // 返回列表
    goBack() {
      this.$emit('backListFormDetail')
    },

    // 获取详情
    getDetail() {
      getApplyDetail({ id: this.listItem.id }).then(res => {
        if (res.data.orders) {
          res.data.orders.forEach(item => {
            item.sums1 = ['invoiceNum', 'approvalNumber', 'retiredQty']
            item.sums2 = ['singlePayPrice', 'payAmount', 'payRebateAmount', 'totalDiscAmount']
          })
        }
        this.detailData = res.data
      })
    },

    // 检查文件类型
    checkFileType(url) {
      if (!url) return ''
      const fileSuffix = url.substring(url.lastIndexOf('.') + 1)

      if (['jpg', 'jpeg', 'png'].includes(fileSuffix)) {
        return 'image'
      } else if (['doc', 'docx', 'dot', 'wps', 'wpt'].includes(fileSuffix)) {
        return 'word'
      } else if (['xls', 'xlsx', 'xlt', 'et', 'ett'].includes(fileSuffix)) {
        return 'excel'
      } else if (['ppt', 'pptx', 'dps', 'dpt', 'pot', 'pps'].includes(fileSuffix)) {
        return 'ppt'
      } else if (['pdf'].includes(fileSuffix)) {
        return 'pdf'
      } else {
        return ''
      }
    },

    // 打开链接
    openLink(url) {
      getFileUrl({ key: url }).then(res => {
        window.open(res.data)
      })
    },

    // 点击打印
    toPrint() {
      this.printId = this.listItem.id
      // this.isShowPrint = true;
      this.getDateil(this.listItem.id, 'getDeliverDetail').then(res => {
        this.$endLoading()
        this.$refs.preView.show(this.hiprintTemplate, this.outputData)
      })
    },

    backDetail() {
      this.printId = ''
      this.isShowPrint = false
    },
    handleRefreshList(){
      // this.recordSelected = []
      // this.$refs.pageRef.refreshList()

    },
    handleInitPrint(){
      this.$nextTick(() => {
      this.initPrint()
    })
    }
  }
}
</script>

<style scoped lang="scss">
.detail-container {
  width: 100%;
  height: 100%;
}
.main-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  height: 60px;
  border-bottom: 1px solid #dcdfe6;
  margin-bottom: 20px;
  .title {
    font-size: 16px;
    font-weight: 600;
    padding-left: 10px;
  }
}
</style>
