<template>
  <el-dialog
    :visible.sync="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.name"
            autocomplete="off"
            placeholder="请输入作者"
          ></el-input>
        </el-form-item>
        <el-form-item label="日期" :label-width="formLabelWidth">
          <!-- <el-input
            v-model="dialogForm.name"
            autocomplete="off"
            placeholder="请输入作者"
          ></el-input> -->
          <el-date-picker
            v-model="dialogForm.date"
            type="date"
            placeholder="请选择日期（出版年份、发布日期）"
          >
          </el-date-picker>
        </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-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="status"> </el-switch>
        </el-form-item>
        <el-form-item label="文件" :label-width="formLabelWidth">
          <AutoUploader
            v-model="ltFile"
            :fileLimit="1"
            :fileSize="50"
            listType="text"
            :fileType="['pdf']"
            ref="museumFaceImage"
          />
        </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 { addVirtual, editVirtual } from "@/api/vitual";
import AutoUploader from "@/components/Uploader/AutoUploader.vue";
import { mapGetters } from "vuex";
export default {
  name: "InfoEditDialog",
  components: {
    AutoUploader,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    form: {
      type: Object,
      default: () => ({}),
    },
  },
  computed: {
    ...mapGetters(["userInfo"]),
    dialogVisible: {
      get: function () {
        return this.visible;
      },
      set: function () {},
    },
    title() {
      if (this.dialogForm.bvId) {
        return "修改虚拟展厅信息";
      } else {
        return "添加虚拟展厅";
      }
    },
  },
  watch: {
    form: {
      handler: function (value) {
        let that = this;
        that.dialogForm = JSON.parse(JSON.stringify(value));
        // 编辑状态
        if (that.dialogForm.literatureId) {
          // console.log("that.dialogForm.", that.dialogForm);
          that.status = Boolean(Number(that.dialogForm.status));
          // 回填封面
          if (this.dialogForm.files) {
            that.files = [
              {
                name: this.dialogForm.name + "封面.png",
                url: this.dialogForm.faceImageUrl,
                fileId: this.dialogForm.faceImage,
              },
            ];
          }
        }
      },
      immediate: true,
      deep: true,
    },
    status(value) {
      console.log(value);
    },
  },
  data() {
    return {
      dialogForm: {},
      formLabelWidth: "100px",
      status: false,
      files: [], //文献文件
    };
  },
  methods: {
    handlePreview({ type, file }) {
      console.log(type, file);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    // 取消编辑
    cancelForm() {
      this.$emit("handleClose");
      this.faceImage = [];
    },

    async handleSubmit() {
      if (this.dialogForm.bvId) {
        let params = { ...this.dialogForm };
        // 回填文件
        if (this.faceImage.length > 0) {
          params.faceImage = this.faceImage[0].fileId;
        }
        // 处理状态
        // debugger
        params.status = this.status ? 1 : 0;
        let res = await editVirtual(params);
        if (res.code == 0) {
          this.$message.success("修改成功！");
          this.reload();
        }
      } else {
        let params = { ...this.dialogForm };
        // 回填图片
        if (this.faceImage.length > 0) {
          params.faceImage = this.faceImage[0].fileId;
        }
        // 处理状态
        params.status = this.status ? 1 : 0;
        // return
        let res = await addVirtual(params);
        if (res.code == 0) {
          this.$message.success("添加成功！");
          this.reload();
        }
      }
    },
    reload() {
      this.$emit("refresh", true); //需要重新获取orgTree
      this.$emit("handleClose");
      this.faceImage = [];
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.$emit("handleClose");
          this.faceImage = [];
        })
        .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;
  }
  .relate {
    flex: 1;
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}


</style>