<template>
  <a-table ref="tableRef0"
           row-key="contractSettleId" :dataSource="dataList" :columns="columns">
    <template #bodyCell="{ column, record, index }">
      <template v-if="column.key === 'singleAmount'">
        <a-input v-model:value="record.singleAmount" type="number" />
      </template>
      <template v-if="column.key === 'inDate'">
        <a-date-picker
          class="ele-fluid"
          placeholder="请选择实际进场日期"
          v-model:value="record.inDate"
          valueFormat="YYYY-MM-DD"
        />
      </template>
      <template v-if="column.key === 'outDate'">
        <a-date-picker
          class="ele-fluid"
          placeholder="请选择实际出场日期"
          v-model:value="record.outDate"
          valueFormat="YYYY-MM-DD"
        />
      </template>
      <template v-if="column.key === 'num'">
        <a-input v-model:value="record.num" type="number" />
      </template>
      <template v-if="column.key === 'payRate'">
        <a-input v-model:value="record.payRate" type="number">
          <template #addonAfter>%</template>
        </a-input>
      </template>
      <template v-if="column.key === 'amountWithTax'">
        <span
          v-if="record.singleAmount && record.num">{{ ((record.singleAmount * record.num) * record.payRate / 100).toFixed(2)
          }}</span>
      </template>
      <template v-if="column.key === 'tax'">
        <span
          v-if="record.singleAmount && record.num">{{ (((record.singleAmount * record.num) * record.payRate / 100) * (record.taxRate / 100)).toFixed(2)
          }}</span>
      </template>
      <template v-if="column.key === 'amountWithoutTax'">
        <span
          v-if="record.singleAmount && record.num">{{ (((record.singleAmount * record.num) * record.payRate / 100) - (record.singleAmount * record.num * (record.taxRate / 100))).toFixed(2)
          }}</span>
      </template>
      <template v-if="column.key === 'remark'">
        <a-input v-model:value="record.remark" />
      </template>
      <template v-if="column.key === 'action'">
        <div class="flex justify-center items-center">
          <close-circle-outlined style="font-size: 1.5rem; color: pink;cursor: pointer"
                                 @click.native="del(index)" />
        </div>
      </template>
    </template>
    <template #summary>
      <a-table-summary-row>
        <a-table-summary-cell>合计</a-table-summary-cell>
        <a-table-summary-cell />
        <a-table-summary-cell />
        <a-table-summary-cell />
        <a-table-summary-cell />
        <a-table-summary-cell />
        <a-table-summary-cell />
        <a-table-summary-cell />
        <a-table-summary-cell />
        <a-table-summary-cell>
          {{ totals.amountWithTax }}
        </a-table-summary-cell>
        <a-table-summary-cell>
          {{ totals.tax }}
        </a-table-summary-cell>
        <a-table-summary-cell>
          {{ totals.amountWithoutTax }}
        </a-table-summary-cell>
        <a-table-summary-cell />
        <a-table-summary-cell />
      </a-table-summary-row>
    </template>
  </a-table>
</template>

<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import { SettleDetail } from "@/api/tower/settleDetail/model";
import { ColumnItem } from "ele-admin-pro/es/ele-pro-table/types";
import { assignObject, EleProTable } from "ele-admin-pro";
import {
  CloseCircleOutlined
} from "@ant-design/icons-vue";
import dayjs from "dayjs";

const props = defineProps<{
  dataSource: SettleDetail[]
}>();
const tableRef0 = ref<InstanceType<typeof EleProTable> | null>(null);

const columns = ref<ColumnItem[]>([
  {
    title: "序号",
    key: "index",
    width: 48,
    align: "center",
    fixed: "left",
    hideInSetting: true,
    customRender: ({ index }) => index + (tableRef0.value?.tableIndex ?? 1)
  },
  {
    title: "设备名",
    dataIndex: "equipmentName",
    key: "equipmentName"
  },
  {
    title: "设备型号",
    dataIndex: "equipmentModel",
    key: "equipmentModel"
  },
  {
    title: "出场编号",
    dataIndex: "factoryNo",
    key: "factoryNo"
  },
  {
    title: "实际进场日期",
    dataIndex: "inDate",
    key: "inDate"
  },
  {
    title: "实际出场日期",
    dataIndex: "outDate",
    key: "outDate"
  },
  {
    title: "费用类型",
    dataIndex: "settleCostType",
    key: "settleCostType"
  },
  {
    title: "单价",
    dataIndex: "singleAmount",
    key: "singleAmount"
  },
  {
    title: "次数",
    dataIndex: "num",
    key: "num"
  },
  {
    title: "支付比例",
    dataIndex: "payRate",
    key: "payRate"
  },
  {
    title: "含税金额(元)",
    dataIndex: "amountWithTax",
    key: "amountWithTax"
  },
  {
    title: "税金(元)",
    dataIndex: "tax",
    key: "tax"
  },
  {
    title: "不含税总价(元)",
    dataIndex: "amountWithoutTax",
    key: "amountWithoutTax"
  },
  {
    title: "备注",
    dataIndex: "remark",
    key: "remark"
  },
  {
    title: "操作",
    dataIndex: "action",
    key: "action"
  }
]);

const dataList = ref<SettleDetail[]>([]);

const del = (index: number) => {
  dataList.value.splice(index, 1);
  calTotals();
};

const totals = ref({ amountWithTax: 0, tax: 0, amountWithoutTax: 0 });
const calTotals = () => {
  let amountWithTax = 0;
  let tax = 0;
  let amountWithoutTax = 0;
  if (dataList.value && dataList.value.length > 0 && (dataList.value[0].taxRate !== undefined)) {
    const taxRate = dataList.value[0].taxRate;
    dataList.value.forEach(({ singleAmount, num, payRate }) => {
      amountWithTax += singleAmount * num * payRate / 100;
    });
    amountWithTax = Number(amountWithTax.toFixed(2));
    tax = amountWithTax * (taxRate / 100);
    tax = Number(tax.toFixed(2));
    amountWithoutTax = amountWithTax - tax;
    amountWithoutTax = Number(amountWithoutTax.toFixed(2));
  }
  totals.value = { amountWithTax, tax, amountWithoutTax };
};

onMounted(() => {
  dataList.value = props.dataSource;
  calTotals();
});

watch(
  () => props.dataSource,
  (dataSource) => {
    dataList.value = dataSource;
    console.log(dataList.value);
  },
  { deep: true }
);

defineExpose({ dataList });

</script>
