<template>
  <el-dialog
    :visible="dialogVisible"
    width="40%"
    style="height: 98%"
    :before-close="handleClose"
    top="5vh"
    lock-scroll
  >
    <div class="title" slot="title">
      <div class="divider"></div>
      <div class="label">{{ title }}</div>
    </div>
    <div class="dialog-content">
      <el-form :model="dialogForm" class="basic-info">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input
            v-model="dialogForm.name"
            autocomplete="off"
            placeholder="请输入文献名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="作者" :label-width="formLabelWidth">
          <el-input
            v-model="dialogForm.authors"
            autocomplete="off"
            placeholder="请输入作者"
          ></el-input>
        </el-form-item>
        <el-form-item label="文献来源" :label-width="formLabelWidth">
          <el-input
            v-model="dialogForm.source"
            autocomplete="off"
            placeholder="请输入文献来源"
          ></el-input>
        </el-form-item>
        <el-form-item label="出版/发布日期" :label-width="formLabelWidth">
          <el-date-picker
            style="width: 100%"
            v-model="dialogForm.date"
            type="date"
            placeholder="请选择出版/发布日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="status"> </el-switch>
        </el-form-item>
        <el-form-item label="文件" :label-width="formLabelWidth">
          <ManualUploader
            :files="files"
            :fileLimit="1"
            :fileSize="50"
            listType="text"
            :fileType="['pdf']"
            ref="pdf"
          />
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            placeholder="请输入备注"
            v-model="dialogForm.remark"
            maxlength="600"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="dialog-footer">
      <el-button @click="cancelForm">取 消</el-button>
      <el-button type="primary" @click="handleSubmit"
        >确定<i class="el-icon-right" style="margin-left: 5px"></i
      ></el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addLiterature, editLiterature } from "@/api/literature";
import ManualUploader from "@/components/Uploader/ManualUploader.vue";
import { mapGetters } from "vuex";
import { uploadFile } from "@/utils/file";
export default {
  name: "InfoEditDialog",
  components: {
    ManualUploader,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    form: {
      type: Object,
      default: () => ({}),
    },
  },
  computed: {
    ...mapGetters(["userInfo"]),
    title() {
      if (this.dialogForm.literatureId) {
        return "修改文献";
      } else {
        return "添加文献";
      }
    },
  },
  watch: {
    form: {
      handler: function (value) {
        let that = this;

        that.dialogForm = JSON.parse(JSON.stringify(value));
        // 编辑状态
        if (that.dialogForm.literatureId) {
          // 回填状态
          that.status = Boolean(Number(that.dialogForm.status));
          // 回填文件
          if (that.dialogForm.files) {
            that.files = that.dialogForm.files;
          }
        }
      },
      immediate: true,
      deep: true,
    },
    visible: {
      handler: function (value) {
        this.dialogVisible = value;
      },
      deep: true,
      immediate: true,
    },
  },
  data() {
    return {
      dialogForm: {},
      formLabelWidth: "100px",
      status: false,
      files: [], //文献文件
      dialogVisible: false,
    };
  },
  methods: {
    handlePreview({ type, file }) {
      console.log(type, file);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    // 取消编辑
    cancelForm() {
      this.$emit("handleClose");
      this.faceImage = [];
    },

    async handleSubmit() {
      // debugger
      // console.log(this.dialogForm);
      // return
      let params = JSON.parse(JSON.stringify(this.dialogForm));
      // 回填文件
      let file = this.$refs.pdf.getFiles();
      let formData = new FormData();
      // console.log("file", file);
      // return
      //  debugger;
      if (file[0].status == "ready") {
        // debugger;
        formData.append("files", file[0].raw);
        let upLoadRes = await uploadFile(formData);
        if (upLoadRes.code == 0) {
          params.pdfFile = upLoadRes.data[0].fileId;
        } else {
          this.$message.error("上传失败！:" + upLoadRes.data.msg);
        }
      } else if (file[0].status == "success") {
        params.pdfFile = file[0].fileId;
      }
      // 修改状态
      params.status = this.status ? 1 : 0;
      if (params.literatureId) {
        let res = await editLiterature(params);
        if (res.code == 0) {
          this.$message.success("修改成功！");
          this.reload();
        }
      } else {
        let res = await addLiterature(params);
        if (res.code == 0) {
          this.$message.success("添加成功！");
          this.reload();
        }
      }
    },
    reload() {
      this.$emit("refresh");
      this.$emit("handleClose");
      this.files = [];
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.$emit("handleClose");
          this.files = [];
        })
        .catch((_) => {});
    },
  },
};
</script>

<style lang='scss' scoped>
.title {
  display: flex;
  margin-bottom: 16px;
  .divider {
    width: 8px;
    border-left: 4px solid #409eff;
    margin-right: 8px;
  }
  .label {
    font-weight: bold;
  }
}
.dialog-content {
  padding: 0 32px;
  display: flex;
  .basic-info {
    flex: 1;
    margin-right: 48px;
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>