<template>
  <div>
    <el-dialog
      top="8vh"
      title="资产历史"
      :visible.sync="dialogVisible"
      width="680px"
    >
      <el-scrollbar style="height: 58vh">
        <el-form
          ref="elForm"
          :model="form"
          label-width="80px"
          size="small"
          label-position="right"
        >
          <el-form-item prop="riqi" label="日期" required>
            <el-date-picker v-model="form.riqi" placeholder="请选择日期" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item prop="neirong" label="内容">
            <my-tinymce v-model="form.neirong"></my-tinymce>
          </el-form-item>
          <el-form-item prop="id_his_tupian_files_asset_history_id_relation" label="图片">
            <el-upload
              ref="upload"
              :action="action"
              :file-list="fileList"
              :headers="{
                Authorization: `Bearer ${getToken()}`,
              }"
              :auto-upload="false"
              multiple
              list-type="picture"
              :on-success="successHandle"
              :before-upload="uploadBefore"
              :on-remove="removeHande"
            >
              <el-button slot="trigger" size="mini" type="primary">选取图片</el-button>
              <el-button style="margin-left: 10px;" size="mini" type="success" @click="$refs['upload'].submit()">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>
        </el-form>
      </el-scrollbar>

      <template #footer>
        <el-button size="mini" @click="hide">取 消</el-button>
        <el-button size="mini" type="warning" plain @click="init">重 置</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { save, show } from "@/api/system/baseForm";
import { getToken } from "@/utils/auth";

export default {
  data() {
    return {
      action: process.env.VUE_APP_UPLOAD_API,
      dialogVisible: false,
      asset_id: "",
      id: "",
      type: 'add',

      form: {
        riqi: "",
        neirong: "",
        id_his_tupian_files_asset_history_id_relation: "",
      },
      fileList: [],
    };
  },
  methods: {
    //上传
    successHandle(response, file, fileList) {
      this.fileList = fileList;
    },
    removeHande(file, fileList) {
      this.fileList = fileList;
    },
    uploadBefore(file) {
      console.log(file);
      if (file.size / 1000 > 50 * 1024) {
        this.$message({
          type: "warning",
          message: "上传图片大小超过50M！",
        });
        return false;
      }
    },

    getToken,
    show() {
      this.dialogVisible = true;
    },
    hide() {
      this.dialogVisible = false;
    },
    setType(type = "add") {
      let types = ["add", "editor"];
      if (types.includes(type)) {
        this.type = type;
      } else {
        console.warn("Unknown type: " + type);
      }
    },
    setId (id) {
      this.id = id
    },
    setAssetId(id) {
      this.asset_id = id;
    },
    init() {
      for (let key in this.form) {
        if (this.form[key] instanceof Array) {
          this.form[key] = [];
        } else {
          this.form[key] = "";
        }
      }
      this.$refs["elForm"].clearValidate();
    },

    async getDetail () {
      const res = await show({
        table_name: 'asset_histories',
        id: this.id
      })

      this.$integrateData(this.form,res)
      this.fileList = res.id_his_tupian_files_asset_history_id_relation.map(i => {
        return {
          response: i,
          name: i.original_name,
          url: i.url
        }
      })
    },

    submit () {
      if (this.type === "add") {
        if (this.form.hasOwnProperty("id")) {
          delete this.form.id;
        }
      }
      if (this.type === "editor") {
        Object.defineProperty(this.form, "id", {
          value: this.id,
          enumerable: true,
          configurable: true,
          writable: true,
        });
      }
      this.form.id_his_tupian_files_asset_history_id_relation = this.fileList.map(i => {
        return {
          original_name: i.response.original_name,
          name: i.response.name,
          url: /:\/\/:\/\//g.test(i.response.url) ? i.response.url.replace(/:\/\/:\/\//g,"://") : i.response.url,
          file_id: i.response.id
        }
      })
      save({
        table_name: "asset_histories",
        asset_id: this.asset_id,
        ...this.form
      }).then(res => {
        this.$message({
          type: 'success',
          message: '新增资产历史成功'
        })
        this.hide()
        this.init()
        this.$emit('refresh')
      })
    }
  },
  computed: {},
  watch: {
    dialogVisible (newVal) {
      if (newVal) {
        if (this.type === 'editor') {
          this.getDetail()
        }
      } else {
        this.id = '';
        this.asset_id = '';
        this.fileList = [];
      }
    }
  }
};
</script>

<style scoped lang="scss"></style>
