<?php

namespace App\Http\Controllers\Administrator;

use App\ClientModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\ItemModel;
use DB;
use Illuminate\Support\Facades\Crypt;
use Yajra\DataTables\DataTables;
use Gate;

class SaleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct()
    {
        $this->middleware('permission:Client-Payment-List', ['only' => ['index']]);
    }
    public function index(Request $request)
    {
        if($request->ajax()){
            $client = DB::table('nso007_client as client')
                    ->join('nso007_sale_stock as sale','sale.sale_clientid','client.id')
                    ->join('nso007_sale_detail_stock','nso007_sale_detail_stock.saledetail_saleid','sale.id')
                    ->join('nso007_item_stock','nso007_item_stock.id','nso007_sale_detail_stock.saledetail_itemid')
                    ->join('nso007_loan as loan',function($join){
                        $join->on('loan.loan_saleid',"=",'sale.id')
                        ->whereNull("loan.loan_cancel_status")
                        ->orWhere("loan.loan_cancel_status",1);
                    })
                    ->select(DB::raw('CONCAT(client_firstname," ",client_lastname) as client_fullname'),
                            'client.client_gender',
                            'client.id',
                            'client.client_dob',
                            'client.client_address',
                            'client.client_phone1',
                            DB::raw("lPAD(client.id,6,0) AS client_code"),
                            DB::raw("GROUP_CONCAT(nso007_item_stock.item_house) as item_houses")
                    )
                    ->where(function($query) use($request){
                        if(!empty($request->client_id)){
                            $query->whereIn("sale.sale_clientid",$request->client_id);
                        }
                        if(!empty($request->item_id)){
                            $query->whereIn("nso007_item_stock.id",$request->item_id);
                        }
                    })
                    ->when(\Branch::branch_id()!=1,function($query){
                        return $query->where('loan.branch_id',\Branch::branch_id());
                    })
                    ->groupBy('client.id')->get();
            return DataTables::of($client)
            ->addIndexColumn()
            ->editColumn("client_dob",function($client){
                return date("d-m-Y",strtotime($client->client_dob));
            })
            ->addColumn("item_houses",function($client){
                return $client->item_houses;
            })
            ->addColumn('action',function($client){
                $btn = '';
                if(Gate::check('Client-Payment-View'))
                    return '<a href="'.route('client-payment-list.show',Crypt::encrypt($client->id)).'" class="button_info fa fa-eye"></a>';

            })->make(true);
        }
        $items = ItemModel::join("nso007_project_stock","nso007_project_stock.id","nso007_item_stock.item_projectid")
            ->where("nso007_item_stock.available",0)
            ->when(\Branch::branch_id()!=1,function($query){
                return $query->where('nso007_project_stock.branch_id',\Branch::branch_id());
            })
            ->pluck("item_house","nso007_item_stock.id");
        $clients = ClientModel::pluck("client_fullname","id");
        return view('administrator.client-payment.list.sale',compact('items','clients'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
