<template>
  <div class="upgradationBox">
    <div class="uploadForm">
      <div class="upgradeBox">
        <h3 class="lableBox">文件上传：</h3>
        <el-upload
          class="upload-demo"
          ref="upload"
          action="#"
          :before-upload="beforeUpload"
          :http-request="customUpload"
          :limit="1"
          :file-list="fileList"
          :data="reportData"
          :auto-upload="false"
        >
          <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="uploadDate"
          label="创建时间"
          width="180"
        ></el-table-column>
        <el-table-column prop="name" label="文件名称">
          <template slot-scope="scope">
            <span
              @dblclick="handleDblClick(scope.row.path)"
              style="cursor: pointer"
            >
              {{ scope.row.name }}</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="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 {
  listApk,
  uploadApk,
  updateApkApi,
  deleteApkApi,
} from "@/utils/api/index";

export default {
  name: "deviceUpgrade",
  components: {},
  data() {
    return {
      //上传apk notesVal
      fileList: [],
      reportData: {
        title: "",
      },
      fileData: [], //数据列表
      fileloading: false,
      editShow: false,
      titleShow: false,
      ruleForm: {
        notesNewVal: "",
      },
      rules: {
        notesNewVal: [
          { required: true, message: "请输入备注信息", trigger: "blur" },
        ],
      },
      rowData: "", //选中数据
      progress: 0,
      progressLoading: false,
    };
  },
  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); // 或者你可以使用其他方式来显示进度，比如使用进度条组件等。
    },
    beforeUpload(file) {
      const fileName = file.name;
      console.log(fileName);
      if (fileName.length > 19) {
        this.$message.error("文件名长度不能超过 19 个字符！");
        return false; // 阻止文件自动上传
      }
      return true; // 允许文件继续上传
    },
    // //点击升级确定
    // beforeAvatarUpload(file) {
    //   console.log(this.reportData.title);
    //   console.log(file);
    // },
    //上传apk
    submitUpload() {
      //console.log();
      this.$refs.upload.submit();
    },
    customUpload(options) {
      console.log(options);
      const formData = new FormData();
      formData.append("file", options.file);
      formData.append("title", this.reportData.title); // 假设后端需要这个字段
      console.log(formData);
      uploadApk(formData, (progressEvent) => {
        // 计算上传进度百分比
        const percentCompleted = Math.round(
          (progressEvent.loaded * 100) / progressEvent.total
        );
        this.progressLoading = true;
        // 更新UI或执行其他逻辑，比如显示进度条
        console.log(`上传进度: ${percentCompleted}%`);
        this.progress = Math.round(percentCompleted);

        // 你可以通过其他方式将进度信息传递给组件或Vue实例
        // this.uploadProgress = percentCompleted; // 假设你有一个data属性来存储进度
      })
        .then((res) => {
          console.log(res);
          // this.progressLoading = true;
          if (res.code == 200) {
            this.$message({
              duration: 1500,
              showClose: true,
              message: "文件上传成功",
              type: "success",
            });
            this.reportData.title = "";
            this.$refs.upload.clearFiles();
            this.getUpgradeList();
            this.progressLoading = false;
          } else {
            this.$message({
              duration: 1500,
              showClose: true,
              message: res.msg,
              type: "error",
            });
            this.reportData.title = "";
            this.$refs.upload.clearFiles();
          }
        })
        .catch((error) => {});
    },
    // handleAvatarSuccess(res, file) {
    //   this.progressLoading = false;
    //   console.log(res);
    //   if (res.code == 200) {
    //     this.$message({
    //       duration: 1500,
    //       showClose: true,
    //       message: "文件上传成功",
    //       type: "success",
    //     });
    //     this.reportData.title = "";
    //     this.$refs.upload.clearFiles();
    //     this.getUpgradeList();
    //   } else {
    //     this.$message({
    //       duration: 1500,
    //       showClose: true,
    //       message: res.msg,
    //       type: "error",
    //     });
    //     this.reportData.title = "";
    //     this.$refs.upload.clearFiles();
    //   }
    // },
    getUpgradeList() {
      this.fileloading = true;
      listApk({
        pageindex: 1,
        pagesize: 2000,
      })
        .then((res) => {
          console.log(res);
          this.fileData = res.data.list;
          this.fileloading = false;
        })
        .catch((err) => {});
    },
    handleDeleteClick(row) {
      deleteApkApi({
        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);
          updateApkApi({
            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">
.upgradationBox {
  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;
  }
  .uploadForm {
    .el-button--default,
    .el-button--primary {
      width: auto;
    }
  }
  .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-table::before {
    height: 1px !important;
  }
}
.el-dialog__headerbtn {
  top: 18px;
  .el-dialog__close {
    font-size: 26px;
    &:hover {
      background: #e2e2e2;
    }
  }
}
</style>
