<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="[]"
        @get-list="getList">
        <template #amount="{ item }">
          <div>
            <span style="padding-right: 4px">{{ item.currency }}</span>
            <span>{{ moneyFormat(item.amount, 2) }}</span>
          </div>
        </template>
        <template #refundMoney="{ item }">
          <div>
            <span style="padding-right: 4px">{{ item.currency }}</span>
            <span>{{ moneyFormat(item.sumClaimMoney, 2) }}</span>
          </div>
        </template>
        <template #advanceRatio="{ item }">
          <div>
            <span>{{ item.advanceRatio }}%</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, ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();
const contractType = ref([]);
const corporationList = ref([]);
const customerList = ref([]);
const status = ref([
  {
    label: "草稿",
    value: 0,
  },
  {
    label: "审批中",
    value: 10,
  },
  {
    label: "驳回",
    value: 20,
  },
  {
    label: "审批通过",
    value: 30,
  },
  {
    label: "终止",
    value: 99,
  },
]);
const refundStatus = ref([
  {
    label: "未到款",
    value: 0,
  },
  {
    label: "部分到款",
    value: 10,
  },
]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    sellCorporationId: "",
  },
});
const loading = ref(false);
const selectConfig = computed(() => {
  return [
    {
      label: "到款状态",
      prop: "refundStatusNew",
      data: refundStatus.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "归属公司",
        prop: "sellCorporationId",
        "min-width": 220,
      },
      render(type) {
        let text = "";
        if (corporationList.value && corporationList.value.length > 0) {
          let data = corporationList.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "合同类型",
        prop: "contractType",
        width: 120,
      },
      render(type) {
        let text = "";
        if (contractType.value && contractType.value.length > 0) {
          let data = contractType.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "合同编码",
        prop: "code",
        width: 180,
      },
    },
    {
      attrs: {
        label: "客户",
        prop: "buyCorporationId",
        "min-width": 220,
      },
      render(type) {
        let text = "";
        if (customerList.value && customerList.value.length > 0) {
          let data = customerList.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "版本号",
        prop: "version",
        width: 120,
      },
    },
    {
      attrs: {
        label: "合同金额",
        slot: "amount",
        width: 140,
      },
    },
    {
      attrs: {
        label: "已到账金额",
        slot: "refundMoney",
        width: 140,
      },
    },
    {
      attrs: {
        label: "业务员",
        prop: "userName",
        width: 140,
      },
    },
    {
      attrs: {
        label: "创建时间",
        prop: "createTime",
        width: 160,
      },
    },
    {
      attrs: {
        label: "到款状态",
        prop: "refundStatusNew",
        width: 80,
      },
      render(refundStatusNew) {
        return proxy.dictValueLabel(refundStatusNew, refundStatus.value);
      },
    },

    {
      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: "操作",
        width: "120",
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "选择",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              handleSelect(row);
            },
          },
        ];
      },
    },
  ];
});
const getDict = () => {
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      dictCode: "contract_type",
      tenantId: useUserStore().user.tenantId,
    })
    .then((res) => {
      if (res.rows && res.rows.length > 0) {
        contractType.value = res.rows.map((item) => {
          return {
            label: item.dictValue,
            value: item.dictKey,
          };
        });
      }
    });
  proxy.post("/corporation/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    corporationList.value = res.rows.map((item) => {
      return {
        ...item,
        label: item.name,
        value: item.id,
      };
    });
  });
  proxy.post("/customer/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    customerList.value = res.rows.map((item) => {
      return {
        ...item,
        label: item.name,
        value: item.id,
      };
    });
  });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/contract/contractAndSamplePage", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getDict();
getList();
const handleSelect = (row) => {
  proxy.$emit("handleSelectContrct", row);
};
</script>

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