let modules;
modules = {
  name: 'propertiesDialog',
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
    treeNodeArr: {
      type: Array
    },
    option:Object
  },
  inject: ['current_prefix', 'getTitle'],
  components: {},
  data() {
    return {
      showDialog: true,
      falseValue: false,
      vxeOption: {},
      showContent: true,
      attachmentDTO: {
        fileAttachAttributeDTOs:[]
      },
      fileAttachObjDTOs:[],
      // current_prefix: '',
      filePrefix: '',
      fileName: '',
      vxeOption2:{}
    };
  },
  watch: {
    fileName(val) {
      this.attachmentDTO.fileName = this.fileName + this.filePrefix;
    }
  },
  computed: {
    pathNameArr() {
      if (this.attachmentDTO.treePathName) {
        return (this.getTitle() + this.attachmentDTO.treePathName.slice(0, -1)).split(',');
      }
    }
  },
  created() {
    let attachmentDTO = Object.assign({}, this.$attrs.editAttachment);
    this.attachmentDTO = attachmentDTO;
    /*this.current_prefix = attachmentDTO && attachmentDTO.serverName ? ('/' + attachmentDTO
        .serverName) :
      '';*/
    this.initFilePrefix();
  },
  mounted() {
    this.getData();
    this.initTableM1();
    this.initTableM2();
  },
  methods: {
    initFilePrefix() {
      let name = this.attachmentDTO.fileName;
      let index = name.lastIndexOf('.');
      if (index >= 0) {
        this.filePrefix = name.substring(index, name.length)
        this.fileName = name.substring(0, index)
      } else {
        this.fileName = name;
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t2('您确定要保存吗？', 'system.message.sureSave')).then(() => {
            var url = this.current_prefix + '/file_attach/attribute/save';
            this.$http({
              url: url,
              method: 'post',
              data: this.attachmentDTO,
              isLoading: true,
              success: res => {
                this.$message({
                  message: res.content,
                  type: 'success',
                  duration: 500,
                  onClose: t => {
                    this.$attrs.editAttachment.fileName = this.attachmentDTO.fileName
                    this.$attrs.editAttachment.note = this.attachmentDTO.note
                    this.reloadContent();
                  }
                });
              }
            });
          });
        }
      });
    },
    dialogClose() {
      this.$emit('update:visiable', false);
    },
    reloadContent() {
      this.getAttributes();
      this.getObjs();
    },
    getData() {
      this.$commonHttp({
        url: this.current_prefix + '/file_obj/get',
        method: 'post',
        data: {
          id: this.$attrs.editAttachment.id
        },
        isLoading: true,
        // modalStrictly: true,
        success: res => {
          this.attachmentDTO = res.objx || {};
          this.getAttributes();
          this.getObjs();
        }
      });
    },
    getAttributes() {
      this.$http({
        url: this.current_prefix + '/file_attach/attribute/list',
        method: 'post',
        data: {
          stringOne: this.attachmentDTO.fileSn
        },
        isLoading: true,
        // modalStrictly: true,
        success: res => {
          this.$set(this.attachmentDTO, 'fileAttachAttributeDTOs', res.objx || []);
        }
      });
    },
    getObjs() {
      this.$http({
        url: this.current_prefix + '/file_attach/obj/list',
        method: 'post',
        data: {
          stringOne: this.attachmentDTO.fileSn
        },
        isLoading: true,
        // modalStrictly: true,
        success: res => {
          // this.$set(this.attachmentDTO, 'fileAttachObjDTOs', res.objx || []);
          this.fileAttachObjDTOs = res.objx || []
        }
      });
    },
    initTableM1() {
      //明细
      var that = this;
      const tableRef = 'table-m1';
      const tableOption = {
        vue: this,
        tableRef: tableRef,
        tableName: 'user_fileLibrary_propertiesDialog_list-m1',
        columns: [
          {
            type: 'checkbox',
            fixed: 'left',
            width: 48,
            resizable: false
          },
          {
            title: this.$t2('属性名称', 'components.fileLibrary.attributeName'),
            field: 'attributeName',
            width: 200,
            slots: {
              default: "attributeName"
            }
          },
          {
            title: this.$t2('属性值', 'components.fileLibrary.attributeValue'),
            field: 'attributeValue',
            width: 200,
            slots: {
              default: "attributeValue"
            }
          },
          {
            width: 50,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: "operate"
            }
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption = opts;
      });
    },
    addItem() {
      this.attachmentDTO.fileAttachAttributeDTOs.push({});
    },
    formatFileSize(fileSize) {
      fileSize = fileSize || 0;
      if (fileSize < 1024) {
        return fileSize + 'B';
      } else if (fileSize < (1024 * 1024)) {
        var temp = fileSize / 1024;
        temp = temp.toFixed(2);
        return temp + 'KB';
      } else if (fileSize < (1024 * 1024 * 1024)) {
        var temp = fileSize / (1024 * 1024);
        temp = temp.toFixed(2);
        return temp + 'MB';
      } else {
        var temp = fileSize / (1024 * 1024 * 1024);
        temp = temp.toFixed(2);
        return temp + 'GB';
      }
    },
    initTableM2() {
      //明细
      var that = this;
      const tableRef = 'table-m2';
      const tableOption = {
        vue: this,
        tableRef: tableRef,
        tableName: 'user_fileLibrary_propertiesDialog_list-m2',
        columns: [
          {
            type: 'checkbox',
            fixed: 'left',
            width: 48,
            resizable: false
          },
          {
            title: this.$t1('关联对象编码'),
            field: 'objSn',
            width: 200
          },
          {
            title: this.$t1('关联对象名称'),
            field: 'objName',
            width: 200
          },
          {
            width: 50,
            fixed: 'right',
            title: '',
            sortable: false
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption2 = opts;
      });
    }
  }
};
export default modules;
