<?php

namespace App\Community\Actions\Vaccine;

use App\Models\Docter;
use App\Models\OrderVaccine;
use App\Models\Organization;
use App\Models\OrganizationVaccines;
use App\Models\Payment;
use App\Models\Vaccine;
use Carbon\Carbon;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AddVaccine extends RowAction
{
    public $name = '订单修订';

    public function handle()
    {
        $ids = request('vaccine_id');

        $order_id = $this->row->id;

        $old_ids = OrderVaccine::where(['order_id'=>$order_id])->pluck('vaccine_id')->toArray();
        DB::beginTransaction();
        try {
            foreach ($ids as $id){
                $id = intval($id);
                $vaccine = Vaccine::where(['id'=>$id])->first();
//                $org_vaccine = vaccine::where('vaccine_id',$id)->first();
                if(empty($vaccine) || ($vaccine->stock <0)){
                    return $this->response()->error($vaccine->name.'库存不足或无效疫苗');
                }

                if(empty($id)) continue;
                $vaccine_data = [
                    'order_id'=>$order_id,
                    'vaccine_type'=>$vaccine->type,
                    'vaccine_price'=>$vaccine->price,
                    'vaccine_name'=>$vaccine->name,
                    'vaccine_remark'=>$vaccine->remark,
                    'vaccine_supplier'=>$vaccine->supplier,
                    'vaccine_id'=>$id
                ];


                $is_have = OrderVaccine::where(['order_id'=>$order_id,'vaccine_id'=>$id])->first();
                if($is_have) continue;
                $vaccine_data ['created_at'] = Carbon::now();
                $vaccine_data ['updated_at'] = Carbon::now();
                OrderVaccine::insert($vaccine_data);
                Vaccine::decrement('stock',1);
            }

            //删除并添加库存
            foreach ($old_ids as $id){
                $id = intval($id);
                if(!in_array($id,$ids)){
                    OrderVaccine::where(['order_id'=>$order_id,'vaccine_id'=>$id])->delete();
                    Vaccine::increment('stock',1);
                }
            }
            DB::commit();
        } catch (\Exception $e){
            dd($e->getMessage());
            DB::rollBack();
        }


        return $this->response()->success('修改疫苗成功')->refresh();
    }

    public function form()
    {
        $admin = Admin::user();
        $is_admin = Admin::user()->isAdministrator();
        $org_id =$admin->org_id;
        $order_id = $this->row->id;

        if($is_admin){
            $list = Vaccine::pluck('name','id');
        } else {
            $list = Vaccine::where('org_id',$org_id)->pluck('name','id');
        }
        $ids = OrderVaccine::where(['order_id'=>$order_id])->pluck('vaccine_id')->toArray();
        $this->multipleSelect('vaccine_id','选择疫苗')->options($list)->value($ids)->required();
    }

}
