<template>
  <div>
    <!-- 附件上传 -->
    <el-dialog
      :visible.sync="showUpload"
      width="30%"
      append-to-body
      destroy-on-close
      custom-class="my-dialog-container"
    >
      <div slot="title" class="my-dialog-title">附件上传</div>
      <FileUpload
        v-model="list"
        :isShowTip="false"
        v-loading="fileLoading"
        v-if="!showTitle"
      >
        <el-button size="mini" type="primary">上传培训情况附件</el-button>
      </FileUpload>
      <div v-if="showTitle" v-loading="fileLoading">
        <FileUpload v-model="list2" :isShowTip="false">
          <el-button size="mini" type="primary">上传工作内容证明、职称证书等附件</el-button>
        </FileUpload>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-my-save"
          @click="fileSubmit()"
          >提交</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      showUpload: false,
      fileLoading: true,
      list: [],
      // list1: [],
      list2: [],
      //培训
      filelist: {
        id: null,
        assessmentFilename: "",
        assessmentResultsUrl: "",
      },
      //工作
      workList: {
        //职称证书
        titleCertificate: "",
        titleFilename: "",
        // //工作证明
        // workCertificate: "",
        // workFilename: "",
        id: null,
      },
      type: "",
    };
  },
  props: {
    showTitle: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    //打开弹窗
    async open(e, s) {
      this.showUpload = true;
      this.fileLoading = true;
      this.list = [];
      // this.list1 = [];
      this.list2 = [];
      this.type = s;
      if (s == "培训") {
        //获取培训情况的详情
        this.filelist.id = e;
        let msg1 = await this.$api.training.detailTraining(e);
        if (msg1.status == 200) this.fileLoading = false;
        if (
          msg1.result.assessmentResultsUrl &&
          msg1.result.assessmentFilename
        ) {
          this.list = this.begin(
            msg1.result.assessmentResultsUrl,
            msg1.result.assessmentFilename
          );
        }
      } else if (s == "工作") {
        this.workList.id = e;
        let msg1 = await this.$api.work.detailWork(e);
        if (msg1.status == 200) this.fileLoading = false;
        // if (msg1.result.workCertificate && msg1.result.workFilename) {
        //   this.list1 = this.begin(
        //     msg1.result.workCertificate,
        //     msg1.result.workFilename
        //   );
        // }
        if (msg1.result.titleCertificate && msg1.result.titleFilename) {
          this.list2 = this.begin(
            msg1.result.titleCertificate,
            msg1.result.titleFilename
          );
        }
      }
    },
    //点击上传文件弹窗的确认按钮
    async fileSubmit() {
      if (this.type == "培训") {
        let newList = this.submit(this.list);
        this.filelist.assessmentFilename = newList[0].join(",");
        this.filelist.assessmentResultsUrl = newList[1].join(",");
        let msg = await this.$api.training.updateTraining(this.filelist);
        if (msg.status == 200)
          this.$message({
            type: "success",
            message: `修改成功!`,
          });
      } else if (this.type == "工作") {
        // let newList1 = this.submit(this.list1);
        // this.workList.workFilename = newList1[0].join(",");
        // this.workList.workCertificate = newList1[1].join(",");
        let newList2 = this.submit(this.list2);
        this.workList.titleFilename = newList2[0].join(",");
        this.workList.titleCertificate = newList2[1].join(",");
        let msg = await this.$api.work.updateWork(this.workList);
        if (msg.status == 200)
          this.$message({
            type: "success",
            message: `修改成功!`,
          });
      }
      this.showUpload = false;
    },
    //进入时的循环函数
    begin(a, b) {
      let arr1 = [];
      let arr2 = [];
      let arr3 = [];
      arr1 = a.split(",");
      arr2 = b.split(",");
      arr1.forEach((value, index) => {
        arr3.push({
          url: value,
          name: arr2[index],
        });
      });
      return arr3;
    },
    //提交时的循环函数
    submit(a) {
      let arr1 = [];
      let arr2 = [];
      a.forEach((value, index) => {
        arr1.push(value.name);
        arr2.push(value.url);
      });
      return [arr1, arr2];
    },
  },
};
</script>
<style lang="scss" scoped></style>
