<template>
  <div>
    <div class="sty">
      <el-page-header content="申请退押" @back="goBack" />
    </div>
    <el-divider />
    <h3>工程项目信息</h3>
    <el-divider />
    <div class="diy-table-1">
      <el-form ref="form" :model="details">
        <el-row :gutter="0">
          <el-col :xs="12" :sm="8" :lg="8" class="item">
            <div class="label">工程登录编号</div>
            <div class="value">{{ details.refEnginRecordNo }}</div>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="8" class="item">
            <div class="label">项目类别</div>
            <div class="value">{{ details.refProjectName }}</div>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="8" class="item">
            <div class="label">工程编号</div>
            <div class="value">{{ details.refProjectNo }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="8" class="item">
            <div class="label">经销商名称</div>
            <div class="value">{{ details.customerName }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="8" class="item">
            <div class="label">经销商编码</div>
            <div class="value">{{ details.customerNumber }}</div>
          </el-col>

          <el-col :xs="24" :sm="24" :lg="8" class="item">
            <div class="label">工程登录类型</div>
            <div class="value">{{ details.refPromiseStatus }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="8" class="item">
            <div class="label">联系人</div>
            <div class="value">{{ details.refLinkman }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="8" class="item">
            <div class="label">固定电话</div>
            <div class="value">{{ details.refTel }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="24" class="item" v-if="!isCustomer">
            <div class="label">格力备注</div>
            <div class="value">
              <el-input v-model="details.geLiInerNote" placeholder="格力备注"></el-input>
            </div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="8" class="item">
            <div class="label">厂编号</div>
            <div class="value">
              {{ details.refFactoryNo }}
              <!-- <el-form-item prop="refFactoryNo">
                <el-input
                  v-model="details.refFactoryNo"
                  placeholder="厂编号"
                  size="small"
                />
              </el-form-item> -->
            </div>
          </el-col>

          <el-col :xs="24" :sm="24" :lg="16" class="item">
            <div class="label">移动电话</div>
            <div class="value">{{ details.refPhone }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="8" class="item">
            <div class="label">押金总额</div>
            <div class="value">{{ details.depositAmount }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="8" class="item">
            <div class="label">工程差价总额</div>
            <div class="value">{{ details.depositDiffAmount }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="16" class="item">
            <div class="label">使用单位</div>
            <div class="value">{{ details.refUseUnit }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="24" class="item">
            <div class="label">安装地址</div>
            <div class="value">{{ details.refInstallAddress }}</div>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="24" class="item">
            <div class="label">安装时间</div>
            <div class="value" style="padding: 0">
              <el-form-item prop="installDate">
                <el-date-picker
                  v-model="details.installDate"
                  type="datetime"
                  placeholder="安装时间"
                  default-time="00:00:00"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </div>
          </el-col>
          <!-- <el-col :xs="24" :sm="24" :lg="8" class="item">
            <div class="label">寄厂日期</div>
            <div class="value" style="padding: 0">
              <el-form-item prop="sendFactoryDate">
                <el-date-picker
                  v-model="details.sendFactoryDate"
                  type="datetime"
                  placeholder="寄厂日期"
                  default-time="00:00:00"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item>
            </div>
          </el-col> -->
        </el-row>
      </el-form>
    </div>
    <h3>货品信息</h3>
    <el-divider />
    <!-- 列表 -->
    <div class="mymain-container">
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="details.items"
          element-loading-text="Loading"
          border
          fit
          show-summary
          highlight-current-row
          stripe
          :summary-method="$getSummaries"
        >
          <el-table-column
            align="center"
            label="物料编码"
            prop="materialNumber"
            min-width="130"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="产品编码"
            prop="materialOldNumber"
            min-width="130"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="产品名称" prop="materialName" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="规格型号" prop="specification" min-width="300" show-overflow-tooltip />
          <el-table-column align="center" label="单价" prop="price" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="数量" prop="qty" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="金额" prop="totalAmount" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="发货数量" prop="hasSendQty" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="押金金额" prop="depositAmount" min-width="160" show-overflow-tooltip />
          <el-table-column align="center" label="上交资料" prop="dataQty" min-width="160" show-overflow-tooltip />
          <!-- <el-table-column
            align="center"
            label="收差金额"
            prop="diffAmount"
            min-width="160"
            show-overflow-tooltip
          /> -->
        </el-table>
      </div>
    </div>
    <h3>上传资料</h3>
    <el-divider />
    <!-- 筛选条件 -->
    <div>
      <el-form ref="form" :model="details" label-width="100px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="24">
            <el-form-item label="工程验收资料" prop="">
              <imageUpload
                :file-list="fileList"
                :multiple="true"
                :start-restricting="true"
                :restrict-filename="restrictFilename"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="24" v-if="details.dataList && details.dataList.length">
            <!-- 列表 -->
            <div class="mymain-container">
              <div class="table">
                <el-table
                  v-loading="listLoading"
                  :data="details.dataList"
                  element-loading-text="Loading"
                  border
                  fit
                  highlight-current-row
                  stripe
                >
                  <el-table-column
                    align="center"
                    label="资料描述"
                    prop="dataDescribe"
                    min-width="160"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    align="center"
                    label="原文件名"
                    prop="fileName"
                    min-width="160"
                    show-overflow-tooltip
                  />
                  <el-table-column align="center" label="缩略图" prop="fileUrl" min-width="160" show-overflow-tooltip>
                    <template slot-scope="scope">
                      <el-image
                        v-if="checkFileType(scope.row.fileUrl) == 'image'"
                        ref="img"
                        :src="imageURL + scope.row.fileUrl"
                        style="width: 120px; height: 120px"
                        fit="cover"
                        :preview-src-list="[imageURL + scope.row.fileUrl]"
                      />
                      <img
                        v-if="checkFileType(scope.row.fileUrl) == 'word'"
                        class="file"
                        src="@/assets/common/word.png"
                      />
                      <img
                        v-if="checkFileType(scope.row.fileUrl) == 'excel'"
                        class="file"
                        src="@/assets/common/excel.png"
                      />
                      <img
                        v-if="checkFileType(scope.row.fileUrl) == 'ppt'"
                        class="file"
                        src="@/assets/common/ppt.png"
                      />

                      <img
                        v-if="checkFileType(scope.row.fileUrl) == 'pdf'"
                        class="file"
                        style="cursor: pointer"
                        src="@/assets/common/pdf.png"
                        @click="openPdf(scope.row.fileUrl)"
                      />

                      <img
                        v-if="checkFileType(scope.row.fileUrl) == 'file'"
                        class="file aaa"
                        src="@/assets/common/zip.jpeg"
                      />
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    label="下载文件名称"
                    prop="fileName"
                    min-width="160"
                    show-overflow-tooltip
                  >
                    <template slot-scope="scope">
                      {{ '资料_' + scope.row.fileName }}
                    </template>
                  </el-table-column>
                  <el-table-column align="center" label="操作" min-width="160" show-overflow-tooltip>
                    <template slot-scope="scope">
                      <el-button
                        type="text"
                        class="textColor"
                        @click="downLoadFn(scope.row.fileUrl, scope.row.fileName)"
                        >下载</el-button
                      >
                      <el-popconfirm
                        style="margin-left: 10px"
                        title="这是一段内容确定删除吗？"
                        @confirm="delFile(scope.$index)"
                      >
                        <el-button type="text" slot="reference">删除</el-button>
                      </el-popconfirm>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="申请人" prop="submitName">
              <el-input v-model="details.submitName" readonly placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="申请日期" prop="submitTime">
              <el-date-picker
                v-model="details.submitTime"
                readonly
                class="dateStyle"
                type="datetime"
                placeholder="选择日期"
                default-time="00:00:00"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="18">
            <el-form-item label="申请退押" prop="applyNote">
              <el-input
                v-model="details.applyNote"
                type="textarea"
                placeholder="请输入退押说明"
                class="textareaStyle"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="18" v-if="details.checkBy && details.checkDate">
            <el-form-item
              label="验收说明
              "
              prop="checkNote"
            >
              <el-input
                disabled
                v-model="details.checkNote"
                type="textarea"
                placeholder="请输入验收说明
"
                class="textareaStyle"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="24">
            <el-form-item label="">
              <el-button type="primary" size="small" @click="handleSvse">保存</el-button>
              <el-button type="primary" size="small" @click="handleReset">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import { applyDeposit, geDetail } from '@/api/engin_deposit.js'

import imageUpload from '@/components/Common/image-upload.vue'
import { downloadFiles, downloadPdf } from '@/utils/util'

import { getFileUrl } from '@/api/common'

export default {
  components: {
    imageUpload
  },
  data() {
    return {
      isDis: true,
      listLoading: false,
      baseURL: '',
      fileList: [],
      details: {},
      restrictFilename: ['结算单', '合同', '明细表', '审核表', '收据', '发票', '照片', '相片']
    }
  },
  computed: {
    isCustomer() {
      return this.$store.getters.customerId && this.$store.getters.customerNumber
    }
  },
  created() {
    this.imageURL = this.$imageUrl
    geDetail({ id: this.$parent.depositManageId }).then(res => {
      let arr = []
      res.data.items.forEach(item => {
        if (Number(item.qty) > 0) {
          arr.push(item)
        }
        // item.number = (item.qty*100 - item.directTransferQty*100 - item.retiredQty*100) / 100;
        item.sums1 = ['hasSendQty', 'dataQty', 'qty', 'hasSendQty']
        item.sums2 = ['totalAmount', 'diffAmount', 'price', 'depositAmount']
      })
      res.data.items = arr
      this.details = res.data
    })
  },
  methods: {
    goBack() {
      this.$parent.showPage = 1
    },
    handleSvse() {
      const arr = []
      if (!this.details.installDate) {
        this.$errorMsg('请选择安装时间')
        return
      }
      this.fileList.forEach(el => {
        //  console.log(this.details.dataList.length)
        if (this.details.dataList.length) {
          this.details.dataList.forEach((i, e) => {
            console.log(i.fileName == el.name)
            if (el.name === i.fileName) {
              this.details.dataList.splice(e, 1)
            }
          })
        }

        arr.push({
          dataDescribe: el.name,
          enginOrderNo: this.details.refProjectNo,
          fileName: el.name,
          fileUrl: el.url
        })
      })
      applyDeposit({
        ...this.details,
        dataList: [...arr, ...this.details.dataList]
      }).then(res => {
        this.$successMsg('申请成功')
        this.$parent.showPage = 1
        this.$parent.deduction = ''
        this.$parent.getList()
      })
    },
    delFile(index) {
      this.details.dataList.splice(index, 1)
      this.$successMsg('删除成功')
    },
    handleReset() {
      this.$refs.form.resetFields()
    },
    // 批量下载
    async batchDownloadFn() {
      downloadFiles('deposit-manage/downZip', { parentId: this.$parent.depositManageId })
    },
    // 下载
    downLoadFn(v, fileName) {
      if (this.checkFileType(v) === 'pdf') {
        downloadPdf(this.$imageUrl + v, fileName)
        return
      }
      downloadFiles('common/file/getStream', { key: v, fileName: fileName })
      // getFileUrl({ key: v }).then((res) => {
      //   window.open(res.data)
      // })
    },
    openPdf(pdfUrl) {
      window.open(this.$imageUrl + pdfUrl)
    },
    // 检查文件类型
    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 ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.aaa {
  height: 64px;
  width: 64px;
}
.gdzl {
  display: inline-block;
}
::v-deep .textareaStyle {
  .el-textarea__inner {
    resize: none;
    height: 100px;
  }
}
.dateStyle {
  width: 100%;
}
::v-deep .el-input__prefix {
  display: none;
  left: 65px !important;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}
::v-deep .el-date-editor.el-input {
  width: 100%;
  box-sizing: border-box;
  padding-left: 10px;
}
.diy-table-1 {
  ::v-deep .el-form-item {
    margin: 0;
  }
  ::v-deep .el-input__suffix {
    right: -15px;
    -webkit-transition: all 0.3s;
    transition: all 0.3s;
  }
}
</style>
