<?php

namespace App\Admin\Actions\backstage\Coupon;

use App\Models\Coupon;
use App\Models\CouponType;
use App\Models\UserCoupon;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class AllCouponBatchGrant extends BatchAction
{
    public $name = '后台所有优惠券批量发放';

    public function handle(Collection $collection,Request $request)
    {
        //计算总共要发放多少个用户
        $user_arr = $collection->toArray();
        $user_num = count($user_arr);
        //每种优惠券要发放几张
        $coupon_num = $request->get('num');
        //单种优惠券一共要发放几张
        $all_num = $user_num*$coupon_num;
        //获取所有类型为后台发放的优惠券
        $allcoupon = Coupon::where('position_type',2)->get();
        $coupon_arr = $allcoupon->toArray();
        //判断优惠券数量是否充足
        foreach ($coupon_arr as $value)
        {
            if ($value['num'] < $all_num)
            {
                return $this->response()->error('优惠券'.$value['name'].'的数量不足')->refresh();
            }
        }

        for ($i = 0;$i<$coupon_num;$i++)
        {
            foreach ($coupon_arr as $coupon)
            {
                //拿到基础数据
                //获取过期类型是指定几天过期
                if ($coupon['expire_type'] == 1){
                    $effective_days = $coupon['effective_days'];
                    $str_time = '+'.$effective_days.'day';
                    $expire_time_num = strtotime($str_time);
                    $arr = [
                        'user_id' => 0,
                        'coupon_id'=>$coupon['id'],
                        'name' => $coupon['name'],
                        'title' => $coupon['title'],
                        'desc' => $coupon['desc'],
                        'rules' => $coupon['rules'],
                        'icon' => $coupon['icon'],
                        'type' => $coupon['type'],
                        'usable_type' => $coupon['usable_type'],
                        'money' => $coupon['money'],
                        'discount' => $coupon['discount'],
                        'min_consume_amount' => $coupon['min_consume_amount'],
                        'max_reduce_amount' => $coupon['max_reduce_amount'],
                        'expire_type' => $coupon['expire_type'],
                        'effective_days' => $coupon['effective_days'],
                        'expire_time' => $expire_time_num,
                    ];
                }else if ($coupon->expire_type == 2){
                    //设置有效期的基础数据
                    $arr = [
                        'user_id' => 0,
                        'coupon_id'=>$coupon['id'],
                        'name' => $coupon['name'],
                        'title' => $coupon['title'],
                        'desc' => $coupon['desc'],
                        'rules' => $coupon['rules'],
                        'icon' => $coupon['icon'],
                        'type' => $coupon['type'],
                        'usable_type' => $coupon['usable_type'],
                        'money' => $coupon['money'],
                        'discount' => $coupon['discount'],
                        'min_consume_amount' => $coupon['min_consume_amount'],
                        'max_reduce_amount' => $coupon['max_reduce_amount'],
                        'expire_type' => $coupon['expire_type'],
                        'start_time' => $coupon['start_time'],
                        'end_time' => $coupon['end_time'],
                    ];
                }

                //如果是部分产品可用，才有类型数组
                if ($coupon['usable_type'] == 2)
                    //获取类型数组
                {
                    $type = CouponType::where('coupon_id',$coupon['id'])->where('user_coupon_id',0)->pluck('product_type');
                }
                foreach ($collection as $model) {
                    //插入到用户优惠券中
                    $arr['user_id'] = $model->id;
                    //获取到插入之后的id
                    $insert_id = UserCoupon::create($arr)->id;
                    //如果是部分产品可用，则要插入可用类型到优惠券可用类型表
                    if ($coupon['usable_type'] == 2)
                    {
                        foreach ($type as $value)
                        {
                            $coupon_type_arr = [
                                'coupon_id' => 0,
                                'user_coupon_id' =>$insert_id,
                                'product_type' =>$value
                            ];
                            CouponType::create($coupon_type_arr);
                        }
                    }
                    $coupon = Coupon::where('id',$coupon['id'])->first();
                    Coupon::where('id',$coupon['id'])->update(['num'=>$coupon['num']-1]);
                }
            }
        }
        return $this->response()->success('优惠券发放成功')->refresh();

    }
    public function form()
    {
        $this->text('num', '优惠券数量')->help('点击提交后请耐心等待，请勿再次点击提交按钮，以免造成多次发放');
    }

}