<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="版本号" prop="versionCode">
        <el-input v-model="queryParams.versionCode" type="number" placeholder="请输入版本号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="版本名称" prop="versionName">
        <el-input v-model="queryParams.versionName" placeholder="请输入版本名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </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" @click="handelAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listData" row-key="id">

      <el-table-column prop="versionCode" label="版本号"></el-table-column>
      <el-table-column prop="versionName" label="版本名字" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="upgradeDesn" label="升级描述"></el-table-column>
      <el-table-column prop="versionUrl" label="版本地址"></el-table-column>
      <el-table-column prop="status" label="是否发布" align="center" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="changeStatus(scope.row)"> </el-switch>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit"
      @pagination="getList" />

   <!-- 添加或修改机构对话框 -->
    <el-dialog :beforeClose="cancel" :title="title" :visible.sync="open" width="700px" append-to-body destroy-on-close>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item prop="versionCode" label="版本号">
          <el-input v-model.trim="form.versionCode" placeholder="请输入版本号" maxlength="20" />
        </el-form-item>
        <el-form-item prop="versionName" label="版本名称">
          <el-input v-model.trim="form.versionName" placeholder="请输入版本名称" maxlength="20" />
        </el-form-item>
        <el-form-item prop="upgradeDesn" label="升级描述">
          <el-input v-model.trim="form.upgradeDesn" type="textarea" placeholder="请输入升级描述" maxlength="20" />
        </el-form-item>
        <el-form-item prop="versionUrl" label="版本地址">
          <FileUpload ref="fileUpload" :fileSize="100" :fileType="['apk']" :paramsData="{ dirName: 'app-version' }" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listData,save } from "@/api/system/version";


export default {
  name: "version_list",
  dicts: [],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      listData: [],
      // 弹出层标题
      title: "",
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
      },
      form:{},
      rules:{},
      open:false,
      rules: {
        versionCode: [{ required: true, message: "版本号不能为空" }],
        versionName: [{ required: true, message: "版本名称不能为空" }],
        upgradeDesn: [{ required: true, message: "升级描述不能为空" }],
      }

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询机构列表 */
    getList() {
      this.loading = true;
      listData(this.queryParams).then(response => {
        this.listData = response.data.records
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.$refs["form"].resetFields()
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handelAdd(){
      this.form={}
      this.title='添加',
      this.open=true
    },
    changeStatus(item){
      save(item).then(res=>{
        this.$modal.msgSuccess('修改成功');
        this.getList()
     })
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {

          this.form.versionUrl = this.$refs.fileUpload.fileList.length != 0 ? this.$refs.fileUpload.fileList[0].name : catchForm.versionUrl

          save(this.form).then(res=>{
            this.$modal.msgSuccess(this.form.id ? '修改成功' : '添加成功');
            this.open = false
            this.getList()
          })
        }
      });
      
    }

  }
};
</script>
