<?php

namespace App\Http\Controllers\Administrator\Construction;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Yajra\DataTables\DataTables;
use App\ProjectModel;
use App\ConstructionModel\ConstructorModel;
use App\ConstructionModel\ProductModel;

class ProductOriginStockController extends Controller
{
    public function origin_stock_list(Request $request)
    {
        if($request->ajax()){
            $data =DB::table('nso007_construction_product_origin_stock')
            ->leftJoin("nso007_zone_construction","nso007_zone_construction.id","nso007_construction_product_origin_stock.zone_id")
            ->leftJoin('nso007_warehouse_construction','nso007_warehouse_construction.id','nso007_construction_product_origin_stock.warehouse_id')
            ->leftJoin("nso007_project_stock","nso007_project_stock.id","nso007_warehouse_construction.project_id")
            ->leftJoin('nso007_construction_product','nso007_construction_product.id','nso007_construction_product_origin_stock.product_id')
            ->leftJoin('nso007_product_brand_construction','nso007_product_brand_construction.id','nso007_construction_product.brand_id')
            ->leftJoin('nso007_product_categories_construction','nso007_product_categories_construction.id','nso007_construction_product.category_id')
            ->leftJoin('nso007_unit_construction','nso007_unit_construction.id','nso007_construction_product.unit_id')
            ->select("nso007_construction_product_origin_stock.*",
                "nso007_project_stock.project_name",
                "nso007_construction_product.name as product_name",
                'nso007_unit_construction.name as unit_name',
                'nso007_product_categories_construction.name as product_category_name',
                "nso007_product_brand_construction.name as brand_name",
                "nso007_warehouse_construction.name as warehouse_name",
                "nso007_zone_construction.name as zone_name",
            )
            ->when(empty(\Branch::branch_key()), function ($query) {
                $query->where('nso007_project_stock.branch_id', \Branch::branch_id());
            })
            ->get();
            return  DataTables::of($data)
                ->setRowId('id')
                ->addIndexColumn()
                ->addColumn('action', function($data) {
                    $button ="";
                    $button = "<a title='Divide Product' href='".route('construction.origin_stock_list_divide',1)."' class='btn btn-sm btn-primary'><i class='fa fa-scissors' aria-hidden='true'></i></a>";
                    return $button;
                })
                ->editColumn('zone_name',function($data){
                    if($data->using_to==2) return "All";
                    else return $data->zone_name;
                })
                ->rawColumns(['action'=>'action'])
                ->make(true);
        }
        return view('administrator.construction.product-stock.origin-stock-list');
    }
    public function form_request_warehouse(Request $request)
    {
        if($request->ajax()){
            $data =DB::table('nso007_construction_product_origin_stock')
            ->leftJoin('nso007_warehouse_construction','nso007_warehouse_construction.id','nso007_construction_product_origin_stock.warehouse_id')
            ->leftJoin("nso007_project_stock","nso007_project_stock.id","nso007_warehouse_construction.project_id")
            ->leftJoin('nso007_construction_product','nso007_construction_product.id','nso007_construction_product_origin_stock.product_id')
            ->leftJoin('nso007_product_brand_construction','nso007_product_brand_construction.id','nso007_construction_product.brand_id')
            ->leftJoin('nso007_product_categories_construction','nso007_product_categories_construction.id','nso007_construction_product.category_id')
            ->leftJoin('nso007_unit_construction','nso007_unit_construction.id','nso007_construction_product.unit_id')
            ->select("nso007_construction_product_origin_stock.*",
                "nso007_project_stock.project_name",
                "nso007_construction_product.name as product_name",
                'nso007_unit_construction.name as unit_name',
                'nso007_product_categories_construction.name as product_category_name',
                "nso007_product_brand_construction.name as brand_name",
                "nso007_warehouse_construction.name as warehouse_name"
                )
            ->when(empty(\Branch::branch_key()), function ($query) {
                $query->where('nso007_project_stock.branch_id', \Branch::branch_id());
            })
            ->get();
            return  DataTables::of($data)
                ->setRowId('id')
                ->addIndexColumn()
                ->addColumn('action', function($data) {
                    $button ="";
                    return $button;
                })
                ->rawColumns(['action'=>'action'])
                ->make(true);
        }
        return view('administrator.construction.product-stock.form-request-warehouse');
    }
    public function create_request_warehouse()
    {
        $contractors = ConstructorModel::join('nso007_project_stock','nso007_project_stock.id','nso007_constructor_construction.project_id')
            ->when(empty(\Branch::branch_key()), function ($query) {
                $query->where('nso007_project_stock.branch_id', \Branch::branch_id());
             })
            ->pluck('nso007_constructor_construction.name','nso007_constructor_construction.id')
            ->prepend('Please Select','');
        $projects = ProjectModel::when(empty(\Branch::branch_key()), function ($query) {
            $query->where('nso007_project_stock.branch_id', \Branch::branch_id());
        })->pluck('project_name','id')->prepend("Please Select","");

        $product_get = ProductModel::get();
        $products = '<option value="">Please Select</option>';
        foreach($product_get as $key=>$value)
        {
            $products.='<option value="'.$value->id.'">'.$value->name.'</option>';
        }

        return view('administrator.construction.product-stock.create-request-stock-out',compact('contractors','projects','products'));
    }
    public function select_warehouse($project_id)
    {
        return json_encode(DB::table('nso007_warehouse_construction')->where('project_id',$project_id)->get());
    }
    public function store_request_warehouse(Request $request)
    {
        dd("OK");
    }
}
