<template>
  <div id="dynamicdata">
    <!-- tab分页 -->
    <el-row class="top-tab">
      <el-tabs v-model="curType" type='card'>
        <el-tab-pane v-for="(item) in typeList" :key="item.Name" :label="item.Name+'('+item.Sum+'/'+item.Rcount+')'" :name="item.Name">
        </el-tab-pane>
      </el-tabs>
      <div class="pos-right">
        <span style="margin-right:10px;" v-show="lastUpdateTime">最后配置完成时间：{{lastUpdateTime}}</span>
        <span v-if="tipsType==2" class="tips">
          原始点位变化，部分标识受到影响，请重新执行对应规则
          <i class="el-icon-right"></i>
        </span>
        <span v-if="tipsType==3" class="tips">
          对应规则有更新，记得执行哦
          <i class="el-icon-right"></i>
        </span>
        <span v-if="tipsType==0" class="tips">规则执行中。。。</span>
        <el-tooltip class="item" effect="dark" :content="'按对应规则自动设定实例动态参数信息点'" placement="left">
          <el-badge :is-dot="tipsType==2||tipsType==3" class="item">
            <el-button size="small" type="primary" @click="showSelectAll" icon="el-icon-refresh" style="float:right;" :disabled="isPending">配置
            </el-button>
            <!-- @contextmenu.native.prevent="show" -->
          </el-badge>
        </el-tooltip>
      </div>
    </el-row>
    <!-- 设备 -->
    <equipRules v-if="curType=='设备'" ref="equip" :typeName="curType" @refresh="refreshTabs" :isPending="isPending"></equipRules>
    <!-- 部件 -->
    <partsRules v-else-if="curType=='部件'" ref="parts" :typeName="curType" @refresh="refreshTabs" :isPending="isPending"></partsRules>
    <!-- 系统 -->
    <systemRules v-else-if="curType=='系统'" ref="system" :typeName="curType" @refresh="refreshTabs" :isPending="isPending"></systemRules>
    <!-- 空间 -->
    <spaceRules v-else-if="curType=='空间'" ref="space" :typeName="curType" @refresh="refreshTabs" :isPending="isPending"></spaceRules>
    <!-- 开发中 -->
    <div v-else>开发中</div>
    <!-- 应用规则弹窗 -->
    <apply-rules-dialog ref="apply" @refresh="getExecuteFeedback" @cancel="cancelApply" @clash="getClash"></apply-rules-dialog>
    <!-- 冲突日志 -->
    <applyLog ref="applyLog"></applyLog>
    <!-- 规则更新提示 -->
    <el-dialog title="提示" :visible.sync="ruleDialogShow" width="25%" @close="rulesClose">
      <span>{{tipCon}}</span>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="rulesClose">稍后处理</el-button>
        <el-button size="small" type="primary" @click="apply(false)">现在执行</el-button>
      </span>
    </el-dialog>
    <!-- 选择增量 or 全量 执行 -->
    <el-dialog title="提示" :visible.sync="allApplyDialog" width="30%" @close="allApplyClose">
      <div id="applySelect">
        <el-radio v-model="force" :label="false">增量执行：只设定对应规则or原始点位有变化的实例动态参数信息点</el-radio>
        <p style="color:#999;">因自动判断是否变化，可能更新不够完整，但速度比较快</p>
        <p>
          <span class="el-dropdown-link" @click="allApplyFlag=true">
            我要全量执行<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
        </p>
        <div v-show="allApplyFlag">
          <el-radio v-model="force" :label="true">全量执行：按对应规则重新设定所有实例动态参数信息点</el-radio>
          <p style="color:#ff0000;">全量执行时间较长，且执行过程中相关功能不可操作</p>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="allApplyClose">取消</el-button>
        <el-button size="small" type="primary" @click="apply">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tools from "@/utils/tools";
import { mapGetters, mapActions } from "vuex";
// import applyRulesDialog from "@/components/point/dynamicdata/applyRulesDialog";
import applyRulesDialog from "@/components/point/dynamicdata/applyRulesDialog-copy";
import applyLog from "@/components/point/dynamicdata/applyLog";
import equipRules from "@/components/point/dynamicdata/equipRules";
import partsRules from "@/components/point/dynamicdata/partsRules";
import systemRules from "@/components/point/dynamicdata/systemRules";
import spaceRules from "@/components/point/dynamicdata/spaceRules";
import {
  dynamicClashConfirm,
  dynamicPromptmessage,
  dynamicPointTypeList,
  dynamicExecuteFeedback
} from "@/api/scan/request";
export default {
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  data() {
    return {
      curType: "", //当前所在tab页
      typeList: [], //tab页list
      lastUpdateTime: "", //最后更新时间
      tipsType: 1, //提示信息种类
      ruleDialogShow: false, //规则提示执行弹窗
      tipCon: '', //配置按钮提示文字-规则提示执行弹窗文字
      allApplyDialog: false, //全量执行弹窗
      allApplyFlag: false, //全量执行flag
      force: false, //默认增量执行 ----- false - 增量 true - 全量执行
      isPending: false, //是否执行中:执行中禁用所有功能 true即为执行中
      missionID: '', //任务id
      hasClash: false, //默认不含冲突
    };
  },
  components: {
    applyRulesDialog,
    applyLog,
    equipRules,
    partsRules,
    systemRules,
    spaceRules
  },
  created() {
    let typeName = this.$route.query.typeName;
    if (typeName) {
      this.curType = typeName
    }
    this.init();
  },
  mounted() { },
  methods: {
    //获取查询条件-提示信息
    init() {
      let param = { Related: true };
      //对象类型
      this.getTypeNames();
      //获取 任务列表
      this.getMissionList();
    },
    //查询任务列表
    getMissionList() {
      dynamicExecuteFeedback({ Id: null }, res => {
        let missionList = res.Content
        missionList.map(mission => {
          if ('Success/Fail'.indexOf(mission.Completed) > -1) {

          } else {
            this.missionID = mission.Id
          }
        })
        if (this.missionID) {
          this.isPending = true;
          this.tipsType = 0;
          this.getExecuteFeedback(this.missionID)
        } else {
          this.isPending = false;
          this.getPrompt()
        }
      })
    },
    //获取提示消息
    getPrompt() {
      dynamicPromptmessage({}, res => {
        if (res.Result == "success") {
          if (res.Content.length) {
            for (let i = 0; i < res.Content.length; i++) {
              if (res.Content[i].Type == "最后一次执行时间") {
                this.tipsType = this.tipsType > 1 ? this.tipsType : 1;
                this.lastUpdateTime = res.Content[i].Content.substr(0, res.Content[i].Content.lastIndexOf('.'));
              }
              if (res.Content[i].Type == "原始点位增删") {
                this.tipsType = 2;
                this.tipCon = "原始点位变化，部分标识受到影响，请重新执行对应规则";
                this.ruleDialogShow = true;
              }
              if (res.Content[i].Type == "未执行对应规则") {
                this.tipsType = 3;
                this.tipCon = "对应规则更新，是否现在执行？";
                this.ruleDialogShow = true;
              }
            }
          } else {
            this.tipsType = 1;
            this.tipCon = '';
            this.lastUpdateTime = ''
          }
        }
      });
    },
    //获取tab页
    getTypeNames() {
      let param = { Related: true };
      //对象类型
      dynamicPointTypeList(param, res => {
        if (res.Result == "success") {
          this.typeList = res.Content;
          if (this.typeList.length) {
            //记录上次操作的tab页
            this.curType =
              this.curType != 0 ? this.curType : this.typeList[0].Name;
          } else {
            this.tableData = [];
          }
        }
      });
    },
    //显示选择增量or增量执行
    showSelectAll() {
      this.allApplyDialog = true;
      this.force = false;
      this.allApplyFlag = false;
    },
    //执行对应结果
    apply() {
      this.ruleDialogShow = false;
      this.tipsType = 0;
      this.isPending = true;
      this.allApplyClose()
      this.$refs.apply.showDialog(this.allApplyFlag);
    },
    //规则执行弹窗
    rulesClose() {
      this.ruleDialogShow = false;
    },
    //关闭选择全量-增量弹窗
    allApplyClose() {
      this.allApplyDialog = false;
      this.force = false;
    },
    //刷新列表
    refreshTabs(val) {
      this.curType = val;
      this.getTypeNames()
    },
    // show() {
    //   console.log('右键事件')
    // },
    //获取执行反馈
    getExecuteFeedback(missionID) {
      let param = {
        Id: missionID
      }
      setTimeout(() => {
        dynamicExecuteFeedback(param, res => {
          this.allApplyClose()
          let response = res.Content[0]
          if ('Success/Fail'.indexOf(response.Completed) > -1) {
            setTimeout(() => {
              this.isPending = false;
              this.getPrompt()
              this.$refs.apply.getPromptobjs()
              if (this.hasClash) {
                this.$refs.applyLog.showDialog(missionID)
              }
            }, 2000)
          } else {
            this.isPending = true;
            this.tipsType = 0;
            setTimeout(() => {
              this.getExecuteFeedback(missionID)
            }, 2000)
          }
        })
      }, 2000)
    },
    //取消执行
    cancelApply(x) {
      this.isPending = false;
      this.tipsType = 1;
    },
    //接收是否有冲突
    getClash(length) {
      this.hasClash = !!length
    }
  },
  watch: {
    projectId() {
      this.init();
    }
  }
};
</script>
<style lang="scss" scoped>
#dynamicdata {
  overflow: hidden;
  height: 100%;
  background-color: #fff;
  padding: 10px;
  position: relative;
  /deep/ .top-tab {
    position: relative;
    height: 54px;
    .pos-right {
      position: absolute;
      right: 0;
      top: 0;
      .tips {
        color: red;
        position: absolute;
        right: 73px;
        width: 390px;
        text-align: right;
        padding-right: 10px;
        background-color: #fff;
      }
    }
    /deep/ .el-tabs__item.is-top {
      border-top: 2px solid transparent;
      &.is-active {
        border-top: 2px solid #409eff;
      }
    }
  }
  #applySelect {
    /deep/ .el-radio__input.is-checked + .el-radio__label {
      color: #000;
    }
    p {
      font-size: 12px;
      padding-left: 24px;
    }
  }
}
</style>

