<?php  

defined('BASEPATH') OR exit('No direct script access allowed');



class Brchannelend extends CI_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->model('Brchannelmodel', 'channel_model');

    }
    //added channel function
     public function add(){
       
            // $request= new Request();
            // $admin=$request->getBody();

            // 'inputadminname':$('#inputadminname').val(),
            //     'inputloginname':$('#inputloginname').val(),
            //     'inputPassword'
            // $name=$this->input->post('name');
            
            //add security trim input-todo
            //hash password for security
            $name=$this->input->post('name');



            $data= array(
                'name'=>$name,

            );

            $code="";
            // if($this->frontenduser_model->admin_exists($inputadminname)==false){
            //     $code="FAILURE";
            //     $msg="已有管理員姓名".$inputadminname;
            // }
            // else
            if($this->channel_model->name_exists(strtolower($name))==false){
                $code="FAILURE";
                $msg="已有會員來源".$shortcode;
            }
            else{
                $result=$this->channel_model->insertChannel($data);
                if($result==false){
                    $code="FAILURE";
                    $msg="記錄不能加入";     
                }
                else{
                    $code="SUCCESS";
                    $msg="記錄成功加入"; 
                    //for add user it will add a secret key to database
                    
                    //end 

                }
            }

             
            

             header('Content-Type: application/json');
            // $code="SUCCESS";
            // $code="FAILURE";
            // $msg="已有相關記錄";
            // $myArray = ['id'=>12, 'name'=>'Topi'];
            $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
            echo json_encode($myArray);
            // echo "posted";
        
    }
    public function edit(){
       
        // $request= new Request();
        // $admin=$request->getBody();

        // 'inputadminname':$('#inputadminname').val(),
        //     'inputloginname':$('#inputloginname').val(),
        //     'inputPassword'
        // $name=$this->input->post('name');
        
        //add security trim input-todo
        //hash password for security
        $name=$this->input->post('name');
        $id=$this->input->post('id');

        $data= array(

            'name'=>$name,
            'id'=>$id
        );

        $code="";
        // if($this->frontenduser_model->admin_existsbyid($inputadminname,$id)==false){
        //     $code="FAILURE";
        //     $msg="已有管理員姓名".$inputadminname;
        // }
        // else
        if($this->channel_model->name_existsbyid(strtolower($name),$id)==false){
            $code="FAILURE";
            $msg="已有會員來源".$name;
        }
        else{
            $result=$this->channel_model->updateChannel($data);
            if($result==false){
                $code="FAILURE";
                $msg="記錄不能更改";     
            }
            else{
                $code="SUCCESS";
                $msg="記錄成功更改";    
            }

            
        }

         
        

         header('Content-Type: application/json');
        // $code="SUCCESS";
        // $code="FAILURE";
        // $msg="已有相關記錄";
        // $myArray = ['id'=>12, 'name'=>'Topi'];
        $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
        echo json_encode($myArray);
        // echo "posted";
    
    }

     public function delete(){
        $inputdeletecheckboxvalue=$this->input->post('inputdeletecheckboxvalue');
        $result=true;
        foreach ($inputdeletecheckboxvalue as $key => $values){
            if($this->channel_model->deleteAdmin($values)==false){
                $result=false; 
            }
           
            
        }//foreach ($inputdeletecheckboxvalue as $key => $values){
    
        if($result==true){
            $code="SUCCESS";
            $msg="相關記錄成功删徐";
        }
        else{
            $code="FAILURE";
            $msg="相關記錄不能删徐";
        }
       
        // $msg=$inputdeletecheckboxvalue;
        header('Content-Type: application/json');
        $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
        echo json_encode($myArray);
    }
    //end added channel function

    public function get($id=Null){
        $partnerlist=$this->frontenduser_model->getBannerAll();
        echo  '1';
        print_r($partnerlist);
    }

    
    
   
    public function getadmindata(){
        $id=$this->input->post('user_id');
        $data=$this->frontenduser_model->getAdmindateByid($id);
        header('Content-Type: application/json');
        echo json_encode($data);
    }

}//class Admins {
