<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="登录地址" prop="ipaddr">
        <el-input
          v-model="queryParams.ipaddr"
          placeholder="请输入登录地址"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="登录状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="登录时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >发布新版本</el-button
        >
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="list">
      <el-table-column
        label="发布时间"
        align="center"
        prop="createTime"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="版本号"
        align="center"
        width="200"
        prop="versionNumber"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="更新内容"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="下载地址"
        align="center"
        prop="path"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="是否bug" align="center" prop="isBug" width="100">
        <template slot-scope="scope">
            {{  scope.row.isBug === 0 ? '否' : '是' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNumber"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 发布新版本 -->
    <el-dialog title="发布新版本" :visible.sync="openAdd" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="24">
            <div class="upload-container" v-loading="uploadLoading" element-loading-text="上传中...">
                <el-upload
                multiple
                :action="uploadFileUrl"
                :before-upload="handleBeforeUpload"
                :limit="limit"
                :on-error="handleUploadError"
                :on-exceed="handleExceed"
                :on-success="handleUploadSuccess"
                :show-file-list="false"
                :headers="headers"
                class="upload-file-uploader"
                ref="fileUpload"
                >
                <!-- 上传按钮 -->
                <el-button size="mini" type="primary">选取文件</el-button>
                <!-- 上传提示 -->
                <div class="el-upload__tip" slot="tip" v-if="showTip">
                    请上传
                    <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
                    <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
                    的文件
                </div>
                </el-upload>
            </div>
            <div style="margin-bottom: 10px;color: #f56c6c">
            {{form.path}}
            </div>
            <el-form-item label="版本号" required>
              <el-input
                v-model="form.versionNumber"
                type="input"
                placeholder="请输入版本号"
              ></el-input>
            </el-form-item>
            <el-form-item label="更新内容" required>
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入更新内容"
              ></el-input>
            </el-form-item>
            <el-form-item label="是否bug" required>
              <el-select v-model="form.isBug" placeholder="请选择是否bug">
                <el-option
                  v-for="item in isBugList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onAdd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


     <!-- 编辑版本 -->
    <el-dialog title="修改版本" :visible.sync="openEdit" width="600px" append-to-body>
      <el-form ref="editForm" :model="editForm" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="版本号" required>
              <el-input
                v-model="editForm.versionNumber"
                type="input"
                placeholder="请输入版本号"
                disabled
              ></el-input>
            </el-form-item>
            <el-form-item label="更新内容" required>
              <el-input
                v-model="editForm.remark"
                type="textarea"
                placeholder="请输入更新内容"
              ></el-input>
            </el-form-item>
            <el-form-item label="是否bug" required>
              <el-select v-model="editForm.isBug" placeholder="请选择是否bug">
                <el-option
                  v-for="item in isBugList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onEdit">确 定</el-button>
        <el-button @click="cancelEdit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

import {
  getVersionList,
  addVersion,
  updateVersion,
  getModelVersion,
  getModelInfo
} from "@/api/version/index";

export default {
  name: "report",
  data() {
    return {
        openEdit: false,
        editForm: {},
        isBugList: [
            {
                label: '否',
                value: 0
            },
            {
                label: '是',
                value: 1
            }
        ],
        addParams: {

        },
      uploadLoading: false,
      openAdd: false,
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/file/fileUpload',
      fileList: '',
      limit: 1,
      fileSize: 10,
      fileType: ['exe'],
      showTip: false,
      headers: {
        Authorization: 'Bearer ' + getToken(),
      },
      open: false,
      form: {
        versionNumber: '',
        remark: '',
        isBug: 0,
        path: ''
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选择用户名
      selectName: "",
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: "loginTime", order: "descending" },
      // 查询参数
      queryParams: {
        pageNumber: 1,
        pageSize: 10,
        ipaddr: undefined,
        userName: undefined,
        status: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleEdit(row){
        this.openEdit = true
        this.editForm = row
    },
    onEdit(){
        updateVersion(this.addDateRange(this.editForm)).then((response) => {
            this.openEdit = false 
            this.$modal.msgSuccess("编辑成功");
            this.getList();
        });
    },
    cancelEdit(){
        this.openEdit = false
    },
    handleBeforeUpload(file) {
      console.log('file1',file);
      this.uploadLoading = true;  // 设置 loading 状态
      return true;  // 返回 true 允许上传
    },
    handleUploadSuccess(response, file, fileList) {
      this.form.path = response.data.path
      this.uploadLoading = false;
      this.$message.success('上传成功');
      this.$refs.fileUpload.clearFiles();  // 清空上传组件的文件列表
    },
    handleUploadError(error, file, fileList) {
      console.log('error3',error);
      console.log('file3',file);
      console.log('fileList3',fileList);
      this.uploadLoading = false;
      this.$message.error('上传失败,请重新上传');
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    cancel(){
        this.form = {
            versionNumber: '',
            remark: '',
            isBug: '',
            path: ''
        }
        this.openAdd = false
    },
    handleUpdateStatus(row, status){
        this.form.productId = row.productId
        this.form.type = row.type
        this.form.status = status
        if(status === 1){
            this.open = true
        }else{
            updateStatus(this.addDateRange(this.form)).then((response) => {
                this.$modal.msgSuccess("已通过");
                this.getList();
            });
        }
    },


// 确定修改
    onAdd(){
        addVersion(this.addDateRange(this.form)).then((response) => {
            this.$modal.msgSuccess("发布成功");
            this.getList();
            this.cancel()
      });
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      getVersionList(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.list = response.data;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
        this.list = [];
      });
    },
    /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.pageNumber = 1;
    //   this.getList();
    // },
    /** 重置按钮操作 */
    // resetQuery() {
    //   this.dateRange = [];
    //   this.resetForm("queryForm");
    //   this.queryParams.pageNumber = 1;
    //   this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
    // },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return deleteVersion(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 新增发布 */
    handleAdd() {
      this.openAdd = true
    },
  },
};
</script>
<style lang="scss" scoped>
.upload-container {
  position: relative;
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.upload-file-uploader {
  display: flex;
}

:deep(.el-loading-mask) {
  background-color: rgba(255, 255, 255, 0.8);
}
</style>
