<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Auth;
use App\Models\Purchase;
use App\Models\Unit;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Product;
use App\Models\WorkerRecord;
use App\Models\Worker;

class WorkerController extends Controller
{
    public function index(Request $request){
        $show = $request->show;
        if($show != ''){
            $workers = Worker::latest()->paginate($show);
        }else{
            $workers = Worker::latest()->paginate(setting()->ddspinp);
        }

        return view('backend.worker.index',compact('workers'));
    }
    public function create(){
        return view('backend.worker.create');
    }
    /*-------worker record/history--------*/
    public function worker_record(Request $request){
        $this->checkpermission('worker-list');
        $from_date = date('Y-m-01');
        $to_date = date('Y-m-t');

        $records = WorkerRecord::whereDate('transaction_date', '>=',$from_date)->whereDate('transaction_date', '<=' , $to_date)->get();
        $workers = Worker::get(['id','name','phone']);
        return view('backend.worker.record',compact('records','workers','from_date','to_date'));
    }
    public function worker_records(Request $request){
        $this->checkpermission('worker-list');
        $from_date = date('Y-m-d',strtotime($request->from_date));
        $to_date = date('Y-m-d',strtotime($request->to_date));

        if($request->worker == 'All'){
            $records = WorkerRecord::whereDate('transaction_date', '>=',$from_date)->whereDate('transaction_date', '<=' , $to_date)->get();
        }else{
            $records = WorkerRecord::where('worker_id',$request->worker)->whereDate('transaction_date', '>=',$from_date)->whereDate('transaction_date', '<=' , $to_date)->get();
        }
        $workers = Worker::get(['id','name','phone']);
        return view('backend.worker.record',compact('records','workers','from_date','to_date'));
    }
    /*-----------sms send from worker record-------------*/
    public function worker_record_send($id){
        $record = WorkerRecord::find($id);
        //return $record;
        $to = $record->worker->phone;//"017xxxxxxx,+88016xxxxxxx";
        $token = "daf0cc0b638862523830815f351a8063";
        $message = "Dear Worker Total price:".$record->total_price.',pay:'.$record->payment_amount.',due:'.$record->due_amount.',prev due:'.$record->previous_due.',total due:'.$record->total_due.',Date:'.date('d-m-Y',strtotime($record->transaction_date)).'.'.about()->english_name.','.about()->phone;
        $url = "http://api.greenweb.com.bd/api.php";
        $data= array(
            'to'=>"$to",
            'message'=>"$message",
            'token'=>"$token"
        ); // Add parameters in key value

        $ch = curl_init(); // Initialize cURL
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $smsresult = curl_exec($ch);

        return redirect()->route('worker.worker-record')->with('success',$smsresult);
    }
    public function store(Request $request){
        $this->validate($request,[
            'worker_name'=>'required'
        ]);

        $worker = new Worker();
        $worker->name = $request->worker_name;
        $worker->phone = $request->worker_phone;
        $worker->email = $request->worker_email;
        $worker->address = $request->worker_address;
        $worker->current_balance = $request->previous_due;
        $worker->save();

        /*-------------previous due section-----------------*/
        /*if($request->previous_due != ''){
            $invoices = Purchase::latest()->first();
            if($invoices != ''){
                $order_no = date('dmy').$invoices->id + 1;
            }
            else{
                $order_no = date('dmy').'1';
            }
            $purchase = new Purchase();
            $purchase->worker_id = $worker->id;
            $purchase->chalan_no = $order_no;
            $purchase->purchase_date =  date('Y-m-d');
            $purchase->total_price = $request->previous_due;
            $purchase->discount = 0;
            $purchase->grand_total = $request->previous_due;
            $purchase->paid_amount = 0;
            $purchase->due_amount = $request->previous_due;;
            $purchase->created_by = Auth::id();
            $purchase->save();

            $record = new WorkerRecord();
            $record->worker_id = $purchase->worker_id;
            $record->transaction_date = $purchase->purchase_date;
            $record->user_id = Auth::id();
            $record->total_price = $purchase->due_amount;
            $record->payment_amount = 0;
            $record->due_amount = $purchase->due_amount;
            $total_due = $purchase->due_amount;
            $record->previous_due = $total_due - $request->due;
            $record->total_due = $total_due;
            $record->message = "Dear Worker ".$worker->name.", your invoice total price is ".$purchase->grand_total."/=, Payment ".$purchase->payment."/= Due ".$purchase->due."/= Previous Due ".$purchase->due_amount."/= Current Due ".$total_due." Thanks By ".about()->english_name.", ".about()->address;
            $record->created_by = Auth::id();
            $record->save();

        }*/

        return redirect()->route('worker.list')->with('success','Worker Information Save Successfull');

    }
    public function show($id){
        //
    }
    public function edit($id){
        $worker = Worker::find($id);
        return view('backend.worker.edit',compact('worker'));
    }
    public function update(Request $request, $id){
        $this->validate($request,[
            'name'=>'required',
            'phone'=>'required',
            //'address'=>'required'
        ]);

        $worker = Worker::find($id);
        $worker->name = $request->name;
        $worker->phone = $request->phone;
        $worker->email = $request->email;
        $worker->address = $request->address;
        $worker->save();

        return redirect()->route('worker.list')->with('success','Worker Information Update Successfull');
    }
    public function destroy($id){
        //return $id;
		$worker = Worker::find($id);
        $worker->delete();

        return back()->with('success','Worker Information Successfully Deleted ');
    }
}
