<template>
  <div class="tj-table">
    <MOSTY.Table ref="tableRef" :table-columns="tableColumn" :tableData="tableData" :tableloading="tableloading"
      :tableHeight="'100%'" :isSelect="false" style="width: 100%;" :isSort="false">
    </MOSTY.Table>
  </div>
</template>

<script setup>
// 引入
import { reactive, onMounted, ref, nextTick,defineProps, defineExpose } from "vue";
import { getApi } from "@/api/tobAcco_api.js";
import * as MOSTY from "@/components/MyComponents/index";
import { add } from "@/utils/calc.js";


const props = defineProps({
  listQuery: {
    type: Object,
    default: () => ({}),
  },
})
const tableData = ref([]);
const tableColumn = ref([{
  title: "单位名称",
  prop: "orgname",
  width: '300',
  fixed: 'left',
}])
const tableloading = ref(false);

const getTableData = () => {
  tableloading.value = true;
  getApi(props.listQuery, '/mosty-jcgl/ysTj/cwkmList').then((res) => {
    if (res) {
      let resArr = Array.isArray(res) ? res : [];
      resArr.forEach(item => {
        item.rate = item.rate > 0 ? `${item.rate}%` : 0;
      })
      let totalObj = {
        orgname: '合计',
        sumBudget: 0,
        sumCostFee: 0,
        budget1: 0,
        budget2: 0,
        budget3: 0,
        budget4: 0,
        budget5: 0,
        budget6: 0,
        budget7: 0,
        budget8: 0,
        budget9: 0,
        budget10: 0,
        budget11: 0,
        budget12: 0,
        budgetmoney1: 0,
        budgetmoney2: 0,
        budgetmoney3: 0,
        budgetmoney4: 0,
        budgetmoney5: 0,
        budgetmoney6: 0,
        budgetmoney7: 0,
        budgetmoney8: 0,
        budgetmoney9: 0,
        budgetmoney10: 0,
        budgetmoney11: 0,
        budgetmoney12: 0,
        cost_fee1: 0,
        cost_fee2: 0,
        cost_fee3: 0,
        cost_fee4: 0,
        cost_fee5: 0,
        cost_fee6: 0,
        cost_fee7: 0,
        cost_fee8: 0,
        cost_fee9: 0,
        cost_fee10: 0,
        cost_fee11: 0,
        cost_fee12: 0,
        rate: 0,

      }
      // 计算合计
      resArr.forEach(item => {
        totalObj.sumBudget = add(totalObj.sumBudget, item.sumBudget);
        totalObj.sumCostFee = add(totalObj.sumCostFee, item.sumCostFee);

        totalObj.budgetmoney1 = add(totalObj.budgetmoney1, item.budgetmoney1);
        totalObj.budgetmoney2 = add(totalObj.budgetmoney2, item.budgetmoney2);
        totalObj.budgetmoney3 = add(totalObj.budgetmoney3, item.budgetmoney3);
        totalObj.budgetmoney4 = add(totalObj.budgetmoney4, item.budgetmoney4);
        totalObj.budgetmoney5 = add(totalObj.budgetmoney5, item.budgetmoney5);
        totalObj.budgetmoney6 = add(totalObj.budgetmoney6, item.budgetmoney6);
        totalObj.budgetmoney7 = add(totalObj.budgetmoney7, item.budgetmoney7);
        totalObj.budgetmoney8 = add(totalObj.budgetmoney8, item.budgetmoney8);
        totalObj.budgetmoney9 = add(totalObj.budgetmoney9, item.budgetmoney9);
        totalObj.budgetmoney10 = add(totalObj.budgetmoney10, item.budgetmoney10);
        totalObj.budgetmoney11 = add(totalObj.budgetmoney11, item.budgetmoney11);
        totalObj.budgetmoney12 = add(totalObj.budgetmoney12, item.budgetmoney12);

        totalObj.cost_fee1 = add(totalObj.cost_fee1, item.cost_fee1);
        totalObj.cost_fee2 = add(totalObj.cost_fee2, item.cost_fee2);
        totalObj.cost_fee3 = add(totalObj.cost_fee3, item.cost_fee3);
        totalObj.cost_fee4 = add(totalObj.cost_fee4, item.cost_fee4);
        totalObj.cost_fee5 = add(totalObj.cost_fee5, item.cost_fee5);
        totalObj.cost_fee6 = add(totalObj.cost_fee6, item.cost_fee6);
        totalObj.cost_fee7 = add(totalObj.cost_fee7, item.cost_fee7);
        totalObj.cost_fee8 = add(totalObj.cost_fee8, item.cost_fee8);
        totalObj.cost_fee9 = add(totalObj.cost_fee9, item.cost_fee9);
        totalObj.cost_fee10 = add(totalObj.cost_fee10, item.cost_fee10);
        totalObj.cost_fee11 = add(totalObj.cost_fee11, item.cost_fee11);
        totalObj.cost_fee12 = add(totalObj.cost_fee12, item.cost_fee12);

        totalObj.budget1 = add(totalObj.budget1, item.budget1);
        totalObj.budget2 = add(totalObj.budget2, item.budget2);
        totalObj.budget3 = add(totalObj.budget3, item.budget3);
        totalObj.budget4 = add(totalObj.budget4, item.budget4);
        totalObj.budget5 = add(totalObj.budget5, item.budget5);
        totalObj.budget6 = add(totalObj.budget6, item.budget6);
        totalObj.budget7 = add(totalObj.budget7, item.budget7);
        totalObj.budget8 = add(totalObj.budget8, item.budget8);
        totalObj.budget9 = add(totalObj.budget9, item.budget9);
        totalObj.budget10 = add(totalObj.budget10, item.budget10);
        totalObj.budget11 = add(totalObj.budget11, item.budget11);
        totalObj.budget12 = add(totalObj.budget12, item.budget12);
      })
      totalObj.rate = (totalObj.sumCostFee / totalObj.sumBudget * 100).toFixed(2) + '%';
      tableData.value = [totalObj, ...resArr];
    }
  }).finally(() => {
    tableloading.value = false;
  })
}
const setColumn = () => {
  let dic = [
    { title: "消防警卫费用", dm: 1, },
    { title: "安全生产教育培训费", dm: 2, },
    { title: "资本性投资计划预算费用", dm: 3, },
    { title: "劳动防护费用", dm: 4, },
    { title: "信息系统建设维护费用", dm: 5, },
    { title: "中介费、咨询费", dm: 6, },
    { title: "安全生产责任保险投保费用", dm: 7, },
    { title: "安全奖励", dm: 8, },
    { title: "安全管理人员薪酬", dm: 9, },
    { title: "其他涉及安全风险分级管控与事故隐患排查治理等与安全生产直接相关的其它支出", dm: 10, },
    { title: "三同时安全设施、设施设备支出", dm: 11, },
    { title: "新改扩建项目安全评价投入", dm: 12, },
  ]
  /** 单位列 */
  const orgColumn = {
    title: "单位名称",
    prop: "orgname",
    width: '300',
    fixed: 'left',
  }
  const pfColumn = {
    title: "批复总金额",
    prop: "sumBudget",
    width: '130',
    fixed: 'left',
  }

  const costColumn = {
    title: "已执行",
    prop: "sumCostFee",
    width: '130',
    fixed: 'right',
  }

  const reteColumn = {
    title: "执行率",
    prop: "rate",
    width: '130',
    fixed: 'right',
  }

  /** 费用列 */
  const feeColumn = dic.map(item => {
    let obj = {
      title: item.title,
      suntab: [
        { title: "上报数", prop: `budgetmoney${item.dm}`, width: '130', },
        { title: "批复", prop: `budget${item.dm}`, width: '130', },
        { title: "已执行", prop: `cost_fee${item.dm}`, width: '130', }
      ]
    }
    return obj;
  })
  tableColumn.value = [orgColumn, pfColumn, ...feeColumn, costColumn, reteColumn];
}
setColumn();
defineExpose({
  getTableData,
})
onMounted(() => {
  getTableData();
})
</script>

<style scoped>
.tj-table {
  background-color: #fff;
  height: calc(100vh - 220px);
}
</style>
