import {selectDialogMixins} from "../../mixins/selectDialog/index";
import settingConfig from "@/settings.js";

export let indexMixin, uploadDialogMixin;
indexMixin = {
  props: {
    option: {
      type: Object,
    },
    value: {
      type: [String, Object, Array]
    },
    bindData: Boolean,
    imageLazy: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      rows: [],
      showUploadDialog: false,
      size: 0,
      imageUrl: null,
      fileName: null,
      showSourceUrl: null,
      showContent: false,
      isUploadEnabled: false
    };
  },
  computed: {
    title() {
      let fullTitle = this.option.fullTitle
      if(fullTitle){
        return fullTitle
      }
      return (
        this.$t2("查看", "system.button.check") +
        (this.option && this.option.title
          ? this.option.title
          : this.$t2("凭证信息", "components.baseAttachment.title"))
      );
    },
    edit() {
      return this.option && this.option.edit ? this.option.edit : false;
    },
    showViewButton() {
      return this.option && this.option.showViewButton !== false ? true : false;
    },
    fileServerPreviewEnabled(){
      return this.getFileServerPreviewEnabled();
    },
    fileShowImageEnabled(){
      return this.getFileShowImageEnabled();
    },
  },

  created() {
    // this.initRows();
    this.initEvent()
  },
  mounted() {
  },
  methods: {
    getFileServerPreviewEnabled(){
      let value =  window.$vueRoot.$store.getters.fileServerInfo?.fileServerPreviewEnabled ?? (settingConfig.fileServerPreviewEnabled ?? true);
      if(this.option.fileServerPreviewEnabled!== null && this.option.fileServerPreviewEnabled!== undefined){
        value = this.option.fileServerPreviewEnabled
      }
      return value;
    },
    getFileShowImageEnabled(){
      let value =  window.$vueRoot.$store.getters.fileServerInfo?.fileShowImageEnabled ?? (settingConfig.fileShowImageEnabled ?? true);
      if(this.option.fileShowImageEnabled!== null && this.option.fileShowImageEnabled!== undefined){
        value = this.option.fileShowImageEnabled
      }
      return value;
    },
    initEvent() {
      this.$watch(() => {
        return this.getValue();
      }, (value) => {
        this.initRows();
      }, {
        // deep:true,
        immediate: true
      })
    },
    initIsUploadEnabled() {
      let value = true;
      let uploadConfig = this.option.uploadConfig;
      if (uploadConfig && uploadConfig.limit) {
        value = uploadConfig.limit > this.rows.length
      }
      this.isUploadEnabled = value;
    },
    uploadFile() {
      let uploadConfig = this.option.uploadConfig;
      let config = {
        ...uploadConfig
      }

      this.$baseUpload.open({
        ...config,
        callback: (rows) => {
          if (rows.length) {
            let items = [
              ...this.rows,
              ...rows
            ]
            if (uploadConfig && uploadConfig.limit) {
              if (items.length > uploadConfig.limit) {
                items.length = uploadConfig.limit
              }
            }
            this.confirmInsertAttachment(items)
          }
        },
      });
    },
    initRows() {
      let value = this.$baseLodash.cloneDeep(this.getValue() || []);
      this.rows = value;
      this.$nextTick(() => {
        this.initIsUploadEnabled();
        this.initImageUrl();
        this.initShowSourceUrl();
        this.initFileName()
        this.initSize();
      })

    },
    getValue() {
      let value = null
      if (this.bindData) {
        value = this.value;
      } else if (typeof this.option.rows === "function") {
        let toDo = () => {
          let done = (attachments) => {
            value = attachments
          };
          this.option.rows(done);
        }
        toDo();
      } else {
        value = this.option.rows
      }
      return value;
    },
    initShowSourceUrl() {
      let attachment = this.rows;
      let showSourceUrl = null
      if (Array.isArray(attachment)) {
        if (attachment.length) {
          let row = attachment[0];
          if(row){
            showSourceUrl = row.domain + row.url;
          }else{
            showSourceUrl = null;
          }
        }
      } else if (Object.prototype.toString.call(attachment) === "[object Object]") {
        showSourceUrl = attachment.domain + attachment.url;
      } else {
        showSourceUrl = attachment || null;
      }
      this.showSourceUrl = showSourceUrl;
    },
    initImageUrl() {
      let showImagePreview = this.getFileShowImageEnabled();
      let attachment = this.rows;
      let imageUrl = null
      if (Array.isArray(attachment)) {
        if (attachment.length) {
          let row = attachment[0];
          if(row){
            imageUrl = this.$commonFileUtil.getShowUrl(
              row.domain +
              (row.medium ? row.medium : row.url),
              showImagePreview
            );
          }else{
            imageUrl = null;
          }
        }
      } else if (Object.prototype.toString.call(attachment) === "[object Object]") {
        imageUrl = this.$commonFileUtil.getShowUrl(
          attachment.domain +
          (attachment.medium ? attachment.medium : attachment.url),
          showImagePreview
        );
      } else {
        imageUrl = attachment
          ? this.$commonFileUtil.getShowUrl(attachment,showImagePreview)
          : null;
      }
      this.imageUrl = imageUrl;
    },
    initFileName() {
      let attachment = this.rows;
      let fileName = null
      if (Array.isArray(attachment)) {
        if (attachment.length) {
          let row = attachment[0];
          if(row){
            fileName = row.name;
          }else{
            fileName = null;
          }
        }
      } else if (Object.prototype.toString.call(attachment) === "[object Object]") {
        fileName = attachment.name;
      } else {
        if (attachment) {
          let index = attachment.lastIndexOf("/");
          if (index >= 0) {
            fileName = attachment.slice(index + 1);
          }
        }
      }
      this.fileName = fileName;
    },
    initSize() {
      let attachment = this.rows;
      let size = 0;
      if (Array.isArray(attachment)) {
        size = attachment.length;
      } else if (Object.prototype.toString.call(attachment) === "[object Object]") {
        size = 1;
      } else {
        size = !!attachment ? 1 : 0;
      }
      this.size = size;
    },
    openAttachmentDialog() {
      this.initRows();
      this.showUploadDialog = true;
      this.option.open && this.option.open();
    },
    confirmInsertAttachment(rows) {
      this.$emit("input", rows);
      this.option.confirm && this.option.confirm(rows);
      this.$emit("update:visiable", false);
      /*this.$nextTick(()=>{
        this.initRows();
      })*/
    },
    openPreview() {
      let url = this.showSourceUrl;
      if (url) {
        let fileServerPreviewEnabled = this.getFileServerPreviewEnabled();
        if (this.$commonFileUtil.isPictureFile(url)) {
          this.$baseFilePreview(url)
        } else if (fileServerPreviewEnabled && this.$commonFileUtil.isPreviewFile(url)) {
          this.$openFilePreview(url);
        }
      }
    },
  },
};
uploadDialogMixin = {
  props: {
    visiable: Boolean,
    multi: String,
    rows: Array,
    param: Object,
    onConfirm: Function,
    edit: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: () => this.$t2("凭证信息", "components.baseAttachment.title"),
    },
  },
  mixins: [selectDialogMixins],
  created() {
    this.initSetting();
    this.checkRows = this.$baseLodash.cloneDeep(this.rows || []);
    Object.assign(this.option, this.param);
    this.showUpload = true;
  },
  mounted() {
  },
  data() {
    var that = this;
    return {
      showDialog: true,
      falseValue: false,
      selectMulti: true,
      formData: {},
      vxeOption: {},
      option: {
        accept: "file",
        multi: true,
      },
      showUpload: false,
      checkRows: []
    };
  },
  methods: {
    closeWin() {
      this.showDialog = false;
      this.dialogClose();
    },
    confirmWin() {
      this.showDialog = false;
      this.dialogPrimary(this.checkRows);
      this.onConfirm && this.onConfirm(this.checkRows);
    },
  },
};
export default {
  indexMixin,
  uploadDialogMixin
};
