<template>
<div style="padding:24px;overflow-y:hidden;">
  <el-tabs v-model="activeName">
        <el-tab-pane label="" name="zhifa">
            <div class="flex-column">
              
              <el-row style="overflow-y: hidden;">
              
                <el-col :span="12" style="padding: 20px;border: 1px silver solid;margin-left: 10px">
                  
                  <el-form ref="form" :model="form" label-width="80px" size="mini">
                    <el-form-item label="当前版本">
                      <span style="font-size:15px;font-weight:bold;color:#000;">{{currentAppInfo.edition}}</span>
                    </el-form-item>

                    <el-form-item label="版本号">
                      <el-input v-model="form.edition"></el-input>
                    </el-form-item>
                    <el-form-item label="项目名称">
                      <el-input v-model="form.project_name"></el-input>
                    </el-form-item>

                    <el-form-item size="mini">
                      <el-upload
                      size="mini"
                          class="upload-demo"
                          action="#"
                          drag
                          :limit="limit"
                          :on-preview="handlePreview"
                          :file-list="fileList"
                          :auto-upload="false"
                          :before-upload="beforeUpload"
                          :on-remove="handleRemove"
                          :on-exceed="handleExceed"
                          :on-change="handleChange"
                          :on-success="handleSuccess"
                          :on-error="handleError"
                      >
                        <i class="el-icon-upload" style="font-size:60px;line-height:0px;"></i>
                        <div class="el-upload__text">将APP文件拖到此处，或<em>点击上传</em></div>
                      </el-upload>
                    </el-form-item>
                    <el-form-item label="描述信息">
                      <el-input type="textarea" v-model="form.description"></el-input>
                    </el-form-item>
                    <el-form-item label="是否必须更新" label-width="120px">
                      <el-radio-group v-model="form.must_update" size="medium">
                        <el-radio border :label="1">是</el-radio>
                        <el-radio border :label="0">否</el-radio>
                      </el-radio-group>
                    </el-form-item>
                    <el-form-item>
                      <el-button type="primary" @click="onSubmit">立即创建</el-button>
                    </el-form-item>
                  </el-form>
                </el-col>
              </el-row>
            </div>
        </el-tab-pane>
  </el-tabs>
</div>

</template>

<script>

import {postMethodCommon,getMethodCommon} from "../../api/common";

export default {
  name: "version",
  data() {
    return {
      currentAppInfo:{},
      activeName:"zhifa",
      limit: 1,
      fileList: [],
      form: {
        edition: "",
        description: "",
        filepath: "",
        must_update: '',
        project_name: ''
      },
      flyControlForm: {
        edition: "",
        description: "",
        filepath: "",
        must_update: '',
        project_name: ''
      },
    }
  },
  mounted(){
    this.getAppInfo();
  },
  methods: {
    getAppInfo(){
      getMethodCommon("/FireCodeApp/GetUpdateFiles",{project:'yishuixian'}).then(res=>{
        if(res.code == 200){
          this.currentAppInfo = res.data;
          this.form.project_name = res.data.project_name;
          this.form.description = res.data.description;
          this.form.must_update = res.data.must_update;
        }
      })
    },
    //文件上传时的钩子
    handlePreview(file) {

    },
    // 删除文件
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    // 文件上传成功
    handleSuccess(res, file, fileList) {
      this.$message.success('文件上传成功')
    },
    // 文件超出个数限制
    handleExceed(files, fileList) {
      this.$message.warning(`只能选择 ${this.limit} 个文件进行上传!!`)
    },
    // 文件状态改变
    handleChange(file, fileList) {
      this.form.name = file.name.split(".")[0]
      if (file) {
        this.fileList = fileList
      }
    },
    // 文件上传失败
    handleError(err, file, fileList) {
      this.$message.error('文件上传失败')
    },
    beforeUpload(file) {
      let extension = file.name.substring(file.name.lastIndexOf('.') + 1)
      let size = file.size / 1024 / 1024;
      if (extension !== 'apk') {

        this.$message.warning('只能上传后缀是.apk的文件')
        return false
      }
      if (size > 100) {
        this.$message.warning('文件大小不得超过100M')
        return false
      }
      return true
    },
    onSubmit() {
      let _this = this
      let formData = new FormData();
      this.fileList.forEach(item => {
        formData.append("files", item.raw);
      });
      if (this.fileList.length === 0) {
        this.$message.warning(`请选择上传文件!!`)
        return
      }
      let url = `/Files/Upload`
      postMethodCommon(url, formData).then(res => {
        if (res.code === 200) {
          _this.form.filepath = res.result[0].filePath
          postMethodCommon('/FireCodeApp/AddAppFiles', _this.form).then(res2 => {
            if (res2.code === 200) {
              _this.$message.warning(`上传成功`)
              _this.form = {
                edition: "",
                description: "",
                filepath: "",
                must_update: '',
                project_name: ''
              }
              _this.fileList = []
            } else {
              _this.$message.warning(`接口错误!!`)
            }
          })
        } else {
          _this.$message.warning(`接口错误!!`)
        }
      })
    },
    onFlyControlSubmit() {
      let _this = this
      let formData = new FormData();
      this.fileList.forEach(item => {
        formData.append("files", item.raw);
      });
      if (this.fileList.length === 0) {
        this.$message.warning(`请选择上传文件!!`)
        return
      }
      let url = `/Files/Upload`
      postMethodCommon(url, formData).then(res => {
        if (res.code === 200) {
          _this.flyControlForm.filepath = res.result[0].filePath
          postMethodCommon('/TaxProductEquipment/AddAppFiles', _this.flyControlForm).then(res2 => {
            if (res2.code === 200) {
              _this.$message.warning(`上传成功`)
              _this.flyControlForm = {
                edition: "",
                description: "",
                filepath: "",
                must_update: '',
                project_name: ''
              }
              _this.fileList = []
            } else {
              _this.$message.warning(`接口错误!!`)
            }
          })
        } else {
          _this.$message.warning(`接口错误!!`)
        }
      })
    }
  }
}
</script>

<style scoped>
/deep/.el-upload-dragger{
  height:100px;
  line-height: 20px;
}
/deep/ .el-icon-upload{
  
}
/deep/.el-upload__text{

}
</style>