import { defineStore } from 'pinia'
import {
  getCouponListApi,
  receiveCouponApi,
  receiveCouponApiNew,
  getCoupon2idApi,
  getCoupon2ListApi,
} from '@/service/coupon1'
import {
  getCoupon2ViewmListApi,
  getCoupon2DetailApi,
  getCoupon2ModifyApi,
  getCoupon2AuditListApi,
  getCoupon2AuditDetailApi,
  submitCoupon2AuditApi,
} from '@/service/coupon2'
import { until } from '@vueuse/core'

export const useCouponStore = defineStore(
  'coupon',
  () => {
    // 获取优惠券列表
    const getCouponList = async (couponproductids: string) => {
      const { data: couponList, loading } = useRequest(
        () => getCouponListApi({ couponproductids }, { pageindex: 1, rows: 999 }),
        {
          immediate: true,
        },
      )

      await until(loading).toBe(false)
      return couponList.value.Data
    }

    // 领取优惠券 旧 已弃用
    const receiveCoupon = async ({ couponid, coupon2phone, coupon2productids }) => {
      const { data: couponList, loading } = useRequest(
        () => receiveCouponApi({ couponid, coupon2phone, coupon2productids }),
        {
          immediate: true,
        },
      )

      await until(loading).toBe(false)
      return couponList.value
    }

    // 领取优惠券 new
    const receiveCouponNew = ({
      couponid,
      userstype,
      usersidcardnumber,
      usersshtyxydm,
      usersname,
      coupon2productids,
      coupon2phone,
      filesid,
      coupon2merchantid,
    }) => {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve, reject) => {
        const {
          data: couponList,
          loading,
          error,
        } = useRequest(
          () =>
            receiveCouponApiNew({
              couponid,
              userstype,
              usersidcardnumber,
              usersshtyxydm,
              usersname,
              coupon2productids,
              coupon2phone,
              filesid,
              coupon2merchantid,
            }),
          {
            immediate: true,
          },
        )

        await until(loading).toBe(false)

        if (error.value) {
          reject(new Error('领取优惠券失败'))
        }
        resolve(couponList.value)
      })
    }

    // 获取该用户下，已领取的优惠劵id，用于判断是否可以领取
    const getCoupon2id = async (params) => {
      const { data: couponList, loading } = useRequest(() => getCoupon2idApi(params), {
        immediate: true,
      })

      await until(loading).toBe(false)
      return couponList.value
    }

    // 通过优惠券code获取产品信息（视图）
    const getCoupon2ViewmList = async (params: { coupon2code: string }) => {
      const { data: couponList, loading } = useRequest(
        () => getCoupon2ViewmListApi(params, { pageindex: 1, rows: 999 }),
        {
          immediate: true,
        },
      )

      await until(loading).toBe(false)
      return couponList.value
    }

    // 获取已关联到当前用户的优惠券列表
    const getCoupon2List = async (params) => {
      const { data: couponList, loading } = useRequest(
        () => getCoupon2ListApi(params, { pageindex: 1, rows: 999 }),
        {
          immediate: true,
        },
      )

      await until(loading).toBe(false)
      return couponList.value
    }

    // 通过优惠券id获取产品信息（视图）
    const getCoupon2ViewDetailBySid = async (params: { coupon2sid: string }) => {
      const { data: couponList, loading } = useRequest(() => getCoupon2DetailApi(params), {
        immediate: true,
      })

      await until(loading).toBe(false)
      return couponList.value
    }

    // 优惠劵审核修改
    // coupon2sid: 优惠券id 必传
    const getCoupon2Modify = async (params) => {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve, reject) => {
        const {
          data: couponList,
          loading,
          error,
        } = useRequest(() => getCoupon2ModifyApi(params), {
          immediate: true,
        })

        await until(loading).toBe(false)

        if (error.value) {
          reject(new Error('优惠劵审核修改失败'))
        }
        resolve(couponList.value)
      })
    }

    // 优惠劵审核_列表
    const getCoupon2AuditList = async (params) => {
      const { data: couponList, loading } = useRequest(
        () => getCoupon2AuditListApi(params, { pageindex: 1, rows: 9999 }),
        {
          immediate: true,
        },
      )

      await until(loading).toBe(false)
      return couponList.value
    }

    // 优惠劵审核_详情
    const getCoupon2AuditDetail = async (params) => {
      const { data: couponList, loading } = useRequest(() => getCoupon2AuditDetailApi(params), {
        immediate: true,
      })

      await until(loading).toBe(false)
      return couponList.value
    }

    // 优惠劵审核_提交
    const submitCoupon2Audit = async (params) => {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve, reject) => {
        const {
          data: couponList,
          loading,
          error,
        } = useRequest(() => submitCoupon2AuditApi(params), {
          immediate: true,
        })

        await until(loading).toBe(false)

        if (error.value) {
          reject(new Error('优惠劵审核提交失败'))
        }
        resolve(couponList.value)
      })
    }

    return {
      getCouponList,
      receiveCoupon,
      receiveCouponNew,
      getCoupon2id,
      getCoupon2ViewmList,
      getCoupon2List,
      getCoupon2ViewDetailBySid,
      getCoupon2Modify,
      getCoupon2AuditList,
      getCoupon2AuditDetail,
      submitCoupon2Audit,
    }
  },
  {
    persist: true,
  },
)
