<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: () => newPriceSheet(),
          },
        ]"
        @get-list="getList">
        <template #amount="{ item }">
          <div>
            <span style="padding-right: 4px">{{ item.currency }}</span>
            <span>{{ moneyFormat(item.amount, 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";

const { proxy } = getCurrentInstance();
const accountList = 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 sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    status: "",
    sellCorporationId: "",
  },
});
const loading = ref(false);
const selectConfig = computed(() => {
  return [
    {
      label: "审批状态",
      prop: "status",
      data: status.value,
    },
    {
      label: "归属公司",
      prop: "sellCorporationId",
      data: corporationList.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "报价单号",
        prop: "code",
        width: 200,
      },
    },
    {
      attrs: {
        label: "归属公司",
        prop: "sellCorporationId",
      },
      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: "userName",
        width: 120,
      },
    },
    {
      attrs: {
        label: "报价时间",
        prop: "createTime",
        width: 160,
      },
    },
    {
      attrs: {
        label: "客户名称",
        prop: "buyCorporationId",
      },
      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: "报价金额",
        slot: "amount",
        width: 140,
      },
    },
    {
      attrs: {
        label: "预付比例",
        slot: "advanceRatio",
        width: 120,
      },
    },
    {
      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",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "复制",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickCopy(row);
            },
          },
          {
            attrs: {
              label: "打印",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickPrint(row);
            },
          },
        ];
      },
    },
  ];
});
const getDict = () => {
  proxy.post("/saleQuotation/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    accountList.value = res.rows.map((item) => {
      return {
        label: item.alias,
        value: item.id,
      };
    });
  });
  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("/saleQuotation/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 newPriceSheet = () => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "sale_quotation_flow",
      flowName: "报价审批流程",
    },
  });
};
const clickCopy = (item) => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "sale_quotation_flow",
      flowName: "报价审批流程",
      id: item.id,
    },
  });
};
const clickPrint = (item) => {
  console.log(item);
};
</script>

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