<template>
<a-drawer :title="drawerTitle" placement="right" width="100%" :visible="isVisible" @close="closeDrawer">
  <a-card
    style="width: 100%"
    :tab-list="tabList"
    @tabChange="key => onTabChange(key)"
  >  
  <vxe-grid v-bind="gridOptions">
    </vxe-grid>

    <template #tabBarExtraContent>
        <vxe-button status="primary" @click="validateRule()">开始校验</vxe-button>
        <!-- <vxe-button mode="text" status="primary" @click="selectEvent()">显示接口</vxe-button> -->
    </template>
  </a-card>

  <vxe-drawer
      show-footer
      show-confirm-button
      show-cancel-button
      v-model="showPopup"
      title="接口列表"
      width="50%"
      @show="showSubEvent"
      @confirm="confirmSubEvent">
      <vxe-grid ref="productGridRef" v-bind="productGridOptions"  v-on="gridEvents"></vxe-grid>

      <div style="margin:10px">
        <a-divider style="border-color: #7cb305" dashed></a-divider>
      
          <a-button type="link" @click="getAPIData()">获取数据</a-button>
          <a-button type="link" @click="setAPIData()">保存数据</a-button>
          
      </div>
    </vxe-drawer>
</a-drawer>
</template>

<script lang="ts" setup>
import { ref,reactive } from 'vue'
import { VxeGridProps, VxeGridInstance,VxeGridListeners } from 'vxe-table'
import { defHttp } from '/@/utils/http/axios';

const actiiveKey = ref('tab1');

const isVisible = ref(false)

 const drawerTitle= ref();
 const curTpl = ref()
 const fillData =ref({})


const closeDrawer = () => {
  isVisible.value = false
}

const onTabChange = (value: string) => {
  actiiveKey.value = value
  if(actiiveKey.value =="tab1"){
    gridOptions.data = data1.value
  } else if(actiiveKey.value =="tab2"){
    gridOptions.data = data2.value

  } else if(actiiveKey.value =="tab3"){
    gridOptions.data = data3.value
  }
  
};

const tabList = [
  {
    key: 'tab1',tab: '校验关系-表内校验',
  },{
    key: 'tab2',tab: '校验关系-表间校验',
  },{
    key: 'tab3',tab: '自动计算',
  },
];

async function setIniData(tplName,data) {
   isVisible.value = true
   drawerTitle.value="表【"+tplName+"】";
   isVisible.value = true
   fillData.value = await arrayToObject(data);
 
}


const data1 = ref([
    { name: '1.法人境内外资产总额>0，机构网点（职场）数量>=0', 
      code:"法人境内外资产总额:C23A001，<br>机构网点（职场）数量：C23A002", 
      formula: 'C23A001>0 && C23A002>=0', 
      fillValue: 'C23A001:100000,C23A002:50', 
      sta: '',
      params:["C23A001","C23A002"]
    },
    { name: '2.设有境内分支机构（或分公司）的省级行政区数量<=36。公司账户数>=0，个人账户数>=0，公司客户数>=0，个人客户数>=0',
      code:"省级行政区数量:C23A003<br>公司账户数:C23A004<br>个人账户数:C23A005<br>公司客户数:C23A006<br>个人客户数:C23A007", 
      formula: 'C23A003<=36 && C23A004>=0 && C23A005>=0 && C23A006>=0 && C23A007>=0', 
      fillValue: 'C23A003:25,C23A004:10000,C23A005:50000,C23A006:8000,C23A007:45000', 
      sta: "",
      params:["C23A003","C23A004","C23A005","C23A006","C23A007"]
    },
    
    { name: '3.本年度信息科技投入中外包费用<=本年度信息科技投入',
      code:"信息科技投入:C23B001<br>外包费用:C23B002", 
      formula: 'C23B002<=C23B001', 
      fillValue: 'C23B001:50000000,C23B002:20000000', 
      sta:"",
      params:["C23B001","C23B002"]
    },
    
    { name: '4.本年度信息科技投入中信息安全费用<=本年度信息科技投入',
      code:"信息科技投入:C23B001<br>信息安全费用:C23B003", 
      formula: 'C23B003<=C23B001', 
      fillValue: 'C23B001:50000000,C23B003:10000000', 
      sta: "",
      params:["C23B001","C23B003"]
    },
    
    { name: '5.规划d期：A 年到 B 年，A<=B',
      code:"规划开始年:C23C001<br>规划结束年:C23C002", 
      formula: 'C23C001<=C23C002', 
      fillValue: 'C23C001:2023,C23C002:2025', 
      sta: "",
      params:["C23C001","C23C002"]
    },
    
    { name: '6.本年度信息科技投入＝基础设施建设投入＋电子设备采购投入＋软件采购投入＋系统开发项目投入＋系统运维投入＋信息科技咨询投入＋信息科技人力资源费用＋其他',
      code:"信息科技投入:C23B001<br>基础设施:C23B004<br>电子设备:C23B005<br>软件采购:C23B006<br>系统开发:C23B007<br>系统运维:C23B008<br>咨询投入:C23B009<br>人力资源:C23B010<br>其他投入:C23B011", 
      formula: 'C23B001==(C23B004+C23B005+C23B006+C23B007+C23B008+C23B009+C23B010+C23B011)', 
      fillValue: 'C23B001:50000000,C23B004:10000000,C23B005:8000000,C23B006:7000000,C23B007:9000000,C23B008:6000000,C23B009:3000000,C23B010:5000000,C23B011:2000000', 
      sta: "",
      params:["C23B001","C23B004","C23B005","C23B006","C23B007","C23B008","C23B009","C23B010","C23B011"]
    },
    
    { name: '7.信息科技正式员工数量<正式员工数量',
      code:"信息科技员工:C23D001<br>正式员工:C23D002", 
      formula: 'C23D001<C23D002', 
      fillValue: 'C23D001:150,C23D002:2000', 
      sta: "",
      params:["C23D001","C23D002"]
    },
    
    { name: '8.信息科技正式员工数量=正式员工数量合计（信息科技人员情况）',
      code:"信息科技员工总数:C23D001<br>项目管理:C23D003<br>开发:C23D004<br>测试:C23D005<br>运维:C23D006<br>安全:C23D007<br>其他:C23D008", 
      formula: 'C23D001==(C23D003+C23D004+C23D005+C23D006+C23D007+C23D008)', 
      fillValue: 'C23D001:150,C23D003:20,C23D004:50,C23D005:30,C23D006:25,C23D007:15,C23D008:10', 
      sta: "",
      params:["C23D001","C23D003","C23D004","C23D005","C23D006","C23D007","C23D008"]
    },
    
    { name: '9.本年度信息科技正式员工净流失率<=100%，可以为负数',
      code:"年初人数:C23D009<br>年末人数:C23D010", 
      formula: '((C23D009-C23D010)/C23D009)*100<=100', 
      fillValue: 'C23D009:150,C23D010:140', 
      sta: "",
      params:["C23D009","C23D010"]
    },
    
    { name: '10.本年度信息科技正式员工流失率>=本年度信息科技正式员工净流失率',
      code:"离职人数:C23D011<br>年初人数:C23D009<br>年末人数:C23D010", 
      formula: '(C23D011/C23D009)*100>=((C23D009-C23D010)/C23D009)*100', 
      fillValue: 'C23D011:20,C23D009:150,C23D010:140', 
      sta: "",
      params:["C23D011","C23D009","C23D010"]
    },
    
    { name: '11.持有专业高级资质的信息科技正式员工总数<=各分项资质人数之和',
      code:"高级资质总数:C23D012<br>安全管理资质:C23D013<br>服务管理资质:C23D014<br>项目管理资质:C23D015<br>网络管理资质:C23D016<br>国外数据库:C23D017<br>国产数据库:C23D018<br>业务连续性:C23D019<br>审计类:C23D020<br>其他资质:C23D021", 
      formula: 'C23D012<=(C23D013+C23D014+C23D015+C23D016+C23D017+C23D018+C23D019+C23D020+C23D021)', 
      fillValue: 'C23D012:50,C23D013:10,C23D014:8,C23D015:7,C23D016:6,C23D017:5,C23D018:4,C23D019:3,C23D020:2,C23D021:5', 
      sta: "",
      params:["C23D012","C23D013","C23D014","C23D015","C23D016","C23D017","C23D018","C23D019","C23D020","C23D021"]
    },
    
    { name: '12.持有专业高级资质的信息科技正式员工总数<信息科技正式员工数量',
      code:"高级资质总数:C23D012<br>信息科技员工:C23D001", 
      formula: 'C23D012<C23D001', 
      fillValue: 'C23D012:50,C23D001:150', 
      sta: "",
      params:["C23D012","C23D001"]
    },
    
    { name: '13.信息科技投入占本年度总投入的比例<100%',
      code:"信息科技投入:C23B001<br>总投入:C23B012", 
      formula: '(C23B001/C23B012)*100<100', 
      fillValue: 'C23B001:50000000,C23B012:100000000', 
      sta: "",
      params:["C23B001","C23B012"]
    },
    
    { name: '14.本年度信息科技入职正式员工数量<正式员工数量',
      code:"入职人数:C23D022<br>正式员工:C23D002", 
      formula: 'C23D022<C23D002', 
      fillValue: 'C23D022:30,C23D002:2000', 
      sta: "",
      params:["C23D022","C23D002"]
    },
    
    { name: '15.信息科技风险管理人员数量>=专职信息科技风险管理人员数量>=其中具有信息科技背景的人数',
      code:"风险管理人员:C23E001<br>专职人员:C23E002<br>有背景人员:C23E003", 
      formula: 'C23E001>=C23E002 && C23E002>=C23E003', 
      fillValue: 'C23E001:15,C23E002:10,C23E003:8', 
      sta: "",
      params:["C23E001","C23E002","C23E003"]
    },
    
    { name: '16.信息科技风险评估情况中，发现问题数量>=整改问题数量',
      code:"发现问题总数:C23E004<br>整改问题数量:C23E005", 
      formula: 'C23E004>=C23E005', 
      fillValue: 'C23E004:20,C23E005:18', 
      sta: "",
      params:["C23E004","C23E005"]
    },
    
    { name: '17.信息科技内审人员数量>=专职信息科技内审人员数量>=其中具有信息科技背景的人数',
      code:"内审人员总数:C23E006<br>专职内审人员:C23E007<br>有背景内审人员:C23E008", 
      formula: 'C23E006>=C23E007 && C23E007>=C23E008', 
      fillValue: 'C23E006:12,C23E007:8,C23E008:6', 
      sta: "",
      params:["C23E006","C23E007","C23E008"]
    },
    
    { name: '18.历史信息科技审计发现问题总数>=已整改问题数量',
      code:"历史发现问题:C23E009<br>历史已整改:C23E010", 
      formula: 'C23E009>=C23E010', 
      fillValue: 'C23E009:50,C23E010:45', 
      sta: "",
      params:["C23E009","C23E010"]
    },
    
    { name: '19.近三年审计工作明细中，发现问题数量>=整改问题数量',
      code:"审计发现问题:C23E011<br>审计整改问题:C23E012", 
      formula: 'C23E011>=C23E012', 
      fillValue: 'C23E011:30,C23E012:28', 
      sta: "",
      params:["C23E011","C23E012"]
    },
    
    { name: '20.本年度开展信息科技审计项目数量为0时，则"本年度开展业务连续性审计"选"否"',
      code:"审计项目数量:C23E013<br>业务连续性审计:C23E014", 
      formula: 'C23E013!=0 || (C23E013==0 && C23E014=="否")', 
      fillValue: 'C23E013:0,C23E014:否', 
      sta: "",
      params:["C23E013","C23E014"]
    },
    
    { name: '21.本年度开展信息科技审计项目数量为0时，"本年度信息科技审计发现问题总数"和"本年度信息科技审计发现问题已整改"均为0',
      code:"审计项目数量:C23E013<br>发现问题总数:C23E015<br>已整改问题:C23E016", 
      formula: 'C23E013!=0 || (C23E013==0 && C23E015==0 && C23E016==0)', 
      fillValue: 'C23E013:0,C23E015:0,C23E016:0', 
      sta: "",
      params:["C23E013","C23E015","C23E016"]
    },
    
    { name: '22.附件"信息科技治理情况明细报告"不能为空，需上传',
      code:"附件索引:C23F001", 
      formula: 'C23F001!="无"', 
      fillValue: 'C23F001:已上传', 
      sta: "",
      params:["C23F001"]
    },
    
    { name: '23.若"已制定并印发信息科技战略规划"选"是"，则附件"信息科技战略规划"需上传',
      code:"是否制定战略规划:C23F002<br>战略规划附件:C23F003", 
      formula: 'C23F002!="是" || (C23F002=="是" && C23F003!="无")', 
      fillValue: 'C23F002:是,C23F003:已上传', 
      sta: "",
      params:["C23F002","C23F003"]
    },
    
    { name: '24.若"制定并实施数字化转型战略规划"选"是"，则附件"数字化转型战略规划"必上传',
      code:"是否数字转型:C23F004<br>转型规划附件:C23F005", 
      formula: 'C23F004!="是" || (C23F004=="是" && C23F005!="无")', 
      fillValue: 'C23F004:是,C23F005:已上传', 
      sta: "",
      params:["C23F004","C23F005"]
    },
    
    { name: '25.附件"信息科技管理制度汇编"必须上传',
      code:"制度汇编附件:C23F006", 
      formula: 'C23F006!="无"', 
      fillValue: 'C23F006:已上传', 
      sta: "",
      params:["C23F006"]
    },
    
    { name: '26.若"建立信息科技风险管理策略、制度"选"是"，则附件"信息科技风险管理制度汇编"必上传',
      code:"是否风险管理:C23F007<br>风险管理制度附件:C23F008", 
      formula: 'C23F007!="是" || (C23F007=="是" && C23F008!="无")', 
      fillValue: 'C23F007:是,C23F008:已上传', 
      sta: "",
      params:["C23F007","C23F008"]
    },
    
    { name: '27.若"建立信息科技审计制度"选"是"，则附件"信息科技审计制度汇编"必上传',
      code:"是否审计制度:C23F009<br>审计制度附件:C23F010", 
      formula: 'C23F009!="是" || (C23F009=="是" && C23F010!="无")', 
      fillValue: 'C23F009:是,C23F010:已上传', 
      sta: "",
      params:["C23F009","C23F010"]
    },
    
    { name: '28.若"本年度董事会召开涉及信息科技议题的会议数量"大于0，或"形成信息科技工作决议"大于0，则附件"董事会会议纪要、工作决议"必上传',
      code:"会议数量:C23F011<br>工作决议数量:C23F012<br>会议纪要附件:C23F013", 
      formula: '(C23F011<=0 && C23F012<=0) || (C23F011>0 || C23F012>0) && C23F013!="无"', 
      fillValue: 'C23F011:2,C23F012:1,C23F013:已上传', 
      sta: "",
      params:["C23F011","C23F012","C23F013"]
    },
    
    { name: '29.若"本年度委员会全体会议次数"大于0，则附件"信息科技管理委员会会议纪要、工作决议"必上传',
      code:"会议次数:C23F014<br>会议纪要附件:C23F015", 
      formula: 'C23F014<=0 || (C23F014>0 && C23F015!="无")', 
      fillValue: 'C23F014:4,C23F015:已上传', 
      sta: "",
      params:["C23F014","C23F015"]
    },

    { name: '30.附件"信息科技组织架构图"非必传',
      code:"组织架构图附件:C23F016", 
      formula: 'true',
      fillValue: 'C23F016:未上传', 
      sta: "",
      params:["C23F016"]},
    
    { name: '31.若"设立数据管理部门（组织）"选"是"，则附件"数据管理组织架构图"必上传',
      code:"是否数据部门:C23F017<br>数据架构图附件:C23F018", 
      formula: 'C23F017!="是" || (C23F017=="是" && C23F018!="无")', 
      fillValue: 'C23F017:是,C23F018:已上传', 
      sta: "",
      params:["C23F017","C23F018"]
    },
    
    { name: '32.若"形成年度信息科技全面风险管理报告"选"是"，则附件"信息科技全面风险评估报告"必上传',
      code:"是否风险评估报告:C23F019<br>评估报告附件:C23F020", 
      formula: 'C23F019!="是" || (C23F019=="是" && C23F020!="无")', 
      fillValue: 'C23F019:是,C23F020:已上传', 
      sta: "",
      params:["C23F019","C23F020"]
    },
    
    { name: '33.附件"信息科技审计报告"非必传',
      code:"审计报告附件:C23F021", 
      formula: 'true', 
      fillValue: 'C23F021:未上传', 
      sta: "",
      params:["C23F021"]
    }
])

  const data2 = ref([
    { name: '1、T-B-1 报表中指标“本年度信息科技投入”>=T-B-11报表中指标“本年度新采购软件情况-合计金额”',code:"1", formula: 'Develop', fillValue: 'Man', sta: '',},
    { name: '2、T-B-1 报表中指标“本年度信息科技投入”>=T-B-11报表中指标“本年度软件升级及维护情况(不含新采购软件)-合计金额”',code:"1", formula: 'Test', fillValue: 'Women', sta: "错误"},
    { name: '3、T-B-1 报表中指标“本年度风险评估覆盖重要信息系统数量”<=T-B-3报表中指标“重要信息系统总数（不含基础设施类）”',code:"1",formula: 'PM', fillValue: 'Man', sta:""},
 
  ])

  const data3 = ref([
    { name: '1、“信息科技资金投入”中计算公式：本年度信息科技投入＝本年度基础设施建设投入＋本年度电子设备采购投入＋本年度软件采购投入＋本年度系统开发项目投入＋本年度系统运维投入＋本年度信息科技咨询投入＋本年度信息科技人力资源费用＋本年度其他投入。',code:"1", formula: 'Develop', fillValue: 'Man', sta: '',},
    { name: '2、“信息科技人力资源”中 本年度信息科技正式员工流失率＝本年度信息科技离职正式员工数/本年初信息科技正式员工数×100%。 本年度信息科技正式员工净流失率＝（本年初信息科技正式员工数量-本年末信息科技正式员工数量）/本年初信息科技正式员工数量×100%。 正式员工数量合计（信息科技人员情况）=项目（需求）管理正式员工数量+信息科技开发正式工数量+信息科技测试正式员工数量+信息科技运行维护正式员工数量+信息安全正式员工数量+其他信息科技正式员工数量。 驻场信息科技外包人员数量＝驻场项目（需求）管理外包人员数量＋驻场开发外包人员数量＋驻场测试外包人员数量＋驻场运行维护外包人员数量＋驻场安全外包人员数量＋驻场其他外包人员数量。',code:"1", formula: 'Test', fillValue: 'Women', sta: "错误"},
    { name: '3、“信息科技风险评估情况”中本年度/上一年发现问题数量合计=（运行维护管理+开发测试管理+信息安全管理+业务连续性管理+外包管理+其他）合计发现问题数量。 本年度/上一年年整改问题数量合计=（运行维护管理+开发测试管理+信息安全管理+业务连续性管理+外包管理+其他）合计整改问题数量。',code:"1",formula: 'PM', fillValue: 'Man', sta:""},
    { name: '4、“近三年审计工作明细情况”中 发现问题数量合计=各审计项目发现问题数量加总合计；整改问题数量合计=各审计项目整改问题数量加总合计',code:"1",formula: 'PM', fillValue: 'Man', sta:""},
  ])

  
  const gridOptions = reactive<VxeGridProps<any>>({
  columnConfig: {
    useKey: true,
    resizable:true
  },
  columns: [
    { type: 'seq', width: 70 },
    { field: 'name', title: '规则描述',type:"html"},
    { field: 'code', title: '编码',width: "20%",type:"html"},
    { field: 'formula', title: '校验公式',width:"20%",type:"html"},
    { field: 'fillValue', title: '填报的值',width:"20%" ,type:"html"},
    { field: 'sta', title: '校验结果' ,width:"10%",type:"html"},
  ],
  data:data1.value
})


function validateRule() {
  let ruleDatas = [{}];
  if (actiiveKey.value === "tab1") {
    ruleDatas = data1.value;
  } else if (actiiveKey.value === "tab2") {
    ruleDatas = data2.value;
  } else {
    ruleDatas = data3.value;
  }

  ruleDatas.forEach(rule => {
    const values = {};
    // 收集参数值
    rule.params.forEach(param => {
      values[param] = fillData.value[param]; // 获取表单值的函数
    });

    // 替换公式中的参数
    let formula = rule.formula;
    rule.params.forEach(param => {
      const paramRegex = new RegExp(param, 'g');
      formula = formula.replace(paramRegex, values[param]);
    });

    // 计算并判断
    let result;
    try {
      result = eval(formula);
    } catch (e) {
      result = false; // 或其他错误处理
    }

    rule.sta = result ? "正确" : "错误";
    rule.fillValue = rule.params.map(p => `${p}:${values[p]}`).join(',');
  });
}

async function arrayToObject(arr) {
  const result = {};
  arr.forEach(item => {
    if (item.tplcode && item.content) {
      result[item.tplcode] = item.content;
    }
  });
  return result;
}


///////////////////////////////////////////////////////////////////////////

const showPopup = ref();
const selectRow = ref()
const productGridRef = ref<VxeGridInstance<any>>()
const selectEvent = () => {
  showPopup.value = true

}

const productGridOptions = reactive<VxeGridProps<any>>({
  border: true,
  height: '30%',
  rowConfig: {
    keyField: 'id',
    resizable: true
  },
  editConfig:{ 
    trigger: 'click',
    mode: 'row' 
  },
  columnConfig:{
     resizable: true
  },
  toolbarConfig: {
      buttons: [
        { name: '新增', code: 'add', mode:'text',status: 'primary',size:"mini"},
        { name: '删除', code: 'delete', mode:'text',status: 'primary',size:"mini"},
      ]
    },
  columns: [
    { type: 'radio', width: 60 },
    { field: 'name', title: 'API名称',width:"20%", editRender: {
      name: 'input',
      attrs: {
        type: 'input', 
      } },},
    { field: 'url', title: 'API地址',width:"20%", editRender: {
        name: 'input',
        attrs: {
          type: 'TextArea', 
        } 
      }
    },
    { field: 'params', title: '参数', editRender: { 
         name: 'input',
         attrs: {
          type: 'input', 
        } 
      },
    },
  ],
  data: [
    // { name: '机构规模', url:"http://47.98.203.68:8080/stm/api/bigscreen/getBaosongJggm" },
    // { name: '董事会',  url: "http://47.98.203.68:8080/stm/api/bigscreen/getBaosongDsh" },
  ],
  
})

const gridEvents: VxeGridListeners = {
  toolbarButtonClick(params) {
    if(params.code === "add") {
      let newRow = {}
      params.$grid.insert(newRow)
      params.$grid.setEditRow(newRow)
    } else if(params.code==="delete"){
      params.$grid.removeRadioRow();
    }
  }
}



const showSubEvent = () => {
  const $productGrid = productGridRef.value
  if ($productGrid) {
    const row = selectRow.value
    if (row && row.productId) {
      const productRow = $productGrid.getRowById(row.productId)
      $productGrid.setRadioRow(productRow)
    } else {
      $productGrid.clearRadioRow()
    }
  }
}

const confirmSubEvent = () => {
  const $productGrid = productGridRef.value
  if ($productGrid) {
    const row = selectRow.value
    const selectProduct = $productGrid.getRadioRecord()
    if (row && selectProduct) {
      row.productId = selectProduct.id
      row.productName = selectProduct.name
      row.productPrice = selectProduct.price
    }
  }
}

const getAPIData = async () => {
  const $table = productGridRef.value
  if ($table) {
    const selRecord = $table.getRadioRecord()
    try {
      const data = await defHttp.get({ 
        url: "/api/bigscreen/getBaosongJggm",
        headers: {
          'Accept': 'application/json',
          // Add other headers you see in browser's network tab
        }
      });
    } catch (err) {
      console.error("请求失败：", err);
    }
  }
}



const setAPIData = () => {

}



defineExpose({
   setIniData,
 });



</script>

<style scope>
  blockquote {
    padding: 0px 5px;
    color: black;
    cursor: pointer;
  }
</style>

