<template>
  <div>
    <el-card class="box-card">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :searchConfig="searchConfig"
        highlight-current-row
        @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>
        <template #exceptionTypeDetail="{ item }">
          <div>{{ item.exceptionTypeDetail }}</div>
        </template>
      </byTable>
    </el-card>
  </div>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import { ElMessage, ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();
const departmentList = ref([{ dictKey: "0", dictValue: "胜德体育" }]);
const exceptionTypeList = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    departmentId: "",
    code: "",
    wlnCode: "",
    exception: "1",
    exceptionType: "",
  },
});
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: "exceptionType",
      data: exceptionTypeList.value,
      label: "异常类型",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "事业部",
        prop: "departmentName",
        width: 140,
      },
    },
    {
      attrs: {
        label: "订单号",
        slot: "code",
        width: 220,
      },
    },
    {
      attrs: {
        label: "万里牛单号",
        prop: "wlnCode",
        width: 180,
      },
    },
    {
      attrs: {
        label: "订单状态",
        prop: "status",
        width: 140,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["order_status"]);
      },
    },
    {
      attrs: {
        label: "异常明细",
        slot: "exceptionTypeDetail",
        "min-width": 240,
      },
    },
    {
      attrs: {
        label: "异常时间",
        prop: "exceptionTime",
        width: 160,
      },
    },
    {
      attrs: {
        label: "操作",
        width: 140,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          ["1", "2", "3"].includes(row.exceptionType)
            ? {
                attrs: {
                  label: "转为正常",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  clickNormalize(row);
                },
              }
            : {},
          // row.status == 0 || row.status == 10 || row.status == 20
          {
            attrs: {
              label: "删除",
              type: "danger",
              text: true,
            },
            el: "button",
            click() {
              clickDelete(row);
            },
          },
        ];
      },
    },
  ];
});
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,
          };
        })
      );
    }
  });
  proxy.post("/orderInfo/getExceptionTypeList", {}).then((res) => {
    if (res && res.length > 0) {
      exceptionTypeList.value = res.map((item) => {
        return {
          dictKey: item.key,
          dictValue: item.value,
        };
      });
    }
  });
};
getDemandData();
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
      exception: "1",
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/orderInfo/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 clickCode = (row) => {
  proxy.$router.replace({
    path: "/order-detail",
    query: {
      detailId: row.id,
      text: "订单详情",
      random: proxy.random(),
    },
  });
};
const clickDelete = (row) => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.post("/orderInfo/deleteAndStore", { id: row.id }).then(() => {
        ElMessage({ message: "删除成功", type: "success" });
        getList();
      });
    })
    .catch(() => {});
};
const clickNormalize = (row) => {
  proxy.post("/orderInfo/turnToRegularOrder", { id: row.id }).then(() => {
    ElMessage({ message: "操作成功", type: "success" });
    getList();
  });
};
</script>

<style lang="scss" scoped>
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
:deep(.el-dialog) {
  margin-top: 10px !important;
  margin-bottom: 10px !important;
}
.select-card {
  height: calc(100vh - 184px);
  overflow-y: auto;
  overflow-x: hidden;
}
</style>
