<?php
namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use App\Main;
use App\Sort;
use App\Branch;
use App\Store_Branch;
use App\Store_History;
use App\Payment_Method;
use App\Supplier;
use App\Waste;
use App\Packingmethod;
use App\Physicalcondition;
use App\Driver;
use App\Vehicle;
use App\User;
use App\Customer;
use Carbon\Carbon;
use Auth;
use App\Safe_Account;
use App\Client;
use App\Invoice;
use App\Invoice_Item;
use App\Invoice_Payment;
use App\Cheque;
use App\Bank;
use App\Client_Sheet;
use App\Receipt;
use App\Safe_History;
use App\Review;
use App\Halek;
use App\Sorting;
use App\Notifications;
use App\Imports;

class HalekController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


public function SendNotification ( $noti, $action){


$onesignalAppId = "a7e5d801-ffac-42aa-8551-e7e6e26380e6";
$onesignalApiKey = "os_v2_app_u7s5qap7vrbkvbkr47toey4a4y5y4l4mh4dev5ejtqbatrgeb6cysaaley7g5x7r33dfyjkjqaaky2ujg6nyl5g6crocufk3dk4leey";

// محتوى الإشعار
$heading = array(
    "en" => $action
);
$content = array(
    "en" => " $noti "
);

$data = array(
    "screen" => "Notificationstore"
);

// بيانات الإشعار
$fields = array(
    'app_id' => $onesignalAppId,
    'included_segments' => array('All'), // أو تستخدم 'include_player_ids' لمستخدمين محددين
    'headings' => $heading,
    'contents' => $content,
    'data' => $data,
  

);

$fields = json_encode($fields);

$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/");
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json; charset=utf-8',
    'Authorization: Basic ' . $onesignalApiKey
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$response = curl_exec($ch);
curl_close($ch);

print_r($response);


}



public function halek_create_get()
{
    $branch_id = Auth::user()->branch_id ;
    $sell_stores = Store_Branch::with('store')->where('branch_id',$branch_id)->get();
    return view('halek.create', ['sell_stores' => $sell_stores ]);
}

public function halek_create_post( Request $request )
{
     $branch_id = Auth::user()->branch_id ;
     
    /* $sell_store_id = $request->input('sell_store_id');
     $sell_store = Store_Branch::with('store')->where( 'id', $sell_store_id )->first();
     
     echo "$sell_store";
     return ;
     */
   $halek = Halek::create([
                'sell_store_id' => $request->input('sell_store_id'),
                'source' => $request->input('source'),
                'code' => $request->input('code'),
                'quantity' => $request->input('quantity'),
                'unit_purchase_price' => $request->input('unit_purchase_price'),
                'estimated_sale_price' => $request->input('estimated_sale_price'),
                'total' => $request->input('unit_purchase_price') * $request->input('quantity'),
                'site' => $request->input('site'),
                'reason' => $request->input('reason'),
                'party' => $request->input('party'),
                'date' => $request->input('date'),
                'note' => $request->input('note'),
                'branch_id'=> $branch_id,
            ]);




        //added for notifivation
$quantity = $request->input('quantity');
$sell_store_id = $request->input('sell_store_id');
$sell_store = Store_Branch::with('store')->where( 'id', $sell_store_id )->first();
$sell_name = $sell_store->store->sell_name;

$source  = $request->input('source');
$code = $request->input('code');
$unit_purchase_price = $request->input('unit_purchase_price');
$estimated_sale_price = $request->input('estimated_sale_price');
$total = $request->input('unit_purchase_price') * $request->input('quantity');
$site = $request->input('site');
$reason = $request->input('reason');
$party = $request->input('party');
$date = $request->input('date');
$note = $request->input('note');

$admin_name = Auth::user()->name ;
$branch_name = Auth::user()->branch->name ;
$action =" أشعار تسجيل هالك ";
$n = Notifications::create ( [  'type'=>$action,
'action'=>   "  تم تسجيل هالك جديد بواسطة المستخدم  $admin_name    للصنف  $sell_name   بكمية قدرها   $quantity      وذلك من المورد المصدر  $source /       بيانات التكلفة والتقييم : / قيمة الشراء للوحدة : $unit_purchase_price / قيمة البيع التقديرية : $estimated_sale_price /   تاريخ وارد الصنف : $date / الجهة التي قررت الخصم : $party  /   $reason /  $note"  ] );
 $this->SendNotification (   "  تم تسجيل هالك جديد بواسطة المستخدم  $admin_name    للصنف  $sell_name   بكمية قدرها   $quantity      وذلك من المورد المصدر  $source /       بيانات التكلفة والتقييم : / قيمة الشراء للوحدة : $unit_purchase_price / قيمة البيع التقديرية : $estimated_sale_price /   تاريخ وارد الصنف : $date / الجهة التي قررت الخصم : $party  /   $reason /  $note" , $action);

//end notifivation

    

   return back()->with('success', 'تم ادخال الهالك بنجاح');
}


public function show()
{
    $branch_id = Auth::user()->branch_id ;
    $haleks = Halek::where('branch_id',$branch_id)->get(); 
    return view('halek.show',  ['haleks' => $haleks ]);
}

public function details()
{
    
    return view('halek.details');
}
  



public function sorting_create_get()
{
    $branch_id = Auth::user()->branch_id ;
    $sell_stores = Store_Branch::with('store')->where('branch_id',$branch_id)->get();
    $suppliers = Supplier::get();
    return view('sorting.create', ['sell_stores' => $sell_stores , 'suppliers'=>$suppliers ]);
}

public function sorting_create_post( Request $request )
{
   $branch_id = Auth::user()->branch_id ;
   $sort = Sorting::create([
                'supplier_id' => $request->input('supplier_id'),
                'sell_store_id' => $request->input('sell_store_id'),
                'quantity' => $request->input('quantity'),
                'date_from' => $request->input('date_from'),
                'date_to' => $request->input('date_to'),
                'sort_date' => $request->input('sort_date'),
                'note' => $request->input('note'),
                'branch_id'=> $branch_id,
            ]);

    

   return back()->with('success', 'تم ادخال الفرز بنجاح');
}


public function sorting_show()
{
    $branch_id = Auth::user()->branch_id ;
    $sorting = Sorting::with('supplier')->where('branch_id',$branch_id)->get();
   
    return view('sorting.show',  ['sorting' => $sorting ]);
}

public function sorting_details()
{
    
    return view('sorting.details');
}


public function imports_create_get()
{
    $branch_id = Auth::user()->branch_id ;
//    $sell_stores = Store_Branch::with('store')->where('branch_id',$branch_id)->get();
    $branches = Branch::where('id','!=',$branch_id)->get();
    return view('imports.create', ['branches' => $branches , 'sell_stores'=>[]]);

}


public function getSellStores(Request $request)
    {
       
        
       
        $branch_id = $request->input('branch_id');
        try {
        
            $stores = Store_Branch::with('store')->where('branch_id',$branch_id)->get();
                
            return response()->json([
                'success' => true,
                'stores' => $stores,
                
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'حدث خطأ: ' . $e->getMessage()
            ], 500);
        }
    }


    public function imports_create_post( Request $request )
{


   $branch_id = Auth::user()->branch_id ;
   $import = Imports::create([
                'branch_id'=> $branch_id,
                'import_branch_id'=> $request->input('import_branch_id'),
                'sell_store_id' => $request->input('sell_store_id'),
                'quantity' => $request->input('quantity'),
                'quantity_price' => $request->input('quantity_price'),
                'date' => $request->input('date'),
                'notes' => $request->input('notes'),
                
            ]);

    

   return back()->with('success', 'تم تسجيل الوارد بنجاح');
}

public function imports_show()
{
    $branch_id = Auth::user()->branch_id ;
    $imports = Imports::with('sell_store','branch')->where('branch_id',$branch_id)->get();
    return view('imports.show',  ['imports' => $imports ]);
}


public function imports_edit_get($import_id)
{
    $branch_id = Auth::user()->branch_id ;
    $import = Imports::where('id', $import_id)->first();
    $import_branch_id = $import->import_branch_id; 
    $sell_stores = Store_Branch::with('store')->where('branch_id',$import_branch_id)->get();
    $branches = Branch::where('id','!=',$branch_id)->get();
     
    return view('imports.edit', ['import'=>$import,'branches' => $branches , 'sell_stores'=>$sell_stores]);
}


 public function imports_edit_post( Request $request )
{
   $branch_id = Auth::user()->branch_id ;
   $import_id =  $request->input('import_id');
    Imports::where('id', $import_id  )
                      ->update([
                            'import_branch_id'=> $request->input('import_branch_id'),
                            'sell_store_id' => $request->input('sell_store_id'),
                            'quantity' => $request->input('quantity'),
                            'quantity_price' => $request->input('quantity_price'),
                            'date' => $request->input('date'),
                            'notes' => $request->input('notes'),
                            'updated_at' => Carbon::now()
                      ]);

   return back()->with('success', 'تم تعديل الوارد بنجاح');
}


}
