<template>
  <div class="upgradationOta">
    <div class="uploadForm">
      <div class="upgradeBox">
        <h3 class="lableBox">文件上传：</h3>
        <el-upload
          class="upload-demo"
          ref="upload"
          action="/web/api/uploadFile.php"
          :limit="1"
          :file-list="fileList"
          :data="reportData"
          :on-progress="handleProgress"
          :auto-upload="false"
          :on-success="handleAvatarSuccess"
          @before-upload="beforeAvatarUpload"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
        </el-upload>
      </div>
      <div class="noteBox">
        <h3 class="lableBox">文件备注：</h3>
        <el-input
          class="noteClass"
          v-model="reportData.title"
          placeholder="输入备注"
        ></el-input>
        <el-button
          class="uploadBtn"
          size="small"
          type="primary"
          @click="submitUpload"
          >上传到服务器</el-button
        >
      </div>
    </div>
    <div class="uploadPressMask" v-if="progressLoading">
      <div class="centerClass">
        <el-progress type="circle" :percentage="progress"></el-progress>
      </div>
    </div>
    <div class="uploadList">
      <el-table
        :data="fileData"
        style="width: 100%"
        border
        stripe
        height="calc(100% - 0px)"
        v-loading="fileloading"
      >
        <el-table-column type="index" width="50" label="序号">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="180"
        ></el-table-column>
        <el-table-column prop="fileName" label="文件名称">
          <template slot-scope="scope">
            <span
              @dblclick="handleDblClick(scope.row.path)"
              style="cursor: pointer"
            >
              {{ scope.row.fileName }}</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="path" label="文件路径">
          <template slot-scope="scope">
            <span
              @dblclick="handleDblClick(scope.row.path)"
              style="cursor: pointer"
            >
              {{ scope.row.path }}</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="fileSize" label="文件大小"> 
          <template slot-scope="scope">
            {{ scope.row.fileSize | changeType }}
          </template>
        </el-table-column>
        <el-table-column prop="title" label="备注"> </el-table-column>
        <!-- <el-table-column prop="path" label="文件路径"> </el-table-column> -->
        <el-table-column label="操作" width="300" class-name="editClass">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="handleEditClick(scope.row)"
              size="small"
              icon="el-icon-edit"
              >修改</el-link
            >
            <el-link
              type="danger"
              @click="handleDeleteClick(scope.row)"
              size="small"
              icon="el-icon-delete"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      class="titleDialog"
      title="修改备注"
      :visible.sync="titleShow"
      :close-on-click-modal="false"
      width="480px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="文件备注" prop="notesNewVal">
          <el-input v-model="ruleForm.notesNewVal"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="confirmClick">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  postuploadFileApi,
  getqueryUpgradesApi,
  getupdUpgradeApi,
} from "@/utils/api/index";

export default {
  name: "upgradationOta",
  components: {},
  data() {
    return {
      //上传apk notesVal
      fileList: [],
      reportData: {
        title: "",
        upgradeType:1,
      },
      fileData: [], //数据列表
      fileloading: false,
      editShow: false,
      titleShow: false,
      ruleForm: {
        notesNewVal: "",
      },
      rules: {
        notesNewVal: [
          { required: true, message: "请输入备注信息", trigger: "blur" },
        ],
      },
      rowData: "", //选中数据
      progress: 0,
      progressLoading: false,
    };
  },
  filters: {
    changeType(val) {
      if (val == "0") return "0B";
      var k = 1024;
      var sizes = ["B", "KB", "MB", "GB", "TB"];

      let i = Math.floor(Math.log(val) / Math.log(k)); //得出该数字的单位应该是kB?MB
      return (val / Math.pow(k, i)).toPrecision(3) + "" + sizes[i];
    },
  },
  computed: {},
  mounted() {
    this.getUpgradeList();
  },
  methods: {
    //双击选中复制
    handleDblClick(val) {
      // alert(val);
      var input = document.createElement("input"); // 创建input对象
      input.value = val; // 设置复制内容
      document.body.appendChild(input); // 添加临时实例
      input.select(); // 选择实例内容
      document.execCommand("Copy"); // 执行复制
      document.body.removeChild(input); // 删除临时实例
      //this.$message.success("复制成功！");
      this.$message({
        duration: 1500,
        showClose: true,
        message: "复制成功！" + val,
        type: "success",
      });
    },
    //上传进度
    handleProgress(event, file, fileList) {
      this.progressLoading = true;
      // 获取上传进度百分比
      if (event.percent == 100) {
        this.progress = 98;
      } else {
        this.progress = Math.round(event.percent);
      }

      // 显示上传进度
      console.log(this.progress); // 或者你可以使用其他方式来显示进度，比如使用进度条组件等。
    },
    //点击升级确定
    beforeAvatarUpload(file) {
      console.log(this.reportData.title);
      console.log(file);
    },
    //上传apk
    submitUpload() {
      //console.log();
      this.$refs.upload.submit();
    },
    handleAvatarSuccess(res, file) {
      this.progressLoading = false;
      console.log(res);
      this.$message({
        duration: 1500,
        showClose: true,
        message: "文件上传成功",
        type: "success",
      });
      this.reportData.title = "";
      this.$refs.upload.clearFiles();
      this.getUpgradeList();
    },
    getUpgradeList() {
      this.fileloading = true;
      getqueryUpgradesApi()
        .then((res) => {
          console.log(res);
          this.fileData = res.data.filter(item => item.type === "1");
          this.fileloading = false;
        })
        .catch((err) => {});
    },
    handleDeleteClick(row) {
      getupdUpgradeApi({
        act: "del",
        id: row.id,
      })
        .then((res) => {
          console.log(res);
          this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.$message({
                duration: 1500,
                showClose: true,
                message: "文件删除成功",
                type: "success",
              });
              this.getUpgradeList();
            })
            .catch(() => {});
        })
        .catch((err) => {});
    },
    handleEditClick(row) {
      console.log(row);
      this.titleShow = true;
      this.ruleForm.notesNewVal = row.title;
      this.rowData = row;
    },
    confirmClick() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          console.log(this.ruleForm.notesNewVal);
          getupdUpgradeApi({
            act: "edt",
            id: this.rowData.id,
            title: this.ruleForm.notesNewVal,
          })
            .then((res) => {
              console.log(res);
              this.$message({
                duration: 1500,
                showClose: true,
                message: "修改备注成功",
                type: "success",
              });
              this.titleShow = false;
              this.getUpgradeList();
            })
            .catch((err) => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="less">
.upgradationOta {
  height: calc(100% - 24px);
  width: calc(100% - 24px);
  padding: 12px;
  .upgradeBox {
    height: 32px;
    line-height: 32px;
    display: flex;
    margin-bottom: 16px;
    // align-items: center;
    // flex-direction: column;
    .lableBox {
      font-size: 14px;
      font-weight: normal;
      width: 78px;
    }

    .upload-demo {
      position: relative;
      display: flex;
      flex-direction: row-reverse;
      margin-right: 32px;
      .el-upload-list {
        width: 320px;
        height: 32px;
        line-height: 32px;
        border: 1px solid #ddd;
        margin-right: 12px;
        border-radius: 4px;
        .el-upload-list__item {
          transition: none;
          font-size: 14px;
          color: #606266;
          position: relative;
          box-sizing: border-box;
          border-radius: 4px;
          width: 100%;
          height: 32px;
          line-height: 32px;
          margin-top: 0px !important;
          .el-icon-close {
            top: 10px;
          }
        }
      }
    }
  }
  .noteBox {
    height: 32px;
    line-height: 32px;
    display: flex;
    margin-bottom: 16px;
    .noteClass {
      width: 322px;
    }
    .lableBox {
      font-size: 14px;
      font-weight: normal;
      width: 78px;
    }
  }
  .uploadBtn {
    margin-left: 12px;
  }
  .uploadList {
    margin-top: 20px;
    height: calc(100% - 100px);
    .el-table {
      .el-table__cell {
        text-align: center;
      }
    }
  }
  .editClass {
    .el-link.el-link--primary {
      margin-right: 14px;
    }
  }
  .uploadPressMask {
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: hsla(0, 0%, 100%, 0.9);
    margin: 0;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 3;
    .centerClass {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
    }
  }
}
.el-dialog__headerbtn {
  top: 18px;
  .el-dialog__close {
    font-size: 26px;
    &:hover {
      background: #e2e2e2;
    }
  }
}
</style>
