<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :width="1000"
    :visible="visible"
    :maskClosable="false"
    :maxable="maxable"
    :footer="null"
    :title="isUpdate ? '取单' : '取单'"
    :body-style="{
      paddingTop: '10px',
      paddingBottom: '38px',
      backgroundColor: '#f3f3f3'
    }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-space direction="vertical" class="w-full">
      <div v-for="(item, index) in groups" :key="index">
        <a-card
          :title="`订单总价: ￥${formatNumber(item.totalPrice)}`"
          :body-style="{ padding: '10px' }"
        >
          <div class="flex flex-wrap">
            <div v-for="(v, i) in item.cashiers" :key="i" class="flex m-2">
              <div class="item flex flex-col bg-gray-50 p-3 w-[215px]">
                <text>{{ v.goodsName }}</text>
                <text class="text-gray-400">规格：{{ v.spec }}</text>
                <text class="text-gray-400">数量：{{ v.cartNum }}</text>
                <text>小计：{{ v.totalPrice }}</text>
              </div>
            </div>
          </div>
          <!--          <div class="comments p-2 text-gray-400">-->
          <!--            {{ item.comments }}-->
          <!--          </div>-->
          <template #actions>
            <div class="text-left px-3">
              <a-space>
                <a-button type="primary" @click="getCashier(item.groupId)"
                  >取单</a-button
                >
                <a-button danger type="primary" @click="remove(item.groupId)"
                  >删除</a-button
                >
                <!--                <a-button>备注</a-button>-->
              </a-space>
            </div>
          </template>
        </a-card>
      </div>
    </a-space>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import { message } from 'ant-design-vue';
  import { addMpMenu, updateMpMenu } from '@/api/cms/mp-menu';
  import { MpMenu } from '@/api/cms/mp-menu/model';
  import { FormInstance } from 'ant-design-vue/es/form';
  import { getByGroup, listByGroupId, removeByGroup } from '@/api/shop/cashier';
  import { formatNumber } from 'ele-admin-pro/es';
  import { CashierVo } from '@/api/shop/cashier/model';

  // 是否是修改
  const isUpdate = ref(false);

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 类型 0服务 1订单
    type?: number;
    // 页面ID
    pageId?: number;
    // 修改回显的数据
    data?: MpMenu | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  const pageId = ref(0);
  // 是否显示最大化切换按钮
  const maxable = ref(true);
  // 表格选中数据
  const formRef = ref<FormInstance | null>(null);
  //
  const groups = ref<CashierVo[]>();

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  const getCashier = (groupId: number) => {
    getByGroup(groupId).then(() => {
      updateVisible(false);
      emit('done');
    });
  };

  const remove = (groupId: number) => {
    removeByGroup(groupId).then(() => {
      reload();
    });
  };

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const formData = {
          pageId: pageId.value
        };
        const saveOrUpdate = isUpdate.value ? updateMpMenu : addMpMenu;
        saveOrUpdate(formData)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  const reload = () => {
    listByGroupId({}).then((data) => {
      groups.value = data;
    });
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        reload();
      }
    }
  );
</script>
