<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: () => clickAddOrder(),
          },
          {
            text: '操作日志',
            action: () => viewLogs(),
          },
        ]"
        @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 #totalAmount="{ item }">
          <div style="color: #409eff">{{ moneyFormat(item.totalAmount) }}</div>
        </template>
        <template #address="{ item }">
          <div>{{ item.province }}, {{ item.city }}, {{ item.county }}, {{ item.detailedAddress }}</div>
        </template>
      </byTable>
    </el-card>

    <el-dialog title="操作日志" v-if="openLogs" v-model="openLogs" width="50%">
      <byTable
        :source="logsList.data"
        :pagination="logsList.pagination"
        :config="configLogs"
        :loading="loadingLogs"
        highlight-current-row
        @get-list="getLogsList">
      </byTable>
      <template #footer>
        <el-button @click="openLogs = false" size="large">关 闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

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

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    departmentName: "",
    code: "",
    wlnCode: "",
    status: "",
    settlementStatus: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "code",
      label: "订单号",
    },
    {
      type: "input",
      prop: "wlnCode",
      label: "万里牛单号",
    },
    {
      type: "input",
      prop: "departmentName",
      label: "事业部名称",
    },
    {
      type: "select",
      prop: "status",
      dictKey: "order_status",
      label: "订单状态",
    },
    {
      type: "select",
      prop: "settlementStatus",
      label: "结算状态",
      data: proxy.useUserStore().allDict["settlement_status"],
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "事业部",
        prop: "departmentName",
        width: 120,
      },
    },
    {
      attrs: {
        label: "订单号",
        slot: "code",
        width: 200,
      },
    },
    {
      attrs: {
        label: "万里牛单号",
        prop: "wlnCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "快递单号",
        prop: "expressDeliveryCode",
        width: 140,
      },
    },
    {
      attrs: {
        label: "订单状态",
        prop: "status",
        width: 120,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["order_status"]);
      },
    },
    {
      attrs: {
        label: "结算状态",
        prop: "settlementStatus",
        width: 120,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["settlement_status"]);
      },
    },
    {
      attrs: {
        label: "税率",
        prop: "taxRate",
        width: 80,
      },
      render(val) {
        return val + "%";
      },
    },
    {
      attrs: {
        label: "订单总金额 ¥",
        slot: "totalAmount",
        width: 120,
        align: "right",
      },
    },
    {
      attrs: {
        label: "产品总金额 ¥",
        prop: "productTotalAmount",
        width: 120,
        align: "right",
      },
      render(val) {
        return proxy.moneyFormat(val);
      },
    },
    {
      attrs: {
        label: "定制加工费 ¥",
        prop: "customProcessingFee",
        width: 120,
        align: "right",
      },
      render(val) {
        return proxy.moneyFormat(val);
      },
    },
    {
      attrs: {
        label: "代发费 ¥",
        prop: "lssueFee",
        width: 120,
        align: "right",
      },
      render(val) {
        return proxy.moneyFormat(val);
      },
    },
    {
      attrs: {
        label: "快递包材费 ¥",
        prop: "deliveryMaterialsFee",
        width: 120,
        align: "right",
      },
      render(val) {
        return proxy.moneyFormat(val);
      },
    },
    {
      attrs: {
        label: "包装人工费 ¥",
        prop: "packingLabor",
        width: 120,
        align: "right",
      },
      render(val) {
        return proxy.moneyFormat(val);
      },
    },
    {
      attrs: {
        label: "包材费 ¥",
        prop: "packagingMaterialCost",
        width: 120,
        align: "right",
      },
      render(val) {
        return proxy.moneyFormat(val);
      },
    },
    {
      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",
        width: 220,
      },
    },
    {
      attrs: {
        label: "操作",
        width: 180,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          row.status == 0
            ? {
                attrs: {
                  label: "编辑",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  clickUpdate(row);
                },
              }
            : {},
          {
            attrs: {
              label: "税率",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              ElMessage("暂无税率功能");
            },
          },
          {
            attrs: {
              label: "删除",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickDelete(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("/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 clickAddOrder = () => {
  proxy.$router.replace({
    path: "/addOrder",
    query: {
      random: proxy.random(),
    },
  });
};
const clickCode = (row) => {
  proxy.$router.replace({
    path: "/addOrder",
    query: {
      detailId: row.id,
      text: "订单详情",
      random: proxy.random(),
    },
  });
};
const clickDelete = (row) => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.post("/orderInfo/delete", { id: row.id }).then(() => {
        ElMessage({ message: "删除成功", type: "success" });
        getList();
      });
    })
    .catch(() => {});
};
const openLogs = ref(false);
const loadingLogs = ref(false);
const logsList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
  },
});
const type = ref([
  { dictKey: "10", dictValue: "新增" },
  { dictKey: "20", dictValue: "修改" },
  { dictKey: "30", dictValue: "删除" },
]);
const configLogs = computed(() => {
  return [
    {
      attrs: {
        label: "操作时间",
        prop: "createTime",
        width: 160,
        align: "center",
      },
    },
    {
      attrs: {
        label: "操作人",
        prop: "userName",
        align: "center",
      },
    },
    {
      attrs: {
        label: "订单号",
        prop: "orderCode",
        align: "center",
      },
    },
    {
      attrs: {
        label: "行为",
        prop: "type",
        width: 100,
        align: "center",
      },
      render(val) {
        return proxy.dictKeyValue(val, type.value);
      },
    },
  ];
});
const viewLogs = () => {
  logsList.value.data = [];
  logsList.value.pagination.total = 0;
  openLogs.value = true;
  getLogsList({ pageNum: 1, pageSize: 10 });
};
const getLogsList = async (req) => {
  logsList.value.pagination = { ...logsList.value.pagination, ...req };
  loadingLogs.value = true;
  proxy.post("/orderOperatingLog/page", logsList.value.pagination).then((res) => {
    logsList.value.data = res.rows;
    logsList.value.pagination.total = res.total;
    setTimeout(() => {
      loadingLogs.value = false;
    }, 200);
  });
};
const clickUpdate = (row) => {
  proxy.$router.replace({
    path: "/addOrder",
    query: {
      id: row.id,
      text: "编辑订单",
      random: proxy.random(),
    },
  });
};
</script>

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