<template>
  <!-- 模型日志弹窗 -->
  <div id="modelLog">
    <el-dialog title="模型日志" :visible.sync="modelLogVisible" width="40%" :before-close="handleClose">
      <div class="bodys">
        <el-button v-show="filterlogData.length == 1" class="delete-model" @click="deleteModel(filterlogData[0])"><i class="iconfont icon-delete el-icon--left"></i>删除模型</el-button>
        <el-tabs v-model="activeName" type="card" @tab-click="changeModel">
          <el-tab-pane label="上传日志" name="first">
            <el-table :data="filterlogData" stripe height="300px" style="width: 100%">
              <el-table-column label="版本">
                <template slot-scope="scope">
                  <span style="margin-right: 5px">{{ scope.row.Version }}</span>
                  <i v-show="scope.row.Version && scope.row.Status == 4" class="iconfont icon-warn" style="cursor:pointer;" title="查看版本更新信息" @click="handleClickVersion(scope.row)"></i>
                </template>
              </el-table-column>
              <el-table-column prop="RevitVersion" label="revit版本"></el-table-column>
              <el-table-column prop="AcceptTime" label="上传时间"></el-table-column>
              <el-table-column prop="UserName" label="上传人"></el-table-column>
              <el-table-column prop="address" label="操作">
                <template slot-scope="scope">
                  <el-button title="下载模型" @click="handleClick(scope.row)" v-if="!scope.row.Removed" type='primary' size="mini" class="iconfont icon-download"></el-button>
                  <!-- 需求:先不支持删除 -->
                  <!-- <el-button @click="deleteModel(scope.row)" v-if="!scope.row.Removed" type='danger' plain size="mini">删除</el-button> -->
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="工程改造" name="second"></el-tab-pane>
        </el-tabs>
      </div>
    </el-dialog>
    <version-dialog :dialogVisible.sync="dialog" :modelFile="modelFile" ref="addSpaceDialog"></version-dialog>
  </div>
</template>
<script>
import request from "@/api/model/file.js";
import versionDialog from "@/components/model/file/versionDialog";
export default {
  components: {
    versionDialog
  },
  props: {
    modelLogVisible: Boolean,
    modelFolderName: String,
    logData: Array
  },
  data() {
    return {
      dialogVisible: false, //是否显示该弹窗
      activeName: "first", //默认选择上传日志
      tableData: [], //列表数据
      dialog: false,
      modelFile: null,
    };
  },
  computed: {
    filterlogData: function() {
      let newLogData = [];
      if (this.logData.length) {
        this.logData.forEach(item => {
          newLogData.push(item);
        });
      }
      return newLogData
    }
  },
  methods: {
    // 查看版本信息
    handleClickVersion(item) {
      this.modelFile = item;
      this.dialog = true;
    },
    handleClose(done) {
      this.$emit("CloseModelLogDia");
    },
    handleClick(item) {
      console.log(item);
      let url = item.Url.match(/(\/image-service\S*)$/g)?item.Url.match(/(\/image-service\S*)$/g)[0]:''
      if (item.Url) {
        let a = document.createElement("a");
        a.href = url;
        a.download =`${this.modelFolderName}${item.FloorName}模型文件v${item.Version}.rvt`;
        a.click();
      } else {
        this.$message({
          message: "该文件夹下没有资源",
          type: "error"
        });
      }
    },
    changeModel() {},
    /**
     * 删除模型
     *
     */
    deleteModel(item) {
      this.$confirm(
        "此操作只可删除楼层模型文件。已经识别的对象及关系等，暂不可删除。确定删除？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          request.deleteModelFileList({Id: item.Id}, res => {
            if (res.Result == "success") {
              //  刷新页面
              this.$emit("deleteFinish");
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            } else {
              this.$message({
                type: "error",
                message: res.Result
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>
<style lang="less" scoped>
#modelLog {
  .bodys {
    position: relative;
    padding-bottom: 30px;
    .delete-model {
      position: absolute;
      right: 0;
      top: 0;
      z-index: 10;
    }
  }
}
</style>

