<template>
  <div>
    <el-form :model="formData" :rules="formRules" ref="dataForm">
      <el-card style="margin-top: -30px">
        <el-button-group style="display: flex">
          <el-button type="primary"   @click.native="submitUpload()" style="margin: 0 60px 10px auto; height: 35px"
                     :loading="loading">提交
          </el-button>
        </el-button-group>
        <el-row>
          <el-col :span="3">
            <div class="ao-text">
              <span>证书名称：</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="name">
              <el-input v-model="formData.name" auto-complete="off" :disabled="formData.auditStatus == 1"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="3">
            <div class="ao-text">
              <span>证书编号：</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="code">
              <el-input v-model="formData.code" auto-complete="off" :disabled="formData.auditStatus == 1"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="3">
            <div class="ao-text">
              <span>起始日期：</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="vailDate">

              <el-date-picker
                v-model="formData.vailDate"
                type="date"
                :disabled="formData.auditStatus == 1"
                value-format="yyyy-MM-dd"
                style="width: 100%"
                placeholder="请选择生效期">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="3">
            <div class="ao-text">
              <span>有效期：</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="expireDate">

              <el-date-picker
                v-model="formData.expireDate"
                type="date"
                :disabled="formData.auditStatus == 1"
                value-format="yyyy-MM-dd"
                style="width: 100%"
                placeholder="请选择失效期">
              </el-date-picker>

            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="3">
            <div class="ao-text">
              <span>证书备注：</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="remark">
              <el-input v-model="formData.remark" auto-complete="off" type="textarea" autosize></el-input>
            </el-form-item>
          </el-col>


        </el-row>


        <el-row>
          <el-col :span="3">
            <div class="ao-text">
              <span>证书照片：</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-upload
                class="upload-demo"
                ref="upload"
                :action="this.uploadUrl"
                :on-preview="uploadHandlePreview"
                :on-remove="uploadHandleRemove"
                :headers="headers"
                :on-exceed="uploadHandleExceed"
                accept=".jpg,.png"
                :on-change="uploadOnchange"
                :on-success="uploadHandleSuccess"
                :on-error="uploadHandleError"
                :file-list="fileList"
                :data="{type:'image2'}"
                :auto-upload="true">
                <el-button slot="trigger" size="small" type="primary" :disabled="formData.auditStatus == 1">
                  {{ choiceFile }}
                </el-button>
                <div>只能上传 jpg,png 文件，且不超过 3 MB</div>
              </el-upload>
              <!--                            <el-button type="primary" size="mini" icon="search" @click="toViewCompanyCert"-->
              <!--                                       style="text-align:right"-->
              <!--                            >文件预览-->
              <!--                            </el-button-->
              <!--                            >-->

            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>

  </div>
</template>

<script>
import {isertCert, filterCompanyCert, updateCert} from "@/api/sup/userCert";
import store from "../../../store";




export default {
  name: "supCompanyCertAdd",
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
    inputQuery: {
      type: Object,
      required: true,
    },
    editTye: {
      type: Object,//1:新增，2：编辑
      required: true,
    },
    ucId: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      BASE_URL: process.env.VUE_APP_BASE_API,
      code: "",
      isCleck:false,
      formData: {
        vailDate: null,
        expireDate: null,
        remark: null,
        checkComment:null,
        code: null,
        name: null,
        manufacturerIdFk: null,
        status: null,
      },
      formRules: {
        name: [
          {required: true, message: "请输入证书名称", trigger: "blur"}
        ],
        code: [
          {required: true, message: "请输入证书编号", trigger: "blur"}
        ],
      },
      loading: false,
      uploadUrl: "",
      fileList: [],
      headers: {},
      choiceFile: "选取文件",

    };
  },
  components: {
  },
  methods: {
    uploadHandlePreview(file) {
      console.log(file);
      console.log(this.fileList);
    },
    uploadHandleRemove(file, fileList) {
      this.formData.filePath="";
      for (var i=0;i<fileList.length;i++){
        this.formData.filePath+=fileList[i].response.data.name+",";
      }
    },
    uploadHandleExceed(files, fileList) {

    },
    uploadOnchange(file, fileList) {
      let fileName = file.name;
      let uid = file.uid
      let pos = fileName.lastIndexOf(".");
      let lastName = fileName.substring(pos, fileName.length);
      if (lastName.toLowerCase() !== ".jpg" && lastName.toLowerCase() !== ".png" && lastName.toLowerCase() !== ".doc") {
        this.$message.error("上传文件只能是 jpg,png格式");
        for (let i = 0; i < fileList.length; i++) {//从list删除
          if (fileList[i].uid === uid) {
            fileList.splice(i, 1)
          }
        }
        return;
      }
      // 限制上传文件的大小
      const isLt = file.size / 1024 / 1024 / 3 <= 1;
      if (!isLt) {
        this.$message.error("上传文件大小不能超过 3MB");
        for (let i = 0; i < fileList.length; i++) {
          if (fileList[i].uid === uid) {
            fileList.splice(i, 1)
          }
        }
      }
      return isLt;
    },
    uploadHandleSuccess(response, file, fileList) {
      if (response.code === 20000) {
        if(this.formData.filePath==null || this.isCleck==true){
          this.formData.filePath=response.data.name+",";
          this.isCleck=false;
        }else{
          this.formData.filePath+=response.data.name+",";
        }
      } else {
        this.$message.error("文件上传失败：" + response.message);
      }
    },
    uploadHandleError() {

    },

    submitUpload(){
      debugger
          if(this.editTye == 1){
            this.onConfirmSubmit();
          }else{
            this.updateCert();
          }


    },

    onConfirmSubmit() {
      this.formData.businessId=this.ucId;
      isertCert(this.formData).then( (res)=>{
            if(res.code == 20000){
              this.$message.success("添加成功!")
                this.closeDialog();
            }

      })

    },
    updateCert(){
      updateCert(this.formData).then((res)=>{
        if(res.code == 20000){
          debugger
          this.$message.success("修改成功!")
          this.closeDialog();
        }
      })
    },







  },
  filters: {},
  mounted() {

  },
  created() {
    this.uploadUrl = this.BASE_URL + "/udiwms/upload/register/file";
    this.headers = {
      ADMIN_ID: store.getters.adminId,
      ADMIN_TOKEN: store.getters.token,
    };
    if (this.inputQuery.formData == null) {
      this.formData = {};
    }else {
      this.formData = this.inputQuery.formData;
    }
    if (this.formData.filePath != null) {
      this.choiceFile = "更换文件";
      this.isCleck=true;
    } else {
      this.choiceFile = "选取文件";
    }

  },
};
</script>
<style>

.ao-text {
  width: 100%;
  font-size: 13px;
  font-family: "Microsoft YaHei";
  float: right;
  text-align: right;
  margin-top: 10px;
}

</style>



