<template>
  <div class="detail-container">
    <el-page-header @back="goBack" content="审批"></el-page-header>

    <div class="main-title">
      <div class="title">出库单信息</div>
    </div>
    <div class="diy-table-1">
      <el-row :gutter="0">
        <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.orderTime }}</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.orderNo }}</div>
        </el-col>
        <el-col :span="16" class="item">
          <div class="label">经销商</div>
          <div class="value">{{ detailData.customerName }}</div>
        </el-col>
        <el-col :span="8" 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">
            <el-input v-model="detailData.remark" placeholder="请输入备注"></el-input>
          </div>
        </el-col>
        <el-col :span="24" class="item file" style="width: 100%; height: auto">
          <div class="label">附件</div>
          <div class="value">
            <div class="file-list">
              <div class="file-item" v-for="item in detailData.salesOrderPictures">
                <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-row>
    </div>

    <div class="main-title">
      <div class="title">货品信息</div>
    </div>
    <div class="table">
      <el-table
        :data="detailData.saleOrderData"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
        stripe
        max-height="400"
        show-summary
        :summary-method="$getSummaries"
      >
        <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
        <el-table-column
          align="center"
          label="物料编码"
          prop="materialCode"
          min-width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          label="产品编码"
          prop="materialOldNumber"
          min-width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          label="产品名称"
          prop="materialName"
          min-width="160"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          label="规格型号"
          prop="specification"
          min-width="160"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          label="单位"
          prop="unit"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="right"
          label="数量"
          prop="refundableQty"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
        <!-- <el-table-column align="right" label="单价" prop="price" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.price | 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>
    </div>

    <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">{{ userName }}</div>
        </el-col>

        <el-col :span="12" class="item">
          <div class="label">审批日期</div>
          <div class="value">{{ getDate() }}</div>
        </el-col>
        <el-col :span="24" class="item">
          <div class="label">制单人</div>
          <div class="value">{{ detailData.createBy }}</div>
        </el-col>
        <el-col :span="24" class="item">
          <div class="label">审批说明</div>
          <div class="value">
            <el-input v-model="examineForm.remark" placeholder="请输入内容"></el-input>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="page-footer">
      <div class="footer">
        <el-button type="primary" @click="clickSubmitForm('OK')">审批通过</el-button>
        <el-button type="warning" @click="clickSubmitForm('FAIL')">审批驳回</el-button>
        <el-popconfirm title="确定关闭吗？" @onConfirm="goBack" style="margin-left: 10px">
          <el-button slot="reference">关 闭</el-button>
        </el-popconfirm>
      </div>
    </div>
  </div>
</template>

<script>
import { getDetail, examineData } from '@/api/supply/sales'
import { getFileUrl } from '@/api/common'

export default {
  name: 'SalesDetail',
  componentName: 'SalesDetail',
  props: ['listItem'],
  filters: {
    statusFilter(val) {
      const statusList = [
        { label: '已保存', value: 'SAVE' },
        { label: '待审核', value: 'WAIT' },
        { label: '审核通过', value: 'OK' }
        // { label: '审核驳回', value: 'FAIL' },,
      ]
      let obj = statusList.find(o => o.value == val)
      return obj ? obj.label : ''
    }
  },
  data() {
    return {
      userName: JSON.parse(localStorage.getItem('supply_user')).nickName,
      detailData: {},
      formLoading: false,
      srcList: [],
      examineForm: {
        remark: ''
      }
    }
  },

  created() {
    this.getDetail()
  },

  methods: {
    getDate() {
      var date = new Date()
      var seperator1 = '-'
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      var strDate = date.getDate()
      if (month >= 1 && month <= 9) {
        month = '0' + month
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = '0' + strDate
      }
      var currentdate = year + seperator1 + month + seperator1 + strDate
      return currentdate
    },
    // 打开链接
    openLink(url) {
      getFileUrl({ key: url }).then(res => {
        window.open(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 if (['zip', 'rar', 'gz', 'apk'].includes(fileSuffix)) {
        return 'file'
      } else {
        return ''
      }
    },

    // 返回列表
    goBack() {
      this.$emit('backListFormExamine')
    },

    // 获取详情
    getDetail() {
      getDetail({ id: this.listItem.id }).then(res => {
        if (res.data.saleOrderData) {
          res.data.saleOrderData.forEach(item => {
            item.sums1 = ['refundableQty']
            item.sums2 = ['payAmount', 'price']
          })
        }
        this.detailData = res.data
        this.srcList = [this.$imageUrl + this.detailData.fileUrl]
      })
    },

    // 提交审批
    clickSubmitForm(val) {
      this.$confirm('此操作将审批订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let params = {
            id: this.listItem.id,
            approvalRemark: this.examineForm.remark,
            examineStatus: val
          }
          this.formLoading = true
          examineData(params)
            .then(res => {
              this.$successMsg()
              this.goBack()
              this.$parent.getList()
            })
            .finally(res => {
              this.formLoading = false
            })
        })
        .catch(() => {})
    }
  }
}
</script>

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