<template>
  <div class="tenant">
    <div class="content">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :selectConfig="selectConfig"
        highlight-current-row
        :action-list="[
          {
            text: '发起申请',
            action: () => purchasePayment(),
          },
        ]"
        @get-list="getList"
      >
        <template #amount="{ item }">
          <div>
            <span>{{ moneyFormat(item.amount, 2) }}</span>
          </div>
        </template>
      </byTable>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import byTable from "@/components/byTable/index";
import useUserStore from "@/store/modules/user";
// import { ElMessage } from "element-plus";

const { proxy } = getCurrentInstance();
const payStatus = ref([]);
const fundsPaymentMethod = ref([]);
const accountList = ref([]);
const status = ref([
  {
    label: "草稿",
    value: 0,
  },
  {
    label: "审批中",
    value: 10,
  },
  {
    label: "驳回",
    value: 20,
  },
  {
    label: "审批通过",
    value: 30,
  },
  {
    label: "终止",
    value: 99,
  },
]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    status: "",
    payStatus: "",
  },
});
const loading = ref(false);
const selectConfig = computed(() => {
  return [
    {
      label: "审批状态",
      prop: "status",
      data: status.value,
    },
    {
      label: "付款状态",
      prop: "payStatus",
      data: payStatus.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "供应商",
        prop: "supplyName",
        width: 200,
      },
    },
    {
      attrs: {
        label: "申请金额",
        slot: "amount",
        width: 140,
      },
    },
    {
      attrs: {
        label: "付款说明",
        prop: "remark",
      },
    },
    {
      attrs: {
        label: "付款方式",
        prop: "payType",
        width: 160,
      },
      render(type) {
        let text = "";
        if (fundsPaymentMethod.value && fundsPaymentMethod.value.length > 0) {
          let data = fundsPaymentMethod.value.filter(
            (item) => item.value == type
          );
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "付款账户",
        prop: "accountManagementId",
        width: 200,
      },
      render(type) {
        let text = "";
        if (accountList.value && accountList.value.length > 0) {
          let data = accountList.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "申请人",
        prop: "userName",
        width: 140,
      },
    },
    {
      attrs: {
        label: "申请时间",
        prop: "createTime",
        width: 160,
      },
    },
    {
      attrs: {
        label: "审批状态",
        prop: "status",
        width: 140,
      },
      render(type) {
        let text = "";
        if (status.value && status.value.length > 0) {
          let data = status.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "付款状态",
        prop: "payStatus",
        width: 140,
      },
      render(type) {
        let text = "";
        if (payStatus.value && payStatus.value.length > 0) {
          let data = payStatus.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "操作",
        width: "120",
        align: "center",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "打印",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickPrint(row);
            },
          },
        ];
      },
    },
  ];
});
const getDict = () => {
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      dictCode: "pay_status",
      tenantId: useUserStore().user.tenantId,
    })
    .then((res) => {
      if (res.rows && res.rows.length > 0) {
        payStatus.value = res.rows.map((item) => {
          return {
            label: item.dictValue,
            value: item.dictKey,
          };
        });
      }
    });
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      dictCode: "funds_payment_method",
      tenantId: useUserStore().user.tenantId,
    })
    .then((res) => {
      if (res.rows && res.rows.length > 0) {
        fundsPaymentMethod.value = res.rows.map((item) => {
          return {
            label: item.dictValue,
            value: item.dictKey,
          };
        });
      }
    });
  proxy
    .post("/accountManagement/page", { pageNum: 1, pageSize: 999 })
    .then((res) => {
      accountList.value = res.rows.map((item) => {
        return {
          label: item.alias,
          value: item.id,
        };
      });
    });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/pay/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getDict();
getList();
const purchasePayment = () => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "pay_flow",
      flowName: "采购付款申请",
    },
  });
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
