<template>
  <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 #address="{ item }">
        <div>{{ item.province }}, {{ item.city }}, {{ item.county }}, {{ item.detailedAddress }}</div>
      </template>
    </byTable>

    <el-dialog title="售后类型" v-if="openAfterSale" v-model="openAfterSale" width="300px" style="margin-top: 20vh !important">
      <template #footer>
        <el-button type="primary" @click="applyForAfterSale(1)" size="large" v-preReClick>退 货</el-button>
        <el-button @click="applyForAfterSale(2)" size="large" v-preReClick>换 货</el-button>
      </template>
    </el-dialog>

    <el-dialog title="送货单" v-if="openDeliveryNote" v-model="openDeliveryNote" width="1000px">
      <DeliveryNote :rowData="rowData" @clickCancel="clickCancel"></DeliveryNote>
    </el-dialog>
  </el-card>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import DeliveryNote from "/src/components/order/deliveryNote/index";

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",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "code",
      label: "订单号",
    },
    {
      type: "input",
      prop: "wlnCode",
      label: "E10单号",
    },
    {
      type: "select",
      prop: "departmentId",
      data: departmentList.value,
      label: "事业部",
    },
    {
      type: "select",
      prop: "status",
      dictKey: "order_status",
      label: "订单状态",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "事业部",
        prop: "departmentName",
        width: 120,
      },
    },
    {
      attrs: {
        label: "订单号",
        slot: "code",
        "min-width": 200,
      },
    },
    {
      attrs: {
        label: "E10单号",
        prop: "wlnCode",
        "min-width": 180,
      },
    },
    {
      attrs: {
        label: "快递单号",
        prop: "expressDeliveryCode",
        "min-width": 160,
      },
    },
    {
      attrs: {
        label: "订单状态",
        prop: "status",
        width: 120,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["order_status"]);
      },
    },
    {
      attrs: {
        label: "交期",
        prop: "deliveryTime",
        width: 160,
        align: "center",
      },
    },
    {
      attrs: {
        label: "发货时间",
        prop: "shippingTime",
        width: 160,
        align: "center",
      },
    },
    {
      attrs: {
        label: "收货人",
        prop: "consignee",
        width: 140,
      },
    },
    {
      attrs: {
        label: "收货人电话",
        prop: "consigneeNumber",
        width: 140,
      },
    },
    {
      attrs: {
        label: "收货人地址",
        slot: "address",
        "min-width": 220,
      },
    },
    {
      attrs: {
        label: "操作",
        width: 180,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          [40, 50].includes(row.status)
            ? {
                attrs: {
                  label: "售后",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  clickAfterSale(row);
                },
              }
            : {},
          row.type === 1 && row.status == 40
            ? {
                attrs: {
                  label: "送货单",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  clickDeliveryNote(row);
                },
              }
            : {},
          row.status == 20
            ? {
                attrs: {
                  label: "挂起",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  clickHangUp(row);
                },
              }
            : {},
          row.status == 60
            ? {
                attrs: {
                  label: "取消挂起",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  clickCancelHangUp(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,
          };
        })
      );
    }
  });
};
getDemandData();
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 };
  }
  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: "/addOrder",
    query: {
      detailId: row.id,
      text: "订单详情",
      random: proxy.random(),
      orderInquiry: true,
    },
  });
};
const openAfterSale = ref(false);
const rowData = ref({});
const clickAfterSale = (row) => {
  rowData.value = row;
  openAfterSale.value = true;
};
const applyForAfterSale = (type) => {
  openAfterSale.value = false;
  proxy.$router.replace({
    path: "/order/after-sale/initiate",
    query: {
      orderInfoId: rowData.value.id,
      type: type,
      random: proxy.random(),
    },
  });
};
const openDeliveryNote = ref(false);
const clickDeliveryNote = (row) => {
  rowData.value = row;
  openDeliveryNote.value = true;
};
const clickCancel = (status) => {
  openDeliveryNote.value = false;
  if (status) {
    getList();
  }
};
const clickHangUp = (row) => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.post("/orderInfo/suspendOrder", { id: row.id }).then(() => {
        ElMessage({ message: "操作成功", type: "success" });
        getList();
      });
    })
    .catch(() => {});
};
const clickCancelHangUp = (row) => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.post("/orderInfo/cancelSuspendOrder", { id: row.id }).then(() => {
        ElMessage({ message: "操作成功", type: "success" });
        getList();
      });
    })
    .catch(() => {});
};
</script>

<style lang="scss" scoped>
:deep(.el-dialog) {
  margin-top: 10px !important;
  margin-bottom: 10px !important;
}
</style>
