<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.isActivate === 'no-activate' ? "激活": "禁用")+'引擎'}}</el-dropdown-item>
            <el-dropdown-item command="status">{{(ruleEngineInfo.status === '0' ? "关闭": "开启")+'引擎'}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>

      </template>
      <el-descriptions-item label="规则名称"> {{ruleEngineInfo.name}} </el-descriptions-item>
      <el-descriptions-item label="规则类型">
        <dict-tag :options="dict.type.rule_engine_type" :value="ruleEngineInfo.type"/>
      </el-descriptions-item>
      <el-descriptions-item label="规则作用域">
        <dict-tag :options="dict.type.rule_engine_level" :value="ruleEngineInfo.level"/>
      </el-descriptions-item>
      <el-descriptions-item label="引擎编码"> {{ruleEngineInfo.code + " ("+ruleEngineInfo.engineCode+")"}} </el-descriptions-item>
      <el-descriptions-item label="是否激活">
        <dict-tag :options="dict.type.rule_engine_activate_status" :value="ruleEngineInfo.isActivate"/>
      </el-descriptions-item>
      <el-descriptions-item label="规则状态">
        <dict-tag :options="dict.type.sys_normal_disable" :value="ruleEngineInfo.status"/>
      </el-descriptions-item>
    </el-descriptions>
    <editor style="margin-top: 20px" :read-only="true" v-model="ruleEngineInfo.description"></editor>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>规则引擎版本</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="12" v-for="ruleEngineVersion in ruleEngineInfo.ruleEngineVersionList">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>{{ruleEngineVersion.name + "-" + ruleEngineVersion.code}}</span>
              <el-dropdown style="float: right; padding: 3px 0">
                <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="info">规则详情</el-dropdown-item>
                  <el-dropdown-item command="test">测试规则</el-dropdown-item>
                  <el-dropdown-item command="status">禁用规则</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>
            <editor style="margin-top: 10px" :read-only="true" v-model="ruleEngineInfo.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 :label-position="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>
  </div>
</template>

<script>
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'],
  data() {
    return {
      ruleEngineId: this.$route.params && this.$route.params.ruleEngineId,
      ruleEngineInfo: {
        "id": 1,
        "name": "邮箱规则",
        "type": "rule-custom",
        "isActivate": "no-activate",
        "status": "0",
        "description": "<p>根据邮箱规则进行数据校验，内包含所有邮箱校验规则</p>",
        "remark": "-",
        "code": "email",
        "engineCode": "engine_custom_email",
        "level": "data-field",
      },
      // 新增版本
      addVersionStatus: false,
      engineVersionForm: {
        "name": "", "code": "", "status": "0",
        "versionCode": "",
        "isActivate": "no-activate",
        "description": "",
        "codeIng": null
      }
    }
  },
  created() {
    this.initRuleEngine();
  },
  methods: {
    // 生成规则版本
    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.engineCode + "_" + this.engineVersionForm.code
      this.engineVersionForm.codeIng = this.getCodeIng()
    },
    // 确定新增
    addVersionSubmission(){

    },
    // 取消新增
    addVersionCancel(){

    },
    handleClick(clickType){
      if (clickType === "add"){
        this.addVersionStatus = true;
      }else if(clickType === "activate"){
        this.ruleEngineInfo.isActivate = 'no-activate' === this.ruleEngineInfo.isActivate ? 'activated' : 'no-activate'
        this.$modal.msgSuccess("引擎激活状态修改成功");
      }else if(clickType === "status"){
        this.ruleEngineInfo.status = '0' === this.ruleEngineInfo.status ? '1' : '0'
        this.$modal.msgSuccess("引擎状态操作成功");
      }
    },
    initRuleEngine(){
        let response = {
          "code": 200,
          "msg": "查询成功",
          "data": {
            "id": 1,
            "name": "邮箱规则",
            "type": "rule-custom",
            "isActivate": "no-activate",
            "status": "0",
            "description": "<p>根据邮箱规则进行数据校验，内包含所有邮箱校验规则</p>",
            "remark": "-",
            "code": "email",
            "engineCode": "engine_custom_email",
            "level": "data-field",
            "ruleEngineVersionList": [
              {
                "name": "163剔除", "code": "IAW3S02", "status": "0",
                "versionCode": "engine_custom_email_IAW3S02", "isActivate": "no-activate",
                "description": "<p>根据邮箱规则进行数据校验，内包含所有邮箱校验规则</p>"
              },
              {
                "name": "筛选不合规邮箱", "code": "JISID8S", "status": "2",
                "versionCode": "engine_custom_email_JISID8S", "isActivate": "activated",
                "description": "<p>根据邮箱规则进行数据校验，内包含所有邮箱校验规则</p>"
              },
              {
                "name": "剔除自定义邮箱", "code": "DJ8S9EA", "status": "2",
                "versionCode": "engine_custom_email_DJ8S9EA", "isActivate": "no-activate",
                "description": "<p>根据邮箱规则进行数据校验，内包含所有邮箱校验规则</p>"
              },
              {
                "name": "邮箱默认值操作", "code": "XJS9E0S", "status": "1",
                "versionCode": "engine_custom_email_XJS9E0S", "isActivate": "no-activate",
                "description": "<p>根据邮箱规则进行数据校验，内包含所有邮箱校验规则</p>"
              }
            ]
          }
        }
        this.ruleEngineInfo = response.data
    },
    // 获取代码模板
    getCodeIng(){

      let packageName = "com.muyu.rule.engine";
      let customName = "custom";
      let templateName = "template";
      let parentClass = "DataModelEngine";
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} {
    @Override
    public void execution () {
        Object value = getValue();

        if (value == null || "".equals(value) || "null".equals(value)) {
            throw new ActionDiscard();
        }
    }
}`;
      return codeIng;
    }

  }
}
</script>

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