<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '编辑站点统计信息表' : '添加站点统计信息表'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="styleResponsive ? { md: 4, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="
        styleResponsive ? { md: 19, sm: 19, xs: 24 } : { flex: '1' }
      "
    >
      <a-form-item label="站点ID" name="websiteId">
        <a-input
          allow-clear
          placeholder="请输入站点ID"
          v-model:value="form.websiteId"
        />
      </a-form-item>
      <a-form-item label="用户总数" name="userCount">
        <a-input
          allow-clear
          placeholder="请输入用户总数"
          v-model:value="form.userCount"
        />
      </a-form-item>
      <a-form-item label="订单总数" name="orderCount">
        <a-input
          allow-clear
          placeholder="请输入订单总数"
          v-model:value="form.orderCount"
        />
      </a-form-item>
      <a-form-item label="商品总数" name="productCount">
        <a-input
          allow-clear
          placeholder="请输入商品总数"
          v-model:value="form.productCount"
        />
      </a-form-item>
      <a-form-item label="总销售额" name="totalSales">
        <a-input
          allow-clear
          placeholder="请输入总销售额"
          v-model:value="form.totalSales"
        />
      </a-form-item>
      <a-form-item label="本月销售额" name="monthSales">
        <a-input
          allow-clear
          placeholder="请输入本月销售额"
          v-model:value="form.monthSales"
        />
      </a-form-item>
      <a-form-item label="今日销售额" name="todaySales">
        <a-input
          allow-clear
          placeholder="请输入今日销售额"
          v-model:value="form.todaySales"
        />
      </a-form-item>
      <a-form-item label="昨日销售额" name="yesterdaySales">
        <a-input
          allow-clear
          placeholder="请输入昨日销售额"
          v-model:value="form.yesterdaySales"
        />
      </a-form-item>
      <a-form-item label="本周销售额" name="weekSales">
        <a-input
          allow-clear
          placeholder="请输入本周销售额"
          v-model:value="form.weekSales"
        />
      </a-form-item>
      <a-form-item label="本年销售额" name="yearSales">
        <a-input
          allow-clear
          placeholder="请输入本年销售额"
          v-model:value="form.yearSales"
        />
      </a-form-item>
      <a-form-item label="今日订单数" name="todayOrders">
        <a-input
          allow-clear
          placeholder="请输入今日订单数"
          v-model:value="form.todayOrders"
        />
      </a-form-item>
      <a-form-item label="本月订单数" name="monthOrders">
        <a-input
          allow-clear
          placeholder="请输入本月订单数"
          v-model:value="form.monthOrders"
        />
      </a-form-item>
      <a-form-item label="今日新增用户" name="todayUsers">
        <a-input
          allow-clear
          placeholder="请输入今日新增用户"
          v-model:value="form.todayUsers"
        />
      </a-form-item>
      <a-form-item label="本月新增用户" name="monthUsers">
        <a-input
          allow-clear
          placeholder="请输入本月新增用户"
          v-model:value="form.monthUsers"
        />
      </a-form-item>
      <a-form-item label="今日访问量" name="todayVisits">
        <a-input
          allow-clear
          placeholder="请输入今日访问量"
          v-model:value="form.todayVisits"
        />
      </a-form-item>
      <a-form-item label="总访问量" name="totalVisits">
        <a-input
          allow-clear
          placeholder="请输入总访问量"
          v-model:value="form.totalVisits"
        />
      </a-form-item>
      <a-form-item label="商户总数" name="merchantCount">
        <a-input
          allow-clear
          placeholder="请输入商户总数"
          v-model:value="form.merchantCount"
        />
      </a-form-item>
      <a-form-item label="活跃用户数" name="activeUsers">
        <a-input
          allow-clear
          placeholder="请输入活跃用户数"
          v-model:value="form.activeUsers"
        />
      </a-form-item>
      <a-form-item label="转化率(%)" name="conversionRate">
        <a-input
          allow-clear
          placeholder="请输入转化率(%)"
          v-model:value="form.conversionRate"
        />
      </a-form-item>
      <a-form-item label="平均订单金额" name="avgOrderAmount">
        <a-input
          allow-clear
          placeholder="请输入平均订单金额"
          v-model:value="form.avgOrderAmount"
        />
      </a-form-item>
      <a-form-item label="统计日期" name="statisticsDate">
        <a-input
          allow-clear
          placeholder="请输入统计日期"
          v-model:value="form.statisticsDate"
        />
      </a-form-item>
      <a-form-item label="统计类型: 1日统计, 2月统计, 3年统计" name="statisticsType">
        <a-input
          allow-clear
          placeholder="请输入统计类型: 1日统计, 2月统计, 3年统计"
          v-model:value="form.statisticsType"
        />
      </a-form-item>
      <a-form-item label="排序号" name="sortNumber">
        <a-input-number
          :min="0"
          :max="9999"
          class="ele-fluid"
          placeholder="请输入排序号"
          v-model:value="form.sortNumber"
        />
      </a-form-item>
      <a-form-item label="操作用户ID" name="userId">
        <a-input
          allow-clear
          placeholder="请输入操作用户ID"
          v-model:value="form.userId"
        />
      </a-form-item>
      <a-form-item label="商户ID" name="merchantId">
        <a-input
          allow-clear
          placeholder="请输入商户ID"
          v-model:value="form.merchantId"
        />
      </a-form-item>
      <a-form-item label="状态: 0禁用, 1启用" name="status">
        <a-radio-group v-model:value="form.status">
          <a-radio :value="0">显示</a-radio>
          <a-radio :value="1">隐藏</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="是否删除: 0否, 1是" name="deleted">
        <a-input
          allow-clear
          placeholder="请输入是否删除: 0否, 1是"
          v-model:value="form.deleted"
        />
      </a-form-item>
      <a-form-item label="修改时间" name="updateTime">
        <a-input
          allow-clear
          placeholder="请输入修改时间"
          v-model:value="form.updateTime"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject, uuid } from 'ele-admin-pro';
  import { addCmsStatistics, updateCmsStatistics } from '@/api/cms/cmsStatistics';
  import { CmsStatistics } from '@/api/cms/cmsStatistics/model';
  import { useThemeStore } from '@/store/modules/theme';
  import { storeToRefs } from 'pinia';
  import { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
  import { FormInstance } from 'ant-design-vue/es/form';
  import { FileRecord } from '@/api/system/file/model';

  // 是否是修改
  const isUpdate = ref(false);
  const useForm = Form.useForm;
  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: CmsStatistics | null;
  }>();

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

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

  // 用户信息
  const form = reactive<CmsStatistics>({
    id: undefined,
    websiteId: undefined,
    userCount: undefined,
    orderCount: undefined,
    productCount: undefined,
    totalSales: undefined,
    monthSales: undefined,
    todaySales: undefined,
    yesterdaySales: undefined,
    weekSales: undefined,
    yearSales: undefined,
    todayOrders: undefined,
    monthOrders: undefined,
    todayUsers: undefined,
    monthUsers: undefined,
    todayVisits: undefined,
    totalVisits: undefined,
    merchantCount: undefined,
    activeUsers: undefined,
    conversionRate: undefined,
    avgOrderAmount: undefined,
    statisticsDate: undefined,
    statisticsType: undefined,
    sortNumber: undefined,
    userId: undefined,
    merchantId: undefined,
    status: undefined,
    deleted: undefined,
    tenantId: undefined,
    createTime: undefined,
    updateTime: undefined,
    cmsStatisticsId: undefined,
    cmsStatisticsName: '',
    status: 0,
    comments: '',
    sortNumber: 100
  });

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

  // 表单验证规则
  const rules = reactive({
    cmsStatisticsName: [
      {
        required: true,
        type: 'string',
        message: '请填写站点统计信息表名称',
        trigger: 'blur'
      }
    ]
  });

  const chooseImage = (data: FileRecord) => {
    images.value.push({
      uid: data.id,
      url: data.path,
      status: 'done'
    });
    form.image = data.path;
  };

  const onDeleteItem = (index: number) => {
    images.value.splice(index, 1);
    form.image = '';
  };

  const { resetFields } = useForm(form, rules);

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

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        images.value = [];
        if (props.data) {
          assignObject(form, props.data);
          if(props.data.image){
            images.value.push({
              uid: uuid(),
              url: props.data.image,
              status: 'done'
            })
          }
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    },
    { immediate: true }
  );
</script>
