<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:appVersion:add']"
        >发布版本</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:appVersion:remove']"
        >删除</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  height="500" border v-loading="loading" :data="appVersionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="versionId" />
      <el-table-column label="版本名称" align="center" prop="versionName" />
      <el-table-column label="版本CODE" align="center" prop="versionCode" />
      <el-table-column label="更新描述" align="center" prop="note" />
      <el-table-column label="下载地址" align="center" prop="url" />
      <el-table-column prop="type" label="APP类型" align="center" >
        <template slot-scope="scope">
          <span v-if="scope.row.type==1">安卓</span>
          <span v-if="scope.row.type==2">苹果</span>
        </template>
      </el-table-column>
      <el-table-column prop="isForce" label="强制更新" align="center" >
        <template slot-scope="scope">
          <span v-if="scope.row.isForce==1">是</span>
          <span v-if="scope.row.isForce==0">否</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:appVersion:remove']"
          >删除</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-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="版本名称" prop="versionName">
          <el-input v-model="form.versionName" placeholder="请输入版本名称" />
        </el-form-item>
        <el-form-item label="版本CODE" prop="versionCode">
          <el-input-number v-model="form.versionCode"   :min="1"  label="请输入版本CODE"></el-input-number>
        </el-form-item>
        <el-form-item label="更新描述" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="安装包上传" prop="url">
          <el-upload
            accept=".apk"
            class="upload-demo"
            :action="uploadUrl"
            :before-upload="beforeUpload"
            :on-success="handleSuccess"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传APK文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="APP类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio key="1" label="1" >安卓</el-radio>
            <el-radio key="2" label="2" >苹果</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="强制更新" prop="isForce">
          <el-radio-group v-model="form.isForce">
            <el-radio key="1" label="1" >是</el-radio>
            <el-radio key="0" label="0" >否</el-radio>
          </el-radio-group>
        </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 { listAppVersion, getAppVersion, delAppVersion, addAppVersion, updateAppVersion, exportAppVersion } from "@/api/store/appVersion";

export default {
  name: "AppVersion",
  data() {
    return {
      myloading:null,
      fileList:[],
      uploadUrl:process.env.VUE_APP_BASE_API+"/common/uploadOSS",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安卓版本表格数据
      appVersionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        versionName: null,
        versionCode: null,
        note: null,
        url: null,
        type: null,
        isForce: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        versionName: [
          { required: true, message: "版本名不能为空", trigger: "blur" }
        ],
        versionCode: [
          { required: true, message: "版本CODE不能为空", trigger: "blur" }
        ],
        url: [
          { required: true, message: "上传地址不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "APP类型不能为空", trigger: "blur" }
        ],
        isForce: [
          { required: true, message: "是否强制更新不能为空", trigger: "blur" }
        ],
        note: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    beforeUpload(file){
        this.myloading = this.$loading({
          lock: true,
          text: '上传中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
    },
    handleSuccess(res, file) {
        this.myloading.close();
        if(res.code==200){
          this.form.url=res.url;
        }
        else{
          this.msgError(res.msg);
        }

    },
    handleRemove(file, fileList) {
      console.log(this.fileList);
      this.form.url=null;

    },
    handlePreview(file) {

      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {

      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    /** 查询安卓版本列表 */
    getList() {
      this.loading = true;
      listAppVersion(this.queryParams).then(response => {
        this.appVersionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        versionId: null,
        versionName: null,
        versionCode: null,
        note: null,
        url: null,
        type: "1",
        isForce: "1",
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.versionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "发布版本";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const versionId = row.versionId || this.ids
      getAppVersion(versionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改安卓版本";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.versionId != null) {
            updateAppVersion(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addAppVersion(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const versionIds = row.versionId || this.ids;
      this.$confirm('是否确认删除安卓版本编号为"' + versionIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAppVersion(versionIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有安卓版本数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAppVersion(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
