<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";

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/yslbList').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 = totalObj.sumBudget + (item.sumBudget || 0);
        totalObj.sumCostFee = totalObj.sumCostFee + (item.sumCostFee || 0);
        totalObj.budgetmoney1 = totalObj.budgetmoney1 + (item.budgetmoney1 || 0);
        totalObj.budgetmoney2 = totalObj.budgetmoney2 + (item.budgetmoney2 || 0);
        totalObj.budgetmoney3 = totalObj.budgetmoney3 + (item.budgetmoney3 || 0);
        totalObj.budgetmoney4 = totalObj.budgetmoney4 + (item.budgetmoney4 || 0);
        totalObj.budgetmoney5 = totalObj.budgetmoney5 + (item.budgetmoney5 || 0);
        totalObj.budgetmoney6 = totalObj.budgetmoney6 + (item.budgetmoney6 || 0);
        totalObj.budgetmoney7 = totalObj.budgetmoney7 + (item.budgetmoney7 || 0);
        totalObj.budgetmoney8 = totalObj.budgetmoney8 + (item.budgetmoney8 || 0);
        totalObj.budgetmoney9 = totalObj.budgetmoney9 + (item.budgetmoney9 || 0);

        totalObj.cost_fee1 = totalObj.cost_fee1 + (item.cost_fee1 || 0);
        totalObj.cost_fee2 = totalObj.cost_fee2 + (item.cost_fee2 || 0);
        totalObj.cost_fee3 = totalObj.cost_fee3 + (item.cost_fee3 || 0);
        totalObj.cost_fee4 = totalObj.cost_fee4 + (item.cost_fee4 || 0);
        totalObj.cost_fee5 = totalObj.cost_fee5 + (item.cost_fee5 || 0);
        totalObj.cost_fee6 = totalObj.cost_fee6 + (item.cost_fee6 || 0);
        totalObj.cost_fee7 = totalObj.cost_fee7 + (item.cost_fee7 || 0);
        totalObj.cost_fee8 = totalObj.cost_fee8 + (item.cost_fee8 || 0);
        totalObj.cost_fee9 = totalObj.cost_fee9 + (item.cost_fee9 || 0);


        totalObj.budget1 = totalObj.budget1 + (item.budget1 || 0);
        totalObj.budget2 = totalObj.budget2 + (item.budget2 || 0);
        totalObj.budget3 = totalObj.budget3 + (item.budget3 || 0);
        totalObj.budget4 = totalObj.budget4 + (item.budget4 || 0);
        totalObj.budget5 = totalObj.budget5 + (item.budget5 || 0);
        totalObj.budget6 = totalObj.budget6 + (item.budget6 || 0);
        totalObj.budget7 = totalObj.budget7 + (item.budget7 || 0);
        totalObj.budget8 = totalObj.budget8 + (item.budget8 || 0);
        totalObj.budget9 = totalObj.budget9 + (item.budget9 || 0);
      })
      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, },
  ]
  /** 单位列 */
  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>
