import {selectDialogMixins} from '@base/mixins/selectDialog/index.js';
import ElImageViewer from '@base/components/VabUpload/image-viewer';

let modules;
modules = {
  name: 'fileHistoryDialog',
  props: ['visiable', 'multi', 'rows', 'param'],
  inject: ['current_prefix', 'storeAreaCode'],
  mixins: [selectDialogMixins],
  components: {ElImageViewer},
  created() {
    this.initSetting();
    this.selectMulti = true;
  },
  mounted() {
    this.initTableM1();
  },
  data() {
    var that = this;
    return {
      showDialog: true,
      falseValue: false,
      selectMulti: true,
      formData: {},
      vxeOption: {},
      showViewer: false,
      chooseIndex: 0,
      imageDTOs: []
    };
  },
  methods: {
    initTableM1() {
      let that = this;
      let tableOption = {
        vue: that,
        tableRef: 'table-m1',
        tableName: 'user_fileHistoryDialog-m1',
        path: this.current_prefix + '/file_obj/findHistoryPage',
        param: () => {
          return {
            parent: this.$attrs.currentFile.parent,
            storeAreaCode: this.$attrs.currentFile.storeAreaCode,
            attachCode: this.$attrs.currentFile.attachCode,
            ...this.formData
          };
        },
        columns: [
          {type: 'checkbox', fixed: 'left', width: 48, resizable: false},
          {
            title: this.$t2('文件名称', 'components.fileLibrary.fileName'),
            field: 'fileName',
            width: 250,
            fixed: 'left',
            slots: {
              default: ({row, $rowIndex}) => {
                let h = this.$createElement;
                let isPicture = that.$commonFileUtil.isPictureFile(row.url);
                let imageUrl = row.domain + (row.thumbnail ? row.thumbnail : row.url);
                let showUrl = that.$commonFileUtil.getShowUrl(imageUrl);
                return [
                  h('a', {
                    staticClass: "a-link img",
                    on: {
                      click: (event) => {
                        event.stopPropagation();
                        if (isPicture) {
                          let pictureDtoList = that.$refs['table-m1'].getTableData().fullData.filter(item => {
                            return this.$commonFileUtil.isPictureFile(item.url)
                          });

                          that.imageDTOs = pictureDtoList;
                          that.chooseIndex = pictureDtoList.findIndex(item => item.id == row.id);
                          that.showViewer = true;
                        } else {
                          that.$commonFileUtil.downloadFile(row.domain + row.url, row.name)
                        }
                      }
                    }
                  }, [
                    h('el-tooltip', {
                      props: {
                        enterable: false,
                        effect: "dark",
                        content: this.$t2('预览', 'components.fileLibrary.preview'),
                        placement: "top",
                        "popper-class": "tooltip-skin"
                      }
                    }, [
                      h('el-image', {
                        props: {
                          src: showUrl
                        }
                      })
                    ])
                  ]),
                  h('span', row.name)
                ];
              }
            }
          },
          {title: this.$t2('版本号', 'components.fileLibrary.fileRev'), field: 'rev', width: 150},
          {title: this.$t2('创建人', 'components.fileLibrary.createBy'), field: 'createBy', width: 150},
          {title: this.$t2('创建时间', 'components.fileLibrary.createDate'), field: 'createDate', width: 150},
          {
            width: 47,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: ({row, $rowIndex, $table}) => {
                let h = this.$createElement;
                return [
                  h('a', {
                    staticClass: "a-link",
                    on: {
                      click: (event) => {
                        event.stopPropagation();
                        this.reverByHistory(row);
                      }
                    }
                  }, [
                    h('el-tooltip', {
                      props: {
                        enterable: false,
                        effect: "dark",
                        content: this.$t2('还原', 'components.fileLibrary.revert'),
                        placement: "top",
                        "popper-class": "tooltip-skin"
                      }
                    }, [
                      h('i', {
                        staticClass: "el-icon-refresh-left"
                      })
                    ])
                  ])
                ];
              }
            }
          }
        ],
        config: {
          checkboxConfig: {
            checkStrictly: true,
            showHeader: this.selectMulti,
            trigger: 'row'
          }
        }
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        that.vxeOption = opts;
      });
    },
    dialogClose() {
      this.$emit("update:visiable", false);
    },
    reverByHistory(row) {
      this.$baseConfirm(this.$t2('您确定要还原吗？', 'components.fileLibrary.revertConfirmTip')).then(() => {
        this.$http({
          method: 'post',
          url: this.current_prefix + '/file_attach/revert',
          data: row,
          isLoading: true,
          success: res => {
            this.$message({
              message: res.content,
              type: 'success',
              duration: 500,
            });
            this.dialogClose();
            this.$emit('confirm', res.objx);
          }
        });
      });
    }
  }
};
export default modules;
