<template>
  <div class="customer-css">
    <el-dialog :title="pageData.titleCon" :close-on-click-modal="false" :visible.sync="visible" width="390px" style="height: 520px;" class="customer-dialog">
      <el-form :inline="true" label-position="top" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item class="customer-item" label="物料编码">
              <el-input v-model="pageData.partNo" style="width: 120px;" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验类型">
              <el-select v-model="pageData.inspectionTypeNo" placeholder="请选择">
                <el-option
                  v-for="i in pageData.options"
                  :key="i.inspectionTypeNo"
                  :label="i.inspectionTypeName"
                  :value="i.inspectionTypeNo">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </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="*"
              :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 { uploadSopFile } from "@/api/qc/dmBasic.js";

export default {
  name: "DmSopUploadFile",
  data() {
    return {
      visible: false,
      fileList: [],
      pageData: {
        titleCon: "",
        site: "",
        buNo: "",
        createBy: "",
        partNo: "",
        inspectionTypeNo: "",
        options: []
      },
    };
  },
  methods: {
    init(currentRow) {
      this.pageData = JSON.parse(JSON.stringify(currentRow));
      this.visible = true;
    },
    beforeUploadHandle() {},
    onChange(file, fileList) {
      this.fileList = fileList;
    },
    closeDialog() {
      this.fileList = [];
      this.$refs.uploadFile.clearFiles();
      this.$emit("refreshPageTables");
      this.visible = false;
    },
    saveUploadFile() {
      if (this.fileList == null || this.fileList.length === 0) {
        this.$message.error("请先上传文件!");
        return false;
      }
      if (this.pageData.inspectionTypeNo === null || this.pageData.inspectionTypeNo === "") {
        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("orderRef1", this.pageData.site);
      formData.append("orderRef2", this.pageData.partNo);
      formData.append("orderRef3", this.pageData.inspectionTypeNo);
      formData.append("createBy", this.pageData.createBy);
      formData.append("orderRef4", this.pageData.buNo);
      uploadSopFile(formData).then(({ data }) => {
        if (data.code === 0) {
          this.$message.success(data.msg);
          this.$refs.uploadFile.clearFiles();
          this.fileList = [];
        } else {
          this.$message.warning(data.msg);
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
</style>

