<template>
  <div>
    <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: () => clickAdd(),
          },
        ]"
        @get-list="getList"
        @clickReset="clickReset">
        <template #code="{ item }">
          <div>
            <a style="color: #409eff; cursor: pointer; word-break: break-all" @click="clickCode(item)">{{ item.code }}</a>
          </div>
        </template>
      </byTable>
    </el-card>
  </div>
</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,
    code: "",
    flowStatus: "",
    beginTime: "",
    endTime: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "code",
      label: "申购单号",
    },
    {
      type: "select",
      prop: "flowStatus",
      data: flowStatus([{ dictKey: 10, dictValue: "已采购" }]),
      label: "流程状态",
    },
    {
      type: "date",
      propList: ["beginTime", "endTime"],
      label: "申购日期",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "申购单号",
        slot: "code",
        width: 220,
      },
    },
    {
      attrs: {
        label: "申购时间",
        prop: "applyTime",
        align: "center",
        width: 160,
      },
    },
    {
      attrs: {
        label: "申购人",
        prop: "applyName",
        width: 160,
      },
    },
    {
      attrs: {
        label: "状态",
        prop: "flowStatus",
        width: 160,
      },
      render(val) {
        return proxy.dictKeyValue(val, flowStatus([{ dictKey: 10, dictValue: "已采购" }]));
      },
    },
    {
      attrs: {
        label: "申购说明",
        prop: "remark",
      },
    },
    {
      attrs: {
        label: "操作",
        width: 80,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          row.flowStatus == "0"
            ? {
                attrs: {
                  label: "编辑",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  clickUpdate(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("/applyBuy/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 clickAdd = () => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "apply_buy",
      flowName: "申购流程",
      random: proxy.random(),
    },
  });
};
const clickCode = (item) => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "apply_buy",
      flowName: "申购流程",
      processType: "20",
      id: item.id,
      flowId: item.flowId,
      random: proxy.random(),
    },
  });
};
const clickUpdate = (item) => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "apply_buy",
      flowName: "申购流程",
      processType: "40",
      id: item.id,
      random: proxy.random(),
    },
  });
};
</script>

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