<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App;

class MergeController extends Controller
{
	protected $childarray=[];
    public function __construct()
    {
        $this->middleware('auth');
    }
   
    public function show(Request $request)
    {
    	$office_id=$request->session()->get('session_office');
    	$id = Auth::user()->id;
      	//$creation_of_tapal=\App\Tapal::where('id',$id)->get();
     	$currentseat=\App\User_Seat::where('user_id',$id)->first();
        $currentseatid=$currentseat->seat_id;
        if(!$request->session()->has('session_seat'))
        {
           $request->session()->put('session_seat',$currentseatid);
        }
        else
        {
            $currentseatid=$request->session()->get('session_seat');
        }
   		$seatid=$request->session()->get('session_seat');
      	$tapalinbox = \App\Tapal_forward::with('tapal')->whereHas('tapal', function ($query) use ($office_id) { $query->where('status','=','0')->where('office_id',$office_id)->with('user'); })->where('to_seat', $seatid)->where('active_flag',1)->get();
    	return view('merge',compact('tapalinbox'));   
	}
	public function merge(Request $request)
	{ 
		$id=Auth::user()->id;
	    $tapalid=$request->input("tapalmerge");
	    $tapalid2=$request->input("tapalmerge2");
	    $tapal=\App\Tapal::find($tapalid);

	    $mergeinsertobj=\App\MergeTapal::create([//model name database field name
					'tapal_id' => $tapalid,
					'merge_to_tapal' => $tapalid2,
					'merged_by' => $id,
					'merged_on'=>date("Y-m-d"),
					]);
	    //Documents  of firsttapal
    	$attachments=\App\Documents::where('tapal_id','=',$tapalid)->with('tapal')->get();
		foreach($attachments as $attachment)
		{
			$path=$attachment->document_path;
			$filedesc=$attachment->document_name;
			//insert a new document to documents with tapal_id=$tapalid2
			$insertobj=\App\Documents::create([//model name database field name
					'tapal_id' => $tapalid2,
					'document_path' => $path,
					'document_name' => $filedesc,
					'updated_by'=>$id
					]);
		}
		//comments of firstTapal
		$tapal_comment = \App\Comments::where('tapal_id',$tapalid)->with('seat','tapal')->get();
		foreach($tapal_comment as $tapcom)
		{    
			$seatid=$tapcom->seat_id;
			$comtyp=$tapcom->comment_type;
			$comdes=$tapcom->comment_description;
			//insert a new comment to comments with tapal_id=$tapalid2
			$insertobj=\App\Comments::create([//model name database field name
					'tapal_id' => $tapalid2,
					'seat_id' => $seatid ,
					'comment_type' => $comtyp,
					'comment_description' => $comdes,
					'updated_by'=>$id
					]);
		} 
		//Actions of FirstTapal
		$actions=\App\Tapal_action::where('tapal_id',$tapalid)->get();
		foreach($actions as $action)
		{
			$seatid=$action->seat_id;
			$actiontyp=$action->action_type;
			$actiondes=$action->action_description;
			//insert a new action to actions with tapal_id=$tapalid2

			$actionfrwd = \App\Tapal_action::create([
				'tapal_id' => $tapalid2,
				'seat_id' =>  $seatid,
				'action_type' => $actiontyp,
				'action_description' =>$actiondes,
				'created_by'=>$id
				]);
		}
		$tapal->status=1;
		$tapal->save();
		return redirect('inbox'); 
	} 
}