<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Models\Employee;
use App\Models\Salary;

class SalaryController extends Controller
{
    public function index(Request $request){
        //$this->checkpermission('employee-payment');
        $show = $request->show;
        if($show != ''){
            $payments = Salary::latest()->paginate($show);
        }else{
            $payments = Salary::latest()->paginate(setting()->ddspinp);
        }
        /*--------------search which supplier who have due amount------------------------*/
        $employees = Employee::where('status','Active')->orderBy('name','ASC')->get();
       
        return view('backend.salary.index',compact('payments','employees'));
    }
    public function create(Request $request){
        $this->validate($request, [
            'employee_name' => 'required',
        ]);
        $employee = Employee::where('id',$request->employee_name)->first();
        $employees = Employee::where('status','Active')->orderBy('name','ASC')->get();
        if($employee != NULL){
            return view('backend.salary.create',compact('employee','employees'));
        }else{
            return redirect()->back()->with('error','Woops! No Information Found, Please Check the Supplier Name Or Phone No ');
        }
    }
    public function store(Request $request){
        $this->validate($request,[
            'employee_id'=>'required',
            'pay_date'=>'required',
            'payment'=>'required',
        ]);
        $closing =  DB::table('cash_closings')->where('closing_date', date('Y-m-d',strtotime($request->pay_date)))->exists();

        if($closing != true){
            //return $request->all();
            $employee_id = $request->employee_id;
            $amount = $request->payment;
            $pay_date = $request->pay_date;
            $employee = Employee::find($request->employee_id);
            
            if($amount > 0){
                //for employee account
                $salary = new Salary();
                $dt = Carbon::parse($pay_date);
                $salary->employee_id = $request->employee_id;
                $salary->payment_date = $dt->toDateTimeString();
                $salary->pay_month = date('m-Y',strtotime($pay_date));
                $salary->amount =  $amount;
                $salary->total_payment =  $request->total_payment;
                $salary->note = $request->note;
                $salary->created_by = Auth::id();
                $salary->save();
            }
            /*--------check the sms sending section--------*/
            if(isset($request->sms)){
                //return $request->sms;
                $to = $cus->phone;//"017xxxxxxx,+88016xxxxxxx";
                $token = "7a787dd847f4650b1ba1adfb8771c733";
                $message = about()->english_name.','.'Previous due:'.$record->previous_due.',payment:'.$record->payment_amount.',total due:'.$record->total_due.',Note:'.$record->note.',Date:'.date('d-m-Y',strtotime($record->transaction_date)).'.'.about()->phone;
                $url = "http://api.greenweb.com.bd/api.php?json";
                $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);
                //Result
                //echo $smsresult;
                //Error Display
                //echo curl_error($ch);
                DB::table('sms')->where('id',1)->decrement('sms_balance',2);
            }
            return redirect()->route('salary.list')->with('success','Salary payment successfull');
        }
        else{
            return redirect()->back()->with('error','Woops! After cash closing all transaction has been stoped');
        }
    }
    public function edit($id){
        $salary = Salary::find($id);
        $employee = Employee::find($salary->employee_id);
        $employees = Employee::where('status','Active')->orderBy('name','ASC')->get();
        return view('backend.salary.edit',compact('salary','employee','employees'));
    }
    public function update(Request $request, $id){  
        $this->validate($request,[
            'employee_id'=>'required',
            'pay_date'=>'required',
            'payment'=>'required',
            'total_payment'=>'required',
        ]);
        $salary = Salary::find($id);
        $dt = Carbon::parse($request->pay_date);
        $salary->employee_id = $request->employee_id;
        $salary->payment_date = $dt->toDateTimeString();
        $salary->pay_month = date('m-Y',strtotime($request->pay_date));
        $salary->amount =  $request->payment;
        $salary->total_payment =  $request->total_payment;
        $salary->note = $request->note;
        $salary->updated_by = Auth::id();
        $salary->save();
        
        return redirect()->route('salary.list')->with('success','Salary payment successfull');
    }
}