<template>
  <div class="customer-css">
    <el-dialog :title="titleCon" :close-on-click-modal="false" :visible.sync="visible" :before-close="closeDialog" :showClose="false" width="390px" style="height: 530px;" class="customer-dialog">
      <el-form :inline="true" label-position="top" label-width="80px">
        <el-row>
          <el-col :span="12" style="width:36%">
            <el-form-item class="customer-item" label="检验单号">
              <el-input v-model="pageData.inspectionNo" style="width: 120px;" disabled ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="customer-item" label="项目编码">
              <el-input v-model="pageData.itemNo" style="width: 120px;" disabled ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- 图像区域 -->
          <ul class="content-image" v-viewer>
            <li v-for="(item, index) in descImgs" :key="index" style="float: left;display: inline">
              <img :src="item.url" style="width:70px;height: 70px"/>
              <!-- 删除图标 -->
              <div class="delete-img">
                <i class="el-icon-delete" @click="deleteImage(index,item.id)"></i>
              </div>
            </li>
          </ul>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-upload class="customer-upload" drag multiple :file-list="fileList"
                       action="javascript:void(0);" ref="uploadFile"
                       :before-upload="beforeUploadHandle" :on-change="onChange"
                       accept=".jpg,.jpeg,.png,.gif" :auto-upload="false"
                       style="text-align: left;">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveUploadFile">保存</el-button>
        <el-button type="primary" @click="closeDialog">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {uploadEamObjectFile} from '@/api/eam/com_eam_object_upload_file.js';
import {
  searchItemFileUrl, // 查询文件路径
  imageDelete, // 删除图片
} from "@/api/qc/qc.js"
/* 引入组件 */
var functionId = 'C10000002';

export default {
  data() {
    return {
      titleCon: '项目图片上传',
      visible: false,
      userId: this.$store.state.user.name,
      fileList: [],
      pageData: {
        site: '',
        buNo: '',
        username: this.$store.state.user.name,
        inspectionNo: '',
        itemNo: '',
        fileRemark: '',
        folder: ''
      },
      dataListLoading: false,
      descImgs: [],
    }
  },
  methods: {
    // 初始化组件的参数
    init (currentRow) {
      // 初始化参数
      this.pageData = JSON.parse(JSON.stringify(currentRow))
      // 打开页面
      this.visible = true
      this.descImgs = []
      this.searchItemFileUrl()
    },
    // 查询图片列表
    searchItemFileUrl () {
      this.descImgs = []
      searchItemFileUrl(this.pageData).then(({data}) => {
        if (data.code === 0) {
          for (let i = 0; i < data.rows.length; i++) {
            this.descImgs.push(data.rows[i])
          }
        } else {
          this.$message.warning(data.msg)
        }
      })
    },

    // 上传之前
    beforeUploadHandle (file) {
      if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
        this.$message.error('只支持图片！')
        return false
      }
    },
    // 选择上传文件时
    onChange (file, fileList) {
      this.fileList = fileList
    },
    // 关闭modal
    closeDialog () {
      this.fileList = []
      // 清空文件上传记录
      this.$refs.uploadFile.clearFiles()
      // 刷新报工的页面
      //this.$emit('refreshPageTables2')
      // 关闭当前的页面
      this.visible = false
    },

    // 保修当前的数据
    saveUploadFile () {
      // 判断文件是否上传
      if (null == this.fileList || 0 === this.fileList.length) {
        this.$message.error("请先上传图片!")
        return false
      }
      const formData = new FormData()
      // 片接文件
      for (let i = 0; i < this.fileList.length; i++) {
        formData.append("file",this.fileList[i].raw)
      }
      formData.append("site", this.pageData.site)
      formData.append("folder", this.pageData.folder)
      formData.append("objectID", this.pageData.inspectionNo)
      formData.append("orderRef1", this.pageData.itemNo)
      formData.append("orderRef4", this.pageData.buNo)
      uploadEamObjectFile(formData).then(({data}) => {
        if (data.code === 0) {
          this.$message.success(data.msg)
          // 清空文件上传记录
          this.$refs.uploadFile.clearFiles()
          this.searchItemFileUrl()
          this.fileList = []
        } else {
          this.$message.warning(data.msg)
        }
      })
    },

    // 删除图片
    deleteImage (index, id) {
      let param = {
        id: id
      }
      this.$confirm(`是否删除该图片?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        imageDelete(param).then(({data}) => {
          if (data.code === 0) {
            this.searchItemFileUrl()
            this.$message.success(data.msg)
          }else {
            this.$message.warning(data.msg)
          }
        })
      }).catch(() => {
      })
    },
  },
}
</script>
<style scoped lang="scss">

</style>
