<template>
  <div class="app-container">
    <el-descriptions class="margin-top" :title="ruleEngineInfo.name" :column="3" border>
      <template slot="extra">
        <el-dropdown split-button type="primary" @command="handleClick">
          更多操作
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="add">版本添加</el-dropdown-item>
            <el-dropdown-item command="activate">{{(ruleEngineInfo.ruleengine.isActivate === 'no-activate' ? "激活":"禁用")+'引擎'}}</el-dropdown-item>
            <el-dropdown-item command="status">{{(ruleEngineInfo.ruleengine.status === '0' ? "关闭": "开启")+'引擎'}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>

      </template>
      <el-descriptions-item label="规则名称">
        {{ruleEngineInfo.ruleengine.name}}
      </el-descriptions-item>
      <el-descriptions-item label="规则类型">
        <dict-tag :options="dict.type.rule_engine_type" :value="ruleEngineInfo.ruleengine.type"/>
      </el-descriptions-item>
      <el-descriptions-item label="规则作用域">
        <dict-tag :options="dict.type.rule_engine_level" :value="ruleEngineInfo.ruleengine.level"/>
      </el-descriptions-item>
      <el-descriptions-item label="引擎编码"> {{ruleEngineInfo.ruleengine.code + " ("+ruleEngineInfo.ruleengine.engineCode+")"}} </el-descriptions-item>
      <el-descriptions-item label="是否激活">
        <dict-tag :options="dict.type.rule_engine_activate_status" :value="ruleEngineInfo.ruleengine.isActivate"/>
      </el-descriptions-item>
      <el-descriptions-item label="规则状态">
        <dict-tag :options="dict.type.sys_normal_disable" :value="ruleEngineInfo.ruleengine.status"/>
      </el-descriptions-item>
    </el-descriptions>
    <editor style="margin-top: 20px" :read-only="true" v-model="ruleEngineInfo.ruleengine.description"></editor>

    <el-card class="box-card" v-if="ruleEngineInfo.engineVersionList.length>0">
      <div slot="header" class="clearfix">
        <span>规则引擎版本</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="12" v-for="ruleEngineVersion in ruleEngineInfo.engineVersionList">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>{{ruleEngineVersion.name + "-" + ruleEngineVersion.code}}</span>
              <el-dropdown style="float: right; padding: 3px 0" @command="checkRuleEngineVersion">
                <span class="el-dropdown-link">
                  更多操作<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item :command="{event: 'info', ruleEngineVersion: ruleEngineVersion}">规则详情</el-dropdown-item>
                  <el-dropdown-item :command="{event: 'test', ruleEngineVersion: ruleEngineVersion}" :disabled="ruleEngineVersion.status === '0'">测试规则</el-dropdown-item>
                  <el-dropdown-item :command="{event: 'status', ruleEngineVersion: ruleEngineVersion}" :disabled="ruleEngineVersion.status !== '2'">
                    {{ruleEngineVersion.isActivate !== 'no-activate' ? "禁用" : "激活"}}规则
                  </el-dropdown-item>
                  <el-dropdown-item
                    :command="{event: 'publish', ruleEngineVersion: ruleEngineVersion}"
                    v-if="ruleEngineVersion.status === '1' && ruleEngineVersion.isTest === '1'">
                    发布规则
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
            <el-descriptions class="margin-top" :column="2" border>
              <el-descriptions-item label="版本类" :span="2"> {{ruleEngineVersion.versionCode}} </el-descriptions-item>
              <el-descriptions-item label="版本名称"> {{ruleEngineVersion.name}} </el-descriptions-item>
              <el-descriptions-item label="版本CODE"> {{ruleEngineVersion.code}} </el-descriptions-item>
              <el-descriptions-item label="是否激活">
                <dict-tag :options="dict.type.rule_engine_activate_status" :value="ruleEngineVersion.isActivate"/>
              </el-descriptions-item>
              <el-descriptions-item label="版本状态">
                <dict-tag :options="dict.type.rule_engine_version_status" :value="ruleEngineVersion.status"/>
              </el-descriptions-item>
              <el-descriptions-item label="是否测试">
                <dict-tag :options="dict.type.rule_engine_activate_is_test" :value="ruleEngineVersion.isTest"/>
              </el-descriptions-item>
            </el-descriptions>
            <editor style="margin-top: 10px" :read-only="true" v-model="ruleEngineVersion.description"></editor>
          </el-card>
        </el-col>
      </el-row>
    </el-card>
    <el-dialog
      title="新增版本" :visible.sync="addVersionStatus"
      width="75%">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>基本信息</span>
          <el-button style="float: right; padding: 3px 0" type="text" @click="genEngineVersion">生成引擎版本类</el-button>
        </div>
        <el-form :data="engineVersionForm" label-width="80px" :model="engineVersionForm">
          <el-row>
            <el-col :span="24">
              <el-form-item label="版本类">
                <el-input v-model="engineVersionForm.versionCode" disabled placeholder="点击类生成自动生成类名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="名称">
                <el-input v-model="engineVersionForm.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码">
                <el-input v-model="engineVersionForm.code"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="状态">
                <dict-tag :options="dict.type.rule_engine_version_status" :value="engineVersionForm.status"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否激活">
                <dict-tag :options="dict.type.rule_engine_activate_status" :value="engineVersionForm.isActivate"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <editor v-model="engineVersionForm.description" :min-height="150"/>
      </el-card>
      <el-card class="box-card" v-if="engineVersionForm.codeIng != null">
        <div slot="header" class="clearfix">
          <span>引擎编码</span>
        </div>
        <encoding style="height: 800px" v-model="engineVersionForm.codeIng"></encoding>
      </el-card>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addVersionCancel">取 消</el-button>
        <el-button type="primary" @click="addVersionSubmission">确 定</el-button>
      </span>
    </el-dialog>
    <el-drawer
      title="规则版本详情" size="80%" :before-close="ruleEngineVersionInfoStatusClose"
      :visible.sync="ruleEngineVersionInfoStatus"
      :direction="'rtl'">
      <div class="app-container">
        <el-descriptions v-if="ruleEngineVersionInfoAndTest != null" class="margin-top" :column="2" border>
          <el-descriptions-item label="版本类" :span="2"> {{ruleEngineVersionInfoAndTest.versionCode}} </el-descriptions-item>
          <el-descriptions-item label="版本名称"> {{ruleEngineVersionInfoAndTest.name}} </el-descriptions-item>
          <el-descriptions-item label="版本CODE"> {{ruleEngineVersionInfoAndTest.code}} </el-descriptions-item>
          <el-descriptions-item label="是否激活">
            <dict-tag :options="dict.type.rule_engine_activate_status" :value="ruleEngineVersionInfoAndTest.isActivate"/>
          </el-descriptions-item>
          <el-descriptions-item label="版本状态">
            <dict-tag :options="dict.type.rule_engine_version_status" :value="ruleEngineVersionInfoAndTest.status"/>
          </el-descriptions-item>
          <el-descriptions-item label="是否测试">
            <dict-tag :options="dict.type.rule_engine_activate_is_test" :value="ruleEngineVersionInfoAndTest.isTest"/>
          </el-descriptions-item>
        </el-descriptions>
        <editor style="margin-top: 10px" :read-only="true" :min-height="100" v-model="ruleEngineVersionInfoAndTest.description"></editor>
        <el-row style="margin-top: 10px">
          <el-button @click="saveCoding">保存代码</el-button>
        </el-row>
        <encoding  style="height: 800px; margin-top: 20px" v-model="ruleEngineVersionInfoAndTest.codeIng"/>
      </div>

    </el-drawer>
    <el-drawer
      title="规则版本测试" size="80%" :before-close="ruleEngineVersionTestStatusClose"
      :visible.sync="ruleEngineVersionTestStatus"
      :direction="'rtl'">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-descriptions v-if="ruleEngineVersionInfoAndTest != null" class="margin-top" :column="2" border>
            <el-descriptions-item label="版本类" :span="2"> {{ruleEngineVersionInfoAndTest.versionCode}} </el-descriptions-item>
            <el-descriptions-item label="版本名称"> {{ruleEngineVersionInfoAndTest.name}} </el-descriptions-item>
            <el-descriptions-item label="版本CODE"> {{ruleEngineVersionInfoAndTest.code}} </el-descriptions-item>
            <el-descriptions-item label="是否激活">
              <dict-tag :options="dict.type.rule_engine_activate_status" :value="ruleEngineVersionInfoAndTest.isActivate"/>
            </el-descriptions-item>
            <el-descriptions-item label="版本状态">
              <dict-tag :options="dict.type.rule_engine_version_status" :value="ruleEngineVersionInfoAndTest.status"/>
            </el-descriptions-item>
            <el-descriptions-item label="是否测试">
              <dict-tag :options="dict.type.rule_engine_activate_is_test" :value="ruleEngineVersionInfoAndTest.isTest"/>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="12">
          <editor style="margin-top: 10px" :read-only="true" :min-height="105" v-model="ruleEngineVersionInfoAndTest.description"></editor>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <encoding style="margin-top: 20px" v-model="ruleEngineVersionInfoAndTest.codeIng" :read-only="true"></encoding>
        </el-col>
<!--        <el-col :span="12">-->
<!--          <el-col :span="assetStructure == null ? 24 : 12">-->
<!--            <el-card class="box-card">-->
<!--              <div slot="header" class="clearfix">-->
<!--                <span>选择数据接入</span>-->
<!--              </div>-->
<!--              <el-select v-model="assetStructure" style="width: 100%;">-->
<!--                <el-option v-for="_assetStructure in assetStructureList"-->
<!--                           :key="_assetStructure.name"-->
<!--                           :value="_assetStructure.name"-->
<!--                           :label="_assetStructure.name+'('+_assetStructure.systemName+')'"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-card>-->
<!--          </el-col>-->
<!--          <el-col :span="12" v-if="assetStructure != null">-->
<!--            <el-card class="box-card" >-->
<!--              <div slot="header" class="clearfix">-->
<!--                <span>选择资产结构</span>-->
<!--              </div>-->
<!--              <el-select v-model="assets" style="width: 100%;">-->
<!--                <el-option v-for="_assets in assetsList"-->
<!--                           :key="_assets.name"-->
<!--                           :value="_assets.name"-->
<!--                           :label="_assets.name+'('+_assets.as+')'"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-card>-->
<!--          </el-col>-->
<!--          <el-col :span="24">-->
<!--            <el-card class="box-card" v-if="assets != null">-->
<!--              <div slot="header" class="clearfix">-->
<!--                <span>选择资产模型</span>-->
<!--              </div>-->
<!--              <el-descriptions direction="vertical" :column="3" border>-->
<!--                <el-descriptions-item v-for="_dataModel in dataModelList"-->
<!--                                      :label='_dataModel.name + "("+_dataModel.comment+")"'>-->
<!--                  <el-radio v-model="dataMode" :label="_dataModel.name" :value="_dataModel.id">{{dataModeMap[_dataModel.name]}}</el-radio>-->
<!--                </el-descriptions-item>-->
<!--              </el-descriptions>-->
<!--            </el-card>-->
<!--          </el-col>-->
<!--          <el-col :span="24" style="margin-top: 10px" v-if="dataMode != null">-->
<!--            <el-button>测试</el-button>-->
<!--            <el-input style="margin-top: 10px" v-model="testResult" type="textarea" placeholder="请点击测试" />-->
<!--          </el-col>-->
<!--        </el-col>-->
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
import { details, versionsAdd, engineOnOff, statusOnOff, versionsUpd, ruleengine } from '@/api/rule/maintenance'
import Encoding from "@/components/Encoding/index.vue";
export default {
  name: "EngineVersion",
  components: {Encoding},
  dicts: ['rule_engine_activate_status', 'rule_engine_type', 'sys_normal_disable', 'rule_engine_level', 'rule_engine_version_status','rule_engine_activate_is_test'],
  data() {
    return {
      ruleEngineId:this.$route.params && this.$route.params.id,
      ruleEngineInfo: {
        ruleengine:{
          id:null,
          name:null,
          code:null,
          type:null,
          level:null,
          isActivate:null,
          status:null,
          description:null,
          engineCode:null
        },
        engineVersionList:[],
      },
      // 新增版本
      addVersionStatus: false,
      engineVersionForm: {
        ruleId:0,
        name:null,
        code:null,
        status: "0",
        versionCode : null,
        isActivate: "no-activate",
        description: null,
        codeIng: null,
        isTest:"0"
      },
      assetStructure:null,
      assetStructureList:[],
      // 详情抽屉状态
      ruleEngineVersionInfoStatus: false,
      // 测试抽屉状态
      ruleEngineVersionTestStatus: false,
      // 弹框抽屉
      ruleEngineVersionInfoAndTest:{},
      //返回的编译数据
      ruleContentReq: {},
    }
  },
  created() {
    this.initRuleEngine();
  },
  methods: {
    //保存代码

    saveCoding: function() {
      this.ruleEngineVersionInfoAndTest.status = "1"
      console.log(this.ruleEngineVersionInfoAndTest)
      versionsUpd(this.ruleEngineVersionInfoAndTest).then(res=>{
        if (res.code === 200) {
          const codeName = this.ruleEngineVersionInfoAndTest.codeIng;
          this.ruleContentReq.ruleContent=codeName
          this.ruleContentReq.className= this.ruleEngineVersionInfoAndTest.versionCode
          this.$message.success(res.msg)
          ruleengine(this.ruleContentReq).then(
            res=>{
              this.$message.error(res.msg)
              this.ruleEngineVersionInfoStatus=false
              this.initRuleEngine(this.ruleEngineId)
              this.ruleEngineVersionInfoAndTest={}
            }
          )

        }else {
          this.$message.error(res.msg)
          this.initRuleEngine(this.ruleEngineId)
          this.ruleEngineVersionInfoAndTest={}
          this.ruleEngineVersionInfoStatus=false
        }
      })

    },
    //规则版本详情
    ruleEngineVersionInfoStatusClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.ruleEngineVersionInfoAndTest = {};
          done();
        })
        .catch(_ => {});
    },
    //规则版本测试
    ruleEngineVersionTestStatusClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.ruleEngineVersionInfoAndTest = {};
          done();
        })
        .catch(_ => {});
    },
    // 点击规则事件
    checkRuleEngineVersion(command) {
      switch (command.event) {
        case "info"://详情
          this.ruleEngineVersionInfoStatus = true
          this.ruleEngineVersionInfoAndTest = command.ruleEngineVersion;
          break;
        case "test"://测试
          this.ruleEngineVersionTestStatus = true
          this.ruleEngineVersionInfoAndTest = command.ruleEngineVersion;
          break;
        case "status"://激活状态
          command.ruleEngineVersion.isActivate = 'no-activate' === command.ruleEngineVersion.isActivate ? 'activated' : 'no-activate'
          break;
        case "publish"://发布
          command.ruleEngineVersion.status = '2'
          break;
      }
      console.log(command.event);
      console.log(command.ruleEngineVersion);
    },
    // 生成规则版本
    genEngineVersion(){
      if (this.engineVersionForm.name == null || this.engineVersionForm.name === "") {
        this.$message.error('规则名称不可为空');
        return false;
      }
      if (this.engineVersionForm.code == null || this.engineVersionForm.code === "") {
        this.$message.error('规则CODE不可为空');
        return false;
      }
      this.engineVersionForm.versionCode = this.ruleEngineInfo.ruleengine.engineCode + "_" + this.engineVersionForm.code
      this.engineVersionForm.codeIng= this.getCodeIng(this.ruleEngineInfo.ruleengine.level)
    },
    //引擎末班
    getCodeIng(value){
      let parentClass=null
      console.log("规则作用域",value)
      //数据集
      if (value === 'taskContext') {
        parentClass = "DataSetContext";

        //记录
      }else if (value === 'recordContext'){
        parentClass = "RecordContext";

        //数据字段
      }else if (value === 'dataSetContext'){
        parentClass = "DataModelContext";
      }

      let packageName = "com.muyu.engine";
      let customName = "custom";
      let templateName = "template";
      let codeIng = `package ${packageName}.${customName};

import com.muyu.engine.action.ActionDiscard;
import com.muyu.engine.scope.${parentClass};

/**
 * @Author: DongZeLiang
 * @date: 2024/5/6
 * @Description: ${this.engineVersionForm.name}-${this.engineVersionForm.code}
 * @Version: 1.0
 */
public class ${this.engineVersionForm.versionCode} extends ${parentClass} {
    public static void main(String[] args) {
        return "zhangsan";
    }
}`;
      return codeIng;
    },
    // 确定新增
    addVersionSubmission(){
      this.engineVersionForm.ruleId=this.$route.params.id
      console.log("engineVersionForm",this.engineVersionForm)
      versionsAdd(this.engineVersionForm).then(res=>{
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.addVersionStatus=false
          this.initRuleEngine(this.ruleEngineId)
          this.engineVersionForm={}
        }else {
          this.$message.error(res.msg)
          this.addVersionStatus=false
          this.initRuleEngine(this.ruleEngineId)
          this.engineVersionForm={}
        }
      })
    },
    // 取消新增
    addVersionCancel(){
      this.addVersionStatus = false
    },
    handleClick(clickType){
      console.log("clickType",clickType)
      if (clickType === "add"){
        //添加引擎版本
        this.addVersionStatus = true;
      }else if(clickType === "activate"){
        //修改是否激活引擎
        this.ruleEngineInfo.ruleengine.isActivate = 'no-activate' === this.ruleEngineInfo.ruleengine.isActivate ? 'activated' : 'no-activate'
        engineOnOff(this.ruleEngineInfo.ruleengine).then(res=>{
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.initRuleEngine(this.ruleEngineId)
          }else {
            this.$message.error(res.msg)
            this.initRuleEngine(this.ruleEngineId)
          }
        })
      }else if(clickType === "status"){
        //修改规则引擎的状态
        this.ruleEngineInfo.ruleengine.status = '0' === this.ruleEngineInfo.ruleengine.status ? '1' : '0'
        statusOnOff(this.ruleEngineInfo.ruleengine).then(res=>{
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.initRuleEngine(this.ruleEngineId)
          }else {
            this.$message.error(res.msg)
            this.initRuleEngine(this.ruleEngineId)
          }
        })
      }
    },
    //查规则引擎信息和版本信息
    initRuleEngine(){
      details(this.$route.params.id).then(res=>{
        this.ruleEngineInfo=res.data
      })
    }

  }
}
</script>

<style scoped>
.box-card{
  margin-top: 20px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.CodeMirror {
  height: 600px;
}
</style>
