<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="版本号" prop="version">
            <el-input
              v-model="queryParams.version"
              placeholder="请输入参数名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="18">
          <el-form-item label-width="0" class="text-right">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </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>
    </el-row>

    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="更新内容" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="APP版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本" />
        </el-form-item>

        <el-form-item label="下载地址" prop="address">
          <!-- <el-input v-model="form.address" placeholder="请输入下载地址" /> -->
          <EimFileUpload
            v-model="addressList"
            upload-url="/admin-api/minio/files/uploadFiles"
            :file-type="['apk', 'pdf', 'png', 'jpg', 'jpeg']"
            :file-size="200"
          />
        </el-form-item>
        <el-form-item label="更新内容" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入更新内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryAppNewVersionPage,
  saveAppVersion,
  getAppNewVersion,
} from "@/api/infra/appManage";
import EimFileUpload from "@/components/EimFileUpload";

export default {
  name: "AppManage",
  components: {
    EimFileUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        version: null,
        address: null,
      },
      addressList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // address: [
        //   { required: true, message: '下载地址不能为空', trigger: 'blur' }
        // ],
        version: [
          { required: true, message: "版本号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      queryAppNewVersionPage(this.queryParams)
        .then((response) => {
          this.list = response.data.list || [];
          this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        key: undefined,
        value: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAppNewVersion(id).then((response) => {
        this.form = response.data;
        this.addressList.push({ url: this.form.address });
        console.log(this.addressList);
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            this.form.address = this.addressList[0]?.url;
            saveAppVersion(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.address = this.addressList[0]?.url;
            saveAppVersion(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
