<!-- 应用版本列表 -->
<template>
  <div class="app-container applyListVersions">
    <div class="page-container applyListVersionsPage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="appName" label-width="90px">
            <el-input
              maxLength="30"
              v-model="queryParams.appName"
              placeholder="请输入应用名"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="versionName" label-width="90px">
            <el-input
              maxLength="30"
              v-model="queryParams.versionName"
              placeholder="请输入版本号"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="terminalType">
            <el-select v-model="queryParams.terminalType" placeholder="请选择类型" style="width: 200px">
              <el-option label="化学品终端" value="aio_chemical"></el-option>
              <el-option label="电子信息牌" value="aio_infobord"></el-option>
              <el-option label="学习考试一体" value="aio_exam"></el-option>
              <el-option label="化学品终端升级APP" value="aio_chemical_update"></el-option>
              <el-option label="电子信息牌升级APP" value="aio_infobord_update"></el-option>
              <el-option label="学习考试一体升级APP" value="aio_exam_update"></el-option>
            </el-select>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="tableButton(1)"
             v-hasPermiRouter="['demo:demo:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList">
          <el-table-column label="应用名" prop="appName"  show-overflow-tooltip/>
          <el-table-column label="版本号" prop="versionCode" width="100" show-overflow-tooltip/>
          <el-table-column label="版本名称" prop="versionName" width="140" show-overflow-tooltip/>
          <el-table-column label="类型" align="center" prop="terminalType" show-overflow-tooltip width="200">
            <template slot-scope="scope">
              {{scope.row.terminalType=='aio_chemical'?'化学品终端':(
              scope.row.terminalType=='aio_infobord'?'电子信息牌':(
              scope.row.terminalType=='aio_exam'?'学习考试一体':(
              scope.row.terminalType=='aio_chemical_update'?'化学品终端升级APP':(
              scope.row.terminalType=='aio_infobord_update'?'电子信息牌升级APP':(
              scope.row.terminalType=='aio_exam_update'?'学习考试一体升级APP':scope.row.terminalType)))))}}
            </template>
          </el-table-column>
          <el-table-column label="大小" prop="fileSize" width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.fileSize?scope.row.fileSize+'MB':''}}</span>
            </template>
          </el-table-column>
          <el-table-column label="升级说明" prop="releaseNotes" width="260" show-overflow-tooltip/>
          <el-table-column label="创建时间" prop="createTime" width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="140" show-overflow-tooltip>
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <el-dialog :title="dialogForm.versionId?'编辑':'新增'" class="applyList-dialog-box" :visible.sync="dialogType" v-if="dialogType"
               width="560px" append-to-body :close-on-click-modal="false">
      <div class="applyList-dialog-min-box scrollbar-box">
        <el-form  :rules="dialogRules" v-loading="loading" :model="dialogForm" ref="dialogForm">
          <el-form-item label="app" prop="appId" label-width="90px">
            <el-select v-model="dialogForm.appId" :disabled="dialogForm.versionId?true:false" placeholder="请选择app" style="width: 360px">
              <el-option v-for="(item,index) in optionList"
                         :key="item.appId"
                         :label="item.appName"
                         :value="item.appId"/>
            </el-select>
          </el-form-item>
          <el-form-item label="版本号" prop="versionCode" label-width="90px">
            <el-input
              disabled
              v-model="dialogForm.versionCode"
              maxLength="20"
              placeholder="请上传APK"
              style="width: 360px"
            />
          </el-form-item>
          <el-form-item label="版本名称" prop="versionName" label-width="90px">
            <el-input
              disabled
              v-model="dialogForm.versionName"
              maxLength="20"
              placeholder="请上传APK"
              style="width: 360px"
            />
          </el-form-item>
          <el-form-item label="大小" prop="fileSize" label-width="90px">
            <el-input
              disabled
              v-model="dialogForm.fileSize"
              maxLength="20"
              placeholder="请上传APK"
              style="width: 360px"
            />
          </el-form-item>
          <el-form-item label="描述" prop="releaseNotes" label-width="90px">
            <el-input
              resize="none"
              type="textarea"
              v-model="dialogForm.releaseNotes"
              maxlength="100"
              placeholder="请输入描述"
              style="width: 360px"
              show-word-limit
              :rows="4"
            />
          </el-form-item>
          <el-upload
            style="margin:0 90px;"
            class="upload-demo"
            :action="uploadImgUrl"
            :show-file-list="false"
            drag
            multiple
            :on-success="(res)=>handleAvatarSuccess(res)"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <i class="el-icon-upload" style="color:#CCE6FE;margin-top:40px;"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="dialogCancel">取消</p>
        <p class="dialog-footer-button-primary" @click="dialogSubmit">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  const ApkParser = require('app-info-parser/src/apk');
  import {
    terminalVersionList,
    terminalVersionAdd,
    terminalVersionUpdate,
    terminalVersionDelete,
    terminalAppSelect,
  } from "@/api/iotDevice/index";
  import { getToken } from '@/utils/auth'
  export default {
    name: 'index',
    //components: {
    //  addPage
    //},
    data () {
      return {
        uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
        headers: {
          Authorization: getToken(),
        },
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //下拉列表数据
        optionList:[],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          appName:"",
          versionName :null,
          terminalType :null,
        },
        //时间数据
        dateRange:[],
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        dialogType:false,
        dialogForm:{
          appName:"",
          terminalType:"",
          description:"",
        },
        dialogRules:{
          appId: [
            { required: true, message: "app", trigger: "blur" }
          ],
          // package: [
          //   { required: true, message: "请上传APK", trigger: "blur" }
          // ],
          versionCode: [
            { required: true, message: "请上传APK", trigger: "blur" },
          ],
          versionName: [
            { required: true, message: "请上传APK", trigger: "blur" },
          ],
          size: [
            { required: true, message: "请上传APK", trigger: "blur" },
          ],
        },
        apkData:{},
      }
    },
    created () {

    },
    mounted () {
      this.terminalAppSelect();
      this.getList();
    },
    methods: {
      //提交按钮
      dialogSubmit(){
        if(this.loading){
          return
        }
        this.$refs["dialogForm"].validate(valid => {
          if (valid) {
            if(this.dialogForm.versionId){
              terminalVersionUpdate(this.dialogForm).then( response => {
                this.msgSuccess(response.message);
                this.$set(this,'dialogType',false);
                this.resetQuery();
              });
            }else{
              terminalVersionAdd(this.dialogForm).then( response => {
                this.msgSuccess(response.message);
                this.$set(this,'dialogType',false);
                this.resetQuery();
              });
            }
          }
        })
      },
      //关闭按钮
      dialogCancel(){
        this.$set(this,'dialogType',false);
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'dateRange',[])
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          appName:"",
          versionName :null,
          terminalType :null,
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        terminalVersionList(this.queryParams).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      terminalAppSelect(){
        terminalAppSelect({}).then(response => {
          this.$set(this,'optionList',response.data);
        });
      },
      //操作按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //新增
          this.$set(this,'dialogForm',{
            appId:"",
            // package:"",
            versionCode:"",
            versionName:"",
            apkUrl:"",
            fileSize:"",
            releaseNotes:"",
          });
          this.$set(this,'dialogType',true);
        }else if(type == 2){
          //编辑
          let obj = JSON.parse(JSON.stringify(row))
          this.$set(this,'dialogForm',{
            versionId:obj.versionId,
            appId:obj.appId,
            // package:obj.package,
            versionCode:obj.versionCode,
            versionName:obj.versionName,
            apkUrl:obj.apkUrl,
            fileSize:obj.fileSize,
            releaseNotes:obj.releaseNotes,
          });
          this.$set(this,'dialogType',true);
        }else if(type == 3){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            terminalVersionDelete({versionId:row.versionId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }
      },
      //上传相关
      handleAvatarSuccess(res) {
        // this.$set(this.dialogForm,'package',this.apkData.package);
        this.$set(this.dialogForm,'versionCode',this.apkData.versionCode);
        this.$set(this.dialogForm,'versionName',this.apkData.versionName);
        this.$set(this.dialogForm,'fileSize',res.data.size);
        this.$set(this.dialogForm,'apkUrl',res.data.url);
        this.$set(this,'loading',false);
        this.$forceUpdate()
      },
      beforeAvatarUpload(file) {
        this.$set(this,'loading',true);
        let type = false;
        if (file.type == 'application/vnd.android.package-archive' ) {
          if(file.size> 210000000){
            this.msgError('上传图片大小不能超过200M')
            this.$set(this,'loading',true);
            return false
          }
          const parser = new ApkParser(file);
          parser
            .parse()
            .then((result) => {
              this.$set(this,'apkData',{
                // package:result.package,
                versionCode:result.versionCode,
                versionName:result.versionName,
              });
            })
            .catch((err) => {

            });
          type = true;
        }else{
          this.msgError('请上传APK文件')
          type = false;
          this.$set(this,'loading',false);
        }
        return type;
      },
    },
  }
</script>
<style scoped lang="scss">
  .applyListVersions{
    .applyListVersionsPage{

    }
  }
</style>
