<template>
  <div class="page">
    <div class="ele-body">
      <a-card :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="orderGoodsId"
          :columns="columns"
          :datasource="datasource"
          v-model:selection="selection"
          :customRow="customRow"
          tool-class="ele-toolbar-form"
          :scroll="{ x: 1200 }"
          class="sys-org-table"
          :striped="true"
        >
          <template #toolbar>
            <search
              @search="reload"
              :selection="selection"
              @add="openEdit"
              @remove="removeBatch"
            />
          </template>
          <template #bodyCell="{ column, record }">
            <!--            <template v-if="column.key === 'imageUrl'">-->
            <!--              <img :src="record.imageUrl" width="100" height="70" />-->
            <!--            </template>-->
            <template v-if="column.key === 'categoryId'">
              {{ record.categoryId === 25 ? '早餐' : '' }}
              {{ record.categoryId === 26 ? '午餐' : '' }}
              {{ record.categoryId === 27 ? '晚餐' : '' }}
            </template>
            <template v-if="column.key === 'nickname'">
              {{ record.nickname }}
            </template>
            <template v-if="column.key === 'deliveryStatus'">
              <a-tag
                v-if="record.deliveryStatus === 10"
                color="orange"
                @click="onDeliveryStatus(record, 20)"
                >未签到</a-tag
              >
              <a-tag
                v-if="record.deliveryStatus === 20"
                color="green"
                @click="onDeliveryStatus(record, 10)"
                >已签到</a-tag
              >
            </template>
            <template v-if="column.key === 'createTime'">
              {{ record.createTime }}
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a-button @click="openInfo(record)">详情</a-button>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 订单详情 -->
    <order-info v-model:visible="showInfo" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { toDateString } from 'ele-admin-pro';
  import Search from './components/search.vue';
  import OrderInfo from './components/order-info.vue';
  import {
    pageOrderGoods,
    removeBatchOrderGoods,
    updateOrderGoods
  } from '@/api/order/goods';
  import type { OrderGoods, OrderGoodsParam } from '@/api/order/goods/model';
  import useFormData from '@/utils/use-form-data';

  defineProps<{
    activeKey?: boolean;
    data?: any;
  }>();

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    // {
    //   key: 'index',
    //   width: 48,
    //   align: 'center',
    //   fixed: 'left',
    //   hideInSetting: true,
    //   hideInTable: true,
    //   customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    // },
    // {
    //   title: '操作',
    //   key: 'action',
    //   width: 120,
    //   align: 'center',
    //   fixed: 'left',
    //   hideInSetting: true
    // },
    // {
    //   title: '菜品封面图',
    //   dataIndex: 'imageUrl',
    //   key: 'imageUrl',
    //   align: 'center'
    // },
    {
      title: '订单号',
      dataIndex: 'orderId',
      key: 'orderId',
      align: 'center'
    },
    {
      title: '预定日期',
      dataIndex: 'deliveryTime',
      key: 'deliveryTime',
      align: 'center',
      customRender: ({ text }) => toDateString(text, 'MM-dd')
    },
    {
      title: '姓名',
      dataIndex: 'nickname',
      align: 'center'
    },
    {
      title: '菜品名称',
      dataIndex: 'goodsName',
      key: 'goodsName',
      align: 'center'
    },
    {
      title: '餐段',
      dataIndex: 'categoryId',
      key: 'categoryId',
      align: 'center'
    },
    {
      title: '数量',
      dataIndex: 'totalNum',
      align: 'center',
      sorter: true
    },
    // {
    //   title: '付款状态',
    //   dataIndex: 'payStatus',
    //   align: 'center',
    //   sorter: true,
    //   customRender: ({ text }) => {
    //     if (text == 10) {
    //       return '未付款';
    //     }
    //     if (text == 20) {
    //       return '已支付';
    //     }
    //   }
    // },
    // {
    //   title: '订单状态',
    //   dataIndex: 'orderStatus',
    //   align: 'center',
    //   sorter: true,
    //   customRender: ({ text }) => {
    //     if (text == 10) {
    //       return '进行中';
    //     }
    //     if (text == 20) {
    //       return '已取消';
    //     }
    //     if (text == 30) {
    //       return '已完成';
    //     }
    //   }
    // },
    {
      title: '签到状态',
      dataIndex: 'deliveryStatus',
      key: 'deliveryStatus',
      align: 'center'
    },
    {
      title: '报餐时间',
      dataIndex: 'createTime',
      key: 'createTime',
      align: 'center',
      sorter: true,
      customRender: ({ text }) => toDateString(text)
    }
  ]);

  // 表格选中数据
  const selection = ref<OrderGoods[]>([]);
  // 当前编辑数据
  const current = ref<OrderGoods | null>(null);
  // 是否显示资产详情
  const showInfo = ref(false);
  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    // 搜索条件
    if (filters.payStatus) {
      where.payStatus = filters.payStatus;
    }
    if (filters.deliveryStatus) {
      where.deliveryStatus = filters.deliveryStatus;
    }
    where.payStatus = 20;
    where.hasNum = false;
    where.orderStatus = 10;
    where.tenantId = localStorage.getItem('tenantId');
    return pageOrderGoods({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  // 表单数据
  const { form } = useFormData<OrderGoods>({
    orderGoodsId: undefined,
    deliveryStatus: undefined
  });

  /* 搜索 */
  const reload = (where?: OrderGoodsParam) => {
    selection.value = [];
    tableRef?.value?.reload({ where: where });
  };

  /* 打开编辑弹窗 */
  const openInfo = (row?: OrderGoods) => {
    current.value = row ?? null;
    showInfo.value = true;
  };

  const onDeliveryStatus = (row?: OrderGoods, deliveryStatus?: number) => {
    Modal.confirm({
      title: '提示',
      content: '确定要修改签到状态吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        form.orderGoodsId = row?.orderGoodsId;
        form.deliveryStatus = deliveryStatus;
        updateOrderGoods(form).then((msg) => {
          message.success(msg);
          reload();
        });
      }
    });
  };

  /* 批量删除 */
  const removeBatch = () => {
    console.log(selection.value);
    if (!selection.value.length) {
      message.error('请至少选择一条数据');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的记录吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = message.loading('请求中..', 0);
        removeBatchOrderGoods(selection.value.map((d) => d.orderGoodsId))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };

  /* 自定义行属性 */
  const customRow = (record: OrderGoods) => {
    return {
      // 行点击事件
      onClick: () => {
        // console.log(record);
      },
      // 行双击事件
      onDblclick: () => {
        // openEdit(record);
      }
    };
  };

  reload();
</script>

<script lang="ts">
  export default {
    name: 'OrderGoodsIndex'
  };
</script>

<style lang="less" scoped>
  p {
    line-height: 0.8;
  }
  .sys-org-table :deep(.ant-table-body) {
    overflow: auto !important;
    overflow: overlay !important;
  }

  .sys-org-table :deep(.ant-table-pagination.ant-pagination) {
    padding: 0 4px;
    margin-bottom: 0;
  }
  .price-edit {
    padding-right: 5px;
  }
  .comments {
    max-width: 200px;
  }
</style>
