<?php


namespace Mehrab\Front\Http\Controllers;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Mehrab\Common\Responses\AjaxResponses;
use Mehrab\Media\Services\MediaFileService;
use Mehrab\OrderService\Http\Requests\OrderService_address_store_request;
use Mehrab\OrderService\Http\Requests\OrderService_store_request;
use Mehrab\OrderService\Models\OrderService;
use Mehrab\OrderService\Repositories\OrderServiceRepo;
use Mehrab\Service\Repositories\ServiceRepo;
use Mehrab\User\Models\User;
use Mehrab\User\Repositories\UserRepo;


class OrderController extends Controller
{

    public function index($slug){
        $footer = true;
        $leaflet = true;
        $service = (new ServiceRepo())->findBySlug_with($slug,['regions']);
        $questions=$service->questions()->where("parent_id",null)->where("parent_question",0)->orderByDesc("priority")->get();
       // dd($questions[0]->children[1]->children_question);
        $cities = [];
        foreach ($service->regions as $region){
            $cities[$region->city->name][$region->id] =$region->name;
        }
        $cities = collect($cities);
        return view('Front::order',compact('questions','footer','service','cities','leaflet'));
    }

    public function address_store(OrderService_address_store_request $request)
    {
        $this->authorize('set_address',User::class);
        list($address,$address_id) = (new OrderServiceRepo())->address_store_frontend($request);
        return $address ?
            response()->json(['status'=>'success','address' => $address,'address_id' => $address_id],200) :
            response()->json(['status' => 'failed','message'=>'خطا در ثبت آدرس شما'],400);
    }

    public function store(OrderService_store_request $request, UserRepo $userRepo, ServiceRepo $serviceRepo,$serviceParentSlug ,$serviceSlug)
    {
        //$this->authorize('set_order',OrderService::class);
        $service = $serviceRepo->findBySlug_with_questions($serviceSlug);

        if ($request->media) {
            $idMedia = (MediaFileService::privateUpload($request->media)->id);
        }

        $operator_users = $userRepo->random_user_permission_operator_order();
        $user_address = $userRepo->get_user_address($request->address_id);

        $answers = array();
        $price = 0;

        //validate checkbox
        $questionsCheckbox = $service->questions->where('type', 'checkbox')->toArray();
        list($answers, $price) = $serviceRepo->validate_checkbox($request, $questionsCheckbox, $answers, $price);
//        if (!$answers || !$price) {
//            return 'چک باکس ها کامل شوند';
//        }
        //valid radio
        $questionsRadio = $service->questions->where('type', 'radio')->toArray();
        list($answers, $price) = $serviceRepo->validate_radio($request, $questionsRadio, $answers, $price);
//        if (!$answers || !$price) {
//            return 'رادیو باتن ها کامل شوند';
//        }
        $order = (new OrderServiceRepo())->store_order_frontend($request, $user_address, $operator_users, $service, $answers, $idMedia ?? null,$price);
        if ($order) return redirect()->route('panel.orders')->with('status','سفارش شما با موفقیت ثبت گردید .');

        return redirect()->back()->with('status','خطا در ثبت سفارش شما ');
    }

}
