<!--应用升级计划列表-->
<template>
  <div class="app-container applyUpgrades">
    <div class="page-container applyUpgradesPage">
      <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="app名称"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="appVersion" label-width="90px">
            <el-input
              maxLength="30"
              v-model="queryParams.appVersion"
              placeholder="app版本号"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="status">
            <el-select v-model="queryParams.status" placeholder="状态" clearable style="width:200px;">
              <el-option
                v-for="dict in optionList"
                :key="dict.code"
                :label="dict.description"
                :value="dict.code"
              ></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="addButton"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="tableList" ref="multipleTable">
          <el-table-column label="app名称" align="center" prop="appName" show-overflow-tooltip/>
          <el-table-column label="app版本" align="center" prop="appVersion" show-overflow-tooltip width="140"/>
          <el-table-column label="总设备数" align="center" prop="totalDevices" show-overflow-tooltip width="140"/>
          <el-table-column label="分批数" align="center" prop="batchSize" show-overflow-tooltip width="140"/>
          <el-table-column label="成功数" align="center" prop="successCount" show-overflow-tooltip width="140"/>
          <el-table-column label="状态" align="center" prop="status" show-overflow-tooltip width="180">
            <template slot-scope="scope">
              {{scope.row.status=='pending'?'待执行':(scope.row.status=='in_progress'?'进行中':(scope.row.status=='completed'?'已完成':(scope.row.status=='termination'?'终止':(scope.row.status=='failed'?'失败':''))))}}
            </template>
          </el-table-column>
          <el-table-column label="时间" align="center" prop="createTime" show-overflow-tooltip width="180">
            <template slot-scope="scope">
              {{parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}")}}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" prop="deptName" width="140">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="terminalUpgradeWebPlanDetail(scope.row)"
                >详情</p>
                <p class="table-button-p"
                   @click="terminalUpgradeWebPlanTermination(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.appId?'编辑':'新增'" class="applyList-dialog-box" :visible.sync="dialogType" v-if="dialogType"
               width="700px" append-to-body :close-on-click-modal="false">
      <div class="applyList-dialog-min-box scrollbar-box">
        <el-form  v-loading="loading" :rules="dialogRules" :model="dialogForm" ref="dialogForm">
          <el-form-item label="APP" prop="appId" label-width="120px">
            <el-select v-model="dialogForm.appId" @change="terminalVersionSelect" placeholder="请选择APP" style="width: 360px">
              <el-option v-for="(item,index) in optionListOne"
                         :key="item.appId"
                         :label="item.appName"
                         :value="item.appId"/>
            </el-select>
          </el-form-item>
          <el-form-item label="版本" prop="versionId" label-width="120px">
            <el-select v-model="dialogForm.versionId" placeholder="请选择版本" style="width: 360px">
              <el-option v-for="(item,index) in optionListTwo"
                         :key="item.versionId"
                         :label="item.versionName"
                         :value="item.versionId"/>
            </el-select>
          </el-form-item>
          <el-form-item label="类型" prop="deviceCode" style="display: block" label-width="120px">
            <el-select v-model="dialogForm.deviceCode" placeholder="请选择类型" style="width: 360px">
              <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>
          <el-form-item label="分批升级数量" prop="batchSize" label-width="120px">
            <el-input-number v-model="dialogForm.batchSize" :step="1" :min="1" :max="200" step-strictly style="width: 360px"></el-input-number>
          </el-form-item>
          <el-form-item label="强制更新" prop="isForceUpdate" style="display: block" label-width="120px">
            <el-radio-group v-model="dialogForm.isForceUpdate" style="width:320px;">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="升级范围" prop="upgradeScope" style="display: block" label-width="120px">
            <el-radio-group v-model="dialogForm.upgradeScope" style="width:320px;">
              <el-radio label="campus">校区</el-radio>
              <el-radio label="building">楼栋</el-radio>
              <el-radio label="laboratory">实验室</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="校区" prop="schoolId" style="display: block" label-width="120px" v-if="dialogForm.upgradeScope == 'campus' || dialogForm.upgradeScope == 'building' || dialogForm.upgradeScope == 'laboratory'">
            <el-select v-model="dialogForm.schoolId" @change="changeSchool" filterable
                       placeholder="请选择校区" style="width: 360px">
              <el-option v-for="(item,index) in schoolOption"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="楼栋" prop="buildId" style="display: block" label-width="120px" v-if="dialogForm.upgradeScope == 'building' || dialogForm.upgradeScope == 'laboratory'">
            <el-select v-model="dialogForm.buildId" @change="buildSchool" filterable
                       placeholder="请选择楼栋" style="width: 360px">
              <el-option v-for="(item,index) in buildOption"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="实验室" prop="subjectId" style="display: block" label-width="120px" v-if="dialogForm.upgradeScope == 'laboratory'">
            <el-select v-model="dialogForm.subjectId" filterable
                       placeholder="请选择实验室" style="width: 360px">
              <el-option v-for="(item,index) in subjectOption"
                         :key="item.subId"
                         :label="item.subName+(item.roomNum?`(`+item.roomNum+`)`:'')"
                         :value="item.subId"/>
            </el-select>
          </el-form-item>
        </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>
    <el-dialog title="计划详情" class="applyList-dialog-box" :visible.sync="dialogInfoType" v-if="dialogInfoType"
               width="400px" append-to-body :close-on-click-modal="false">
      <div class="dialog-applyUpgrades-info-max-big-box">
        <div class="dialog-info-max-box">
          <p>app名称：</p>
          <p>{{dialogInfoData.appName}}</p>
        </div>
        <div class="dialog-info-max-box">
          <p>app版本号：</p>
          <p>{{dialogInfoData.appVersion}}</p>
        </div>
        <div class="dialog-info-max-box">
          <p>app大小：</p>
          <p>{{dialogInfoData.batchSize?dialogInfoData.batchSize+'MB':''}}</p>
        </div>
        <div class="dialog-info-max-box">
          <p>强制更新：</p>
          <p>{{dialogInfoData.isForceUpdate?'是':'否'}}</p>
        </div>
        <div class="dialog-info-max-box">
          <p>升级范围：</p>
          <p>{{dialogInfoData.upgradeScope=='campus'?'校区':(dialogInfoData.upgradeScope=='building'?'楼栋':(dialogInfoData.upgradeScope=='laboratory'?'实验室':''))}}</p>
        </div>
        <div class="dialog-info-max-box" v-if="dialogInfoData.upgradeScope=='campus'||dialogInfoData.upgradeScope=='building'||dialogInfoData.upgradeScope=='laboratory'">
          <p>校区：</p>
          <p>{{dialogInfoData.schoolName}}</p>
        </div>
        <div class="dialog-info-max-box" v-if="dialogInfoData.upgradeScope=='building'||dialogInfoData.upgradeScope=='laboratory'">
          <p>楼栋：</p>
          <p>{{dialogInfoData.buildName}}</p>
        </div>
        <div class="dialog-info-max-box" v-if="dialogInfoData.upgradeScope=='laboratory'">
          <p>实验室：</p>
          <p>{{dialogInfoData.subjectName}}</p>
        </div>
      </div>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="dialogInfoCancel">关闭</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { systemBuildingGetTreeList,laboratorySubRelInfoGetListByFloor } from "@/api/commonality/permission";
  import {
    terminalUpgradeWebPlanList,
    terminalUpgradeWebPlanCreate,
    terminalUpgradeWebPlanStatus,
    terminalUpgradeWebPlanTermination,
    terminalAppSelect,
    terminalVersionSelect,
    terminalUpgradeWebPlanDetail,
  } from "@/api/iotDevice/index";
  export default {
    name: 'applyUpgrades',
    data(){
      return{
        loading:false,
        optionList:[],
        optionListOne:[],
        optionListTwo:[],
        queryParams:{
          page:1,
          pageSize:20,
          appName:"",
          appVersion:"",
          status:"",
        },
        tableList:[],
        total:0,
        dialogType:false,
        dialogForm:{
          appId:null,
          versionId:null,
          deviceCode:null,
          batchSize:null,
          isForceUpdate:null,
          upgradeScope:null,
          schoolId:null,
          buildId:null,
          subjectId:null,
        },
        dialogRules:{
          appId: [
            { required: true, message: "请选择APP", trigger: "blur" }
          ],
          versionId: [
            { required: true, message: "请选择版本", trigger: "blur" },
          ],
          deviceCode: [
            { required: true, message: "请选择类型", trigger: "blur" },
          ],
          batchSize: [
            { required: true, message: "请选择分批升级数量", trigger: "blur" },
          ],
          isForceUpdate: [
            { required: true, message: "请选择是否强制更新", trigger: "blur" }
          ],
          upgradeScope: [
            { required: true, message: "请选择升级范围", trigger: "blur" },
          ],
          schoolId: [
            { required: true, message: "请选择校区", trigger: "blur" }
          ],
          buildId: [
            { required: true, message: "请选择楼栋", trigger: "blur" },
          ],
          subjectId: [
            { required: true, message: "请选择实验室", trigger: "blur" }
          ],
        },
        schoolOption:[],
        buildOption:[],
        addressList:[],
        subjectOption:[],
        //详情弹窗
        dialogInfoData:{},
        dialogInfoType:false,
      }
    },
    created(){

    },
    mounted(){
      this.getList();
      this.terminalUpgradeWebPlanStatus();
      this.systemBuildingGetTreeList();
      this.terminalAppSelect();
    },
    methods:{
      //详情
      terminalUpgradeWebPlanDetail(row){
        terminalUpgradeWebPlanDetail({planId:row.planId}).then(response => {
          let obj = {
            appName:response.data.appName,
            appVersion:response.data.appVersion,
            batchSize:response.data.batchSize,
            isForceUpdate:response.data.isForceUpdate,
            planId:response.data.planId,
            upgradeScope:response.data.upgradeScope,
            businessId:response.data.businessId,
          }
          this.getAdd(obj);
        });
      },
      getAdd(obj){
        let self = this;
        let idList = obj.businessId.split(',');
        if (obj.upgradeScope == 'campus'){
          for(let i=0;i<self.addressList.length;i++){
            if(self.addressList[i].id == idList[0]){
              obj.schoolName = self.addressList[i].name;
            }
          }
          this.$set(this,'dialogInfoData',obj);
          this.$set(this,'dialogInfoType',true);
        }else if (obj.upgradeScope == 'building'||obj.upgradeScope == 'laboratory'){
          for(let i=0;i<self.addressList.length;i++){
            if(self.addressList[i].id == idList[0]){
              for(let o=0;o<self.addressList[i].buildFloorVoList.length;o++){
                if(self.addressList[i].buildFloorVoList[o].id == idList[1]){
                  obj.schoolName = self.addressList[i].name;
                  obj.buildName = self.addressList[i].buildFloorVoList[o].name;
                }
              }
            }
          }
          if(obj.upgradeScope == 'laboratory'){
            laboratorySubRelInfoGetListByFloor({buildId:idList[1]}).then(response => {
              for(let i=0;i<response.data.length;i++){
                if(response.data[i].subId == idList[2]){
                  obj.subjectName = response.data[i].subName;
                }
              }
              this.$set(this,'dialogInfoData',obj);
              this.$set(this,'dialogInfoType',true);
            })
          }else{
            this.$set(this,'dialogInfoData',obj);
            this.$set(this,'dialogInfoType',true);
          }
        }
      },
      //新增
      addButton(){
        this.$set(this,'dialogForm',{
          appId:null,
          versionId:null,
          deviceCode:null,
          batchSize:null,
          isForceUpdate:null,
          upgradeScope:null,
          schoolId:null,
          buildId:null,
          subjectId:null,
        });
        this.$set(this,'dialogType',true);
      },
      //提交按钮
      dialogSubmit(){
        this.$refs["dialogForm"].validate(valid => {
          if (valid) {
            let obj = {
              appId:this.dialogForm.appId,
              versionId:this.dialogForm.versionId,
              deviceCode:this.dialogForm.deviceCode,
              batchSize:this.dialogForm.batchSize,
              isForceUpdate:this.dialogForm.isForceUpdate,
              upgradeScope:this.dialogForm.upgradeScope,
              scope:{},
            }
            if(this.dialogForm.upgradeScope == "campus"){
              obj.scope.campusId = this.dialogForm.schoolId;
              obj.businessId = this.dialogForm.schoolId;
            }else if(this.dialogForm.upgradeScope == "building"){
              obj.scope.buildingId = this.dialogForm.buildId;
              obj.businessId = this.dialogForm.schoolId;
              obj.businessId = obj.businessId+','+this.dialogForm.buildId;
            }else if(this.dialogForm.upgradeScope == "laboratory"){
              obj.scope.subId = this.dialogForm.subjectId;
              obj.businessId = this.dialogForm.schoolId;
              obj.businessId = obj.businessId+','+this.dialogForm.buildId;
              obj.businessId = obj.businessId+','+this.dialogForm.subjectId;
            }
            terminalUpgradeWebPlanCreate(obj).then(response => {
              this.msgSuccess(response.message)
              this.resetQuery();
            });
          }
        })
      },
      //关闭按钮
      dialogCancel(){
        this.$set(this,'dialogType',false);
      },
      dialogInfoCancel(){
        this.$set(this,'dialogInfoType',false);
      },
      //终止计划
      terminalUpgradeWebPlanTermination(row){
        let self = this;
        this.$confirm('是否确认终止计划?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        }).then(() => {
          terminalUpgradeWebPlanTermination({planId:row.planId}).then(response => {
            self.msgSuccess(response.message)
            self.getList();
          });
        }).catch(() => {});
      },
      //查询
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          appName:"",
          appVersion:"",
          status:"",
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        terminalUpgradeWebPlanList(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'tableList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //计划状态列表
      terminalUpgradeWebPlanStatus(){
        terminalUpgradeWebPlanStatus({}).then(response => {
          this.$set(this,'optionList',response.data);
        });
      },//获取校区
      systemBuildingGetTreeList(){
        systemBuildingGetTreeList({}).then(response => {
          let list = [];
          for(let i=0;i<response.data.length;i++){
            list.push({
              id:response.data[i].id,
              name:response.data[i].name,
            })
          }
          this.$set(this,'schoolOption',list);
          this.$set(this,'addressList',response.data);
        })
      },
      //校区选中
      changeSchool(val){
        let self = this;
        let list = [];
        for(let i=0;i<self.addressList.length;i++){
          if(val == self.addressList[i].id && self.addressList[i].buildFloorVoList[0]){
            for(let o=0;o<self.addressList[i].buildFloorVoList.length;o++){
              list.push({
                id:self.addressList[i].buildFloorVoList[o].id,
                name:self.addressList[i].buildFloorVoList[o].name,
              })
            }
          }
        }
        this.$set(this.dialogForm,'buildId',null);
        this.$set(this.dialogForm,'subjectId',null);
        this.$set(this,'buildOption',list);
        this.$set(this,'subjectOption',[]);
      },
      //楼栋选中
      buildSchool(val){
        laboratorySubRelInfoGetListByFloor({buildId:val}).then(response => {
          this.$set(this.dialogForm,'subjectId',null);
          this.$set(this,'subjectOption',response.data);
        })
      },
      //APP选择器
      terminalAppSelect(){
        terminalAppSelect({}).then(response => {
          this.$set(this,'optionListOne',response.data);
        });
      },
      //版本选择器
      terminalVersionSelect(){
        terminalVersionSelect({appId:this.dialogForm.appId}).then(response => {
          this.$set(this,'optionListTwo',response.data);
          this.$set(this.dialogForm,'versionId',"");
        });
      },
    },
  }
</script>

<style scoped lang="scss">
  .applyUpgrades{
    .applyUpgradesPage{

    }
  }
</style>
<style lang="scss">
  .dialog-applyUpgrades-info-max-big-box{
    .dialog-info-max-box{
      display: flex;
      p{
        line-height:30px;
        font-size:16px;
      }
      p:nth-child(1){
        width:100px;
        text-align: right;
      }
      p:nth-child(2){
        flex:1;
        margin-left:5px;
      }
    }
  }
</style>
