<template>
  <el-card class="box-card">
    <byTable
      :source="sourceList.data"
      :pagination="sourceList.pagination"
      :config="config"
      :loading="loading"
      :searchConfig="searchConfig"
      highlight-current-row
      :action-list="[
        {
          text: '发起采购退货',
          action: () => clickInitiateReturn(),
        },
      ]"
      @get-list="getList"
      @clickReset="clickReset">
    </byTable>
  </el-card>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import { flowStatus } from "/src/utils/flowStatus";

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "select",
      prop: "flowStatus",
      label: "流程状态",
      data: flowStatus(),
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "采购退货单号",
        prop: "code",
        align: "center",
      },
    },
    {
      attrs: {
        label: "关联合同数量",
        prop: "purchaseCount",
        align: "center",
      },
    },
    {
      attrs: {
        label: "发起时间",
        prop: "createTime",
        align: "center",
      },
    },
    {
      attrs: {
        label: "退货时间",
        prop: "returnTime",
        align: "center",
      },
    },
    {
      attrs: {
        label: "发起人",
        prop: "createName",
        align: "center",
      },
    },
    {
      attrs: {
        label: "流程状态",
        prop: "flowStatus",
        width: 120,
      },
      render(val) {
        return proxy.dictKeyValue(val, flowStatus());
      },
    },
    {
      attrs: {
        label: "操作",
        width: 260,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          row.flowStatus == 0
            ? {
                attrs: {
                  label: "变更退货单",
                  type: "danger",
                  text: true,
                },
                el: "button",
                click() {
                  clickChange(row);
                },
              }
            : {},
          {
            attrs: {
              label: "打印退货单",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              console.log(row);
            },
          },
          {
            attrs: {
              label: "查看详情",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickDetails(row);
            },
          },
        ];
      },
    },
  ];
});
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/purchaseReturn/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
const clickInitiateReturn = () => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "purchase_return",
      flowName: "采购退货流程",
      random: proxy.random(),
    },
  });
};
const clickDetails = (item) => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "purchase_return",
      flowName: "采购退货流程",
      processType: "20",
      id: item.id,
      flowId: item.flowId,
      random: proxy.random(),
    },
  });
};
const clickChange = (item) => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "purchase_return",
      flowName: "采购退货流程",
      processType: "30",
      businessId: item.id,
      flowId: item.flowId,
      random: proxy.random(),
    },
  });
};
</script>

<style lang="scss" scoped></style>
