<?php

namespace App\Http\Controllers\Administrator;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;
use DB;
use Gate;
class CommissionConditionController extends Controller
{
    function __construct()
    {
        $this->middleware('permission:Withdraw-Condition-List',['only'=>['index']]);
        $this->middleware('permission:Withdraw-Condition-Create', ['only' => ['create','store']]);
        $this->middleware('permission:Withdraw-Condition-Edit', ['only' => ['edit','update']]);
        $this->middleware('permission:Withdraw-Condition-Delete', ['only' => ['destroy']]);
        $this->middleware('permission:Withdraw-One-Condition-List', ['only' => ['index_one_condition']]);

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('administrator.commission-condition.index');
    }
    public function get_data(){
        $data = DB::table('nso007_commission_condition')->where('status','=',1)->get();
        return DataTables::of($data)
        ->addIndexColumn()
//        ->addColumn('checkbox',function($data){
//            $checkbox = '';
//            if($data->status==2){
//                $checkbox =  '<label class="checkbox-list">
//                            <input id="'.$data->id.'" type="checkbox" class="checkbox" name="status" checked>
//                            <span class="checkmark"></span>
//                        </label>';
//            }else{
//                $checkbox = '<label class="checkbox-list">
//                            <input id="'.$data->id.'" type="checkbox" class="checkbox" name="status">
//                            <span class="checkmark"></span>
//                        </label>';
//            }
//            return $checkbox;
//        })
        ->addColumn('action',function($data){
            $edit = '';
            $delete = '';
            if(Gate::check('Withdraw-Condition-Edit'))
            {
                $edit   = '<a style="color:white;margin-left:0;" data-id="'.$data->id.'" data-condition_name="'.$data->condition_name.'"  data-condition="'.$data->condition.'" data-commission="'.$data->commission.'" href="#" class="btn_edit_condition button_info btn-circle btn-xs"><i class="fa fa-pencil" title="Edit Time"></i></a>';
            }
            if(Gate::check('Withdraw-Condition-Delete'))
            {
                $delete = '<a style="color:white;margin-left:0;" data-id="'.$data->id.'" href="#" class="btn_remove_condition button_danger btn-circle btn-xs"><i class="fa fa-trash" title="Remove Time"></i></a>';
            }
            return $edit.$delete;
        })
        ->rawColumns(['checkbox'=>'checkbox','action'=>'action'])
        ->make(true);
    }

    /**
     * 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)
    {
        $this->validate($request,[
            'condition_name' => 'required',
            'condition' => 'required',
            'commission' => 'required'
        ]);
        $condition = implode('/',$request->condition);
        $commission = implode('/',$request->commission);

        DB::table('nso007_commission_condition')->insert([
            'condition_name' => $request->condition_name,
            'condition' => $condition,
            'status' => 1,
            'commission' => $commission
        ]);
        \LogActivity::addToLog('Create Commission Condition Stored Successfully !');
        return back()->with('success','Create Commission Condition Stored Successfully');
    }

    /**
     * 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)
    {
        // dd($request->all());
        $this->validate($request,[
            'condition_name' => 'required',
            'condition' => 'required',
            'commission' => 'required'
        ]);
        DB::table('nso007_commission_condition')->where('id',$id)->update(
            [
                'condition_name'  => $request->condition_name,
                'condition'  => implode('/',$request->condition),
                'status'  => 1,
                'commission' => implode('/',$request->commission)
            ]
        );
        return back()->with('success','Commission Condition was successfully updated');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // dd("holp");
        DB::table('nso007_commission_condition')->where('id',$id)->delete();
        return back()->with('success','Commission Condition was successfully deleted');
    }
    public function update_status($id){
        $condition = DB::table('nso007_commission_condition')->find($id);
        // dd($condition);
        if($condition->status==1){
            DB::table('nso007_commission_condition')->where('id',$id)->update([
                'status' => 2
            ]);
        }else{
            DB::table('nso007_commission_condition')->where('id',$id)->update([
                'status' => 1
            ]);
        }
        
        echo \json_encode($condition);
    }

    public function index_one_condition()
    {
        return view('administrator.commission-condition-one.index');
    }
    public function get_data_one_condition(){
        $data = DB::table('nso007_commission_condition')->where('status','=',2)->get();
        return DataTables::of($data)
            ->addIndexColumn()
            ->addColumn('action',function($data){
                $action="";
                if(Gate::check('Withdraw-One-Condition-Edit'))
                    $action.='<a style="color:white;margin-left:0;" data-id="'.$data->id.'" data-condition-name="'.$data->condition_name.'" data-condition="'.$data->condition.'" data-commission="'.$data->commission.'" href="#" class="btn_edit_condition_one button_info btn-circle btn-xs"><i class="fa fa-pencil" title="Edit Time"></i></a>';
                if(Gate::check('Withdraw-One-Condition-Delete'))
                    $action.='<a style="color:white;margin-left:0;" data-id="'.$data->id.'" href="#" class="btn_remove_condition button_danger btn-circle btn-xs"><i class="fa fa-trash" title="Remove Time"></i></a>';
                return $action;
            })
            ->rawColumns(['checkbox'=>'checkbox','action'=>'action'])
            ->make(true);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store_one_condition(Request $request)
    {
        $this->validate($request,[
            'condition_name' => 'required',
            'condition' => 'required',
            'commission' => 'required'
        ]);
        $condition = $request->condition;
        $commission = $request->commission;

        DB::table('nso007_commission_condition')->insert([
            'condition_name' => $request->condition_name,
            'condition' => $condition,
            'status' => 2,
            'commission' => $commission
        ]);
        \LogActivity::addToLog('Create Withdraw Condition One Stored Successfully !');
        return back()->with('success','Create Withdraw Condition One Stored Successfully');
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update_one_condition(Request $request, $id)
    {
        $this->validate($request,[
            'condition_name' => 'required',
            'condition' => 'required',
            'commission' => 'required'
        ]);
        DB::table('nso007_commission_condition')->where('id',$id)->update(
            [
                'condition_name' => $request->condition_name,
                'condition'  => $request->condition,
                'status' => 2,
                'commission' => $request->commission
            ]
        );
        \LogActivity::addToLog('Updated Withdraw Condition One Stored Successfully !');
        return back()->with('success','Withdraw Condition was successfully updated');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy_one_condition($id)
    {
        // dd("holp");
        DB::table('nso007_commission_condition')->where('id',$id)->delete();
        \LogActivity::addToLog('Deleted Withdraw Condition One Stored Successfully !');
        return back()->with('success','Withdraw Condition One was successfully deleted');
    }

}
