<template>
  <div class="app-container">
    <el-radio-group v-model="queryParams.type" style="margin-bottom: 30px" :size="ELsize" @change="handleQuery()">
      <el-radio-button :label="0">安卓</el-radio-button>
      <el-radio-button :label="1">ios</el-radio-button>
    </el-radio-group>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" :size="ELsize" @click="goNew({}, '新增')">新增版本</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list" row-key="id" :border="TableBorder" :size="ELTableSize"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="{$index}">
          {{ $getTableIndex(queryParams, $index + 1) }}
        </template>
      </el-table-column>
      <el-table-column label="APP名称" prop="name" width="400" align="center" />
      <el-table-column label="客户端" prop="projectCategorySort" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.type === 0">安卓</el-tag>
          <el-tag v-if="row.type === 1" type="success">IOS</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="版本号" prop="version" align="center" />
      <el-table-column label="应用版本号" prop="level" align="center" />
      <el-table-column label="强制更新" prop="status" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="更新描述" prop="description" show-overflow-tooltip align="center" />
      <el-table-column label="下载地址" prop="downloadLink" show-overflow-tooltip align="center" />
      <el-table-column label="更新时间" prop="createTime" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button :size="ELTableSize" type="text" @click="goNew(scope.row, '查看')">查看</el-button>
          <el-button :size="ELTableSize" type="text" icon="el-icon-edit" @click="goNew(scope.row, '编辑')">编辑</el-button>
          <el-button :size="ELTableSize" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-drawer :title="title + '新建版本'" :visible.sync="drawer" :before-close="handleClose">
      <el-form class="form" ref="form" :rules="rules" :model="form" label-width="120px" :size="ELsize">
        <el-form-item label="APP名称:" prop="name">
          <el-input v-model="form.name" :disabled="title == '查看'" placeholder="请输入APP名称" clearable
            maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="客户端:" prop="type">
          <el-radio v-model="form.type" :disabled="title == '查看'" :label="0">安卓</el-radio>
          <el-radio v-model="form.type" :disabled="title == '查看'" :label="1">IOS</el-radio>
        </el-form-item>
        <el-form-item label="版本号（V）:" prop="version">
          <el-input v-model="form.version" :disabled="title == '查看'" placeholder="请输入，仅需输入数字，不需要输入V" clearable
            maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="应用版本号:" prop="level">
          <el-input-number v-model="form.level" style="width: 100%" :disabled="title == '查看'" :min="1" :step="1"
            step-strictly placeholder="请输入，仅需输入数字" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="强制更新:" prop="status">
          <el-switch v-model="form.status" :disabled="title == '查看'"> </el-switch>
        </el-form-item>
        <el-form-item label="更新描述:" prop="description">
          <el-input type="textarea" :rows="3" :disabled="title == '查看'" placeholder="请输入更新描述" clearable
            v-model="form.description"> </el-input>
        </el-form-item>
        <el-form-item label="下载地址:" prop="downloadLink">
          <el-input v-model="form.downloadLink" :disabled="title == '查看'" placeholder="请输入下载地址"></el-input>
          <fileUpload v-model="form.downloadLink" v-if="title !== '查看'" :fileType="null" :limit="1" :fileSize="500" />
        </el-form-item>
        <el-form-item>
          <el-button v-if="title !== '查看'" type="primary" :loading="btnLoading" @click="onSubmit">确定</el-button>
          <el-button @click="handleClose()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { versionAdd, versionPage, versionInfo, versionEdit, versionDel } from "@/api/versions.js";
export default {
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 遮罩层
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 0,
      },
      list: [],
      tabPosition: "Android",
      loading: false,
      drawer: false,
      title: "新增",
      rules: {
        name: [{ required: true, message: "请输入APP名称", trigger: "change" }],
        type: [{ required: true, message: "请选择客户端", trigger: "change" }],
        version: [{ required: true, message: "请输入版本号（V）", trigger: "change" }],
        level: [{ required: true, message: "请输入应用版本号", trigger: "change" }],
        status: [{ required: true, message: "请选择是否强制更新", trigger: "change" }],
        description: [{ required: true, message: "请输入更新描述", trigger: "change" }],
        downloadLink: [{ required: true, message: "请输入下载地址或上传文件", trigger: "change" }],
      },
      form: {
        status: false,
      },
      btnLoading: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    goNew(row = {}, title) {
      this.drawer = true;
      this.title = title;
      if (row.id) {
        this.getDetails(row.id);
      }
    },
    handleClose() {
      this.form = {
        status: false,
      };
      this.resetForm("form");
      this.drawer = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getDetails(id) {
      versionInfo(id).then((res) => {
        this.form = res.data;
      });
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let apiFun = this.form.id ? versionEdit : versionAdd;
          apiFun(this.form)
            .then((res) => {
              this.$message.success(this.form.id ? "编辑成功！" : "新增成功！");
              this.getList();
              this.handleClose();
            })
            .finally((e) => {
              this.btnLoading = false;
            });
        }
      });
    },
    // 获取列表
    getList() {
      this.loading = true;
      versionPage(this.queryParams).then((response) => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return versionDel(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-drawer__body {
  padding-left: 2%;
  padding-right: 10%;
}

::v-deep .el-input.is-disabled .el-input__inner,
::v-deep .el-textarea.is-disabled .el-textarea__inner {
  background-color: #fff;
  color: rgb(96, 98, 102);
}

::v-deep .el-input-number>span {
  display: none;
}

::v-deep .el-input-number--medium .el-input__inner {
  padding: 0 15px;
  text-align: left;
}

::v-deep .el-input.is-disabled .el-input__inner,
::v-deep .el-textarea.is-disabled .el-textarea__inner,
::v-deep .el-range-editor.is-disabled input,
::v-deep .el-radio__input.is-disabled+span.el-radio__label {
  color: #606266;
}
</style>