<template>
  <el-dialog
    :visible="dialogVisible"
    width="40%"
    :before-close="handleClose"
    top="5vh"
    lock-scroll
    v-loading="loading"
    element-loading-background="rgba(0, 0, 0, 0.5)"
  >
    <div class="title" slot="title">
      <div class="divider"></div>
      <div class="label">{{ title }}</div>
    </div>
    <div class="el-dialog-div">
      <div class="dialog-content">
        <el-form
          :model="dialogForm"
          class="basic-info"
          ref="form"
          :rules="rules"
          size="mini"
        >
          <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
            <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 size="mini" @click="cancelForm">取 消</el-button>
        <el-button size="mini" type="primary" @click="handleSubmit"
          >确定<i class="el-icon-circle-check" style="margin-left: 5px"></i
        ></el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { addLiterature, editLiterature } from "@/api/literature";
import { mapGetters } from "vuex";
import { uploadFile } from "@/utils/file";
export default {
  name: "InfoEditDialog",
  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: true,
      files: [], //文献文件
      dialogVisible: false,
      rules: {
        name: [
          {
            required: true,
            message: "请输入文献名称",
            trigger: "blur",
          },
        ],
      },
      loading: false,
    };
  },
  methods: {
    // 取消编辑
    cancelForm() {
      this.$emit("handleClose");
      // this.reset();
      this.resetChildData();
      this.reloadParent();
    },

    resetChildData() {
      this.files = [];
      this.$refs.form.resetFields();
    },

    reloadParent() {
      this.$emit("handleClose");
      this.$emit("reloadParent");
    },
    handleSubmit() {
      let that = this;
      this.loading = true;
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(that.dialogForm));
          // 回填文件
          let file = that.$refs.pdf.getFiles();
          let formData = new FormData();
          if (file.length > 0) {
            if (file[0].status == "ready") {
              formData.append("files", file[0].raw);
              let upLoadRes = await uploadFile(formData);
              if (upLoadRes.code == 0) {
                params.pdfFile = upLoadRes.data[0].fileId;
              } else {
                that.$message.error("上传失败！:" + upLoadRes.data.msg);
              }
            } else if (file[0].status == "success") {
              params.pdfFile = file[0].fileId;
            }
          } else {
            params.pdfFile = "";
          }

          // 修改状态
          params.status = that.status ? 1 : 0;
          if (params.literatureId) {
            let res = await editLiterature(params);
            if (res.code == 0) {
              that.$message.success("修改成功！");
              that.reloadParent();
              that.resetChildData();
            }
            this.loading = false;
          } else {
            let res = await addLiterature(params);
            if (res.code == 0) {
              that.$message.success("添加成功！");
              that.reloadParent();
              that.resetChildData();
            }
            this.loading = false;
          }
        }
      });
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.reloadParent();
          this.resetChildData();
        })
        .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;
  }
}

.el-dialog-div {
  // height: 50vh;
  overflow-x: hidden;
}

.dialog-content {
  padding: 0 32px;
  display: flex;

  .basic-info {
    flex: 1;
    margin-right: 48px;
  }

  .relate {
    flex: 1;
  }
}

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