<template>
  <el-card class="box-card">
    <byTable
      :hideTable="true"
      :hidePagination="true"
      :source="sourceList.data"
      :pagination="sourceList.pagination"
      :config="config"
      :loading="loading"
      :searchConfig="searchConfig"
      highlight-current-row
      @get-list="getList"
      @clickReset="clickReset">
    </byTable>
    <div style="margin-left: 15px">系统缺少订单数组:</div>
    <Editor :value="tableList.systemMissingOrderList" ref="systemMissingOrderList" />
    <div style="margin-left: 15px; margin-top: 20px">系统多余订单数组:</div>
    <Editor :value="tableList.systemSurplusOrderList" ref="systemSurplusOrderList" />
  </el-card>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import { ElMessage } from "element-plus";
import Editor from "/src/components/Editor/index.vue";

const { proxy } = getCurrentInstance();
const departmentList = ref([{ dictKey: "0", dictValue: "胜德体育" }]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    departmentId: "",
    code: "",
    wlnCode: "",
    status: "",
    settlementStatus: "",
    exception: "0",
    compareStr: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "code",
      label: "订单号",
    },
    {
      type: "input",
      prop: "wlnCode",
      label: "万里牛单号",
    },
    {
      type: "select",
      prop: "departmentId",
      data: departmentList.value,
      label: "事业部",
    },
    {
      type: "select",
      prop: "status",
      dictKey: "order_status",
      label: "订单状态",
    },
    {
      type: "select",
      prop: "settlementStatus",
      label: "结算状态",
      data: proxy.useUserStore().allDict["settlement_status"],
    },
    {
      type: "textarea",
      prop: "compareStr",
      label: "对比字符串",
    },
  ];
});
const config = computed(() => {
  return [];
});
const getDemandData = () => {
  proxy.post("/department/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      departmentList.value = departmentList.value.concat(
        res.rows.map((item) => {
          return {
            dictKey: item.id,
            dictValue: item.name,
          };
        })
      );
    }
  });
};
getDemandData();
const tableList = ref({
  systemMissingOrderList: "",
  systemSurplusOrderList: "",
});
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
      exception: "0",
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  if (sourceList.value.pagination.compareStr) {
    loading.value = true;
    proxy.post("/orderInfo/compare", sourceList.value.pagination).then((res) => {
      if (res.systemMissingOrderList && res.systemMissingOrderList.length > 0) {
        tableList.value.systemMissingOrderList = Object.freeze(res.systemMissingOrderList.join("，"));
      } else {
        tableList.value.systemMissingOrderList = "";
      }
      console.log(tableList.value.systemMissingOrderList);
      proxy.$refs.systemMissingOrderList.changeHtml(tableList.value.systemMissingOrderList);
      if (res.systemSurplusOrderList && res.systemSurplusOrderList.length > 0) {
        tableList.value.systemSurplusOrderList = Object.freeze(res.systemSurplusOrderList.join("，"));
      } else {
        tableList.value.systemSurplusOrderList = "";
      }
      proxy.$refs.systemSurplusOrderList.changeHtml(tableList.value.systemSurplusOrderList);
      setTimeout(() => {
        loading.value = false;
      }, 200);
    });
  } else {
    return ElMessage("请输入对比字符串");
  }
};
getList();
const clickReset = () => {
  tableList.value = {
    systemMissingOrderList: "",
    systemSurplusOrderList: "",
  };
  proxy.$refs.systemMissingOrderList.changeHtml("");
  proxy.$refs.systemSurplusOrderList.changeHtml("");
  getList("", true);
};
</script>

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