<?php

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


class Index extends CI_Controller
{
    public $uid;
    public function __construct() 
    {
        parent::__construct();
        $this->permission->is_admin();
        $this->load->model('Navigation_model');
        $admin_data=$this->session->userdata('admin_chtcdn');
        $this->uid=$admin_data['OPI_SN'];
    }

    public function index() 
    {
        $data=array();
        $data['card_list']=array();
        $data['card_list']=$this->Navigation_model->get_card_list($this->uid);
        $n_nt_sn_string='0';
        foreach ($data['card_list'] as $c) {
            $n_nt_sn_string.=','.$c->nt_sn;
        }
        $data['nav_list']=array();
        $nav_list=$this->Navigation_model->get_nav_list($n_nt_sn_string);
        foreach ($nav_list as $v) {
            $data['nav_list'][$v->n_nt_sn][]=$v;
        }
        $data['mid']=$this->uid;
        $this->load->view('n-header', $data);
        $this->load->view('index/index',$data);
        $this->load->view('n-footer');
    }

    public function search()
    {
        $data=array();
        $data['q']=$this->input->post('q')==''?'请输入关键词，吼吼！':$this->input->post('q');
        $this->load->model('Operator_model');
        $data['userlist']=$this->Operator_model->get_user_by_name($data['q'],50);
        $data['navlist']=$this->Navigation_model->get_nav_list(false,$data['q']);
        $data['other_nav']=$this->Navigation_model->get_nav_list('1');

        $keyword=explode(' ',$data['q'],2);
        $data['keyword1']=isset($keyword[1])?$keyword[0]:'';
        $data['keyword2']=isset($keyword[1])?$keyword[1]:$keyword[0];
        $data['orderId'] =trim(str_replace('订单', '', $data['q']));
        $data['c_action']='search';

        $this->load->view('n-header', $data);
        $this->load->view('index/search');
        $this->load->view('n-footer');
    }

    public function add_card()
    {
        $nt_name=$this->input->post('nt_name');
        $insertid=$this->Navigation_model->add_nav_type($nt_name,$this->uid);
        if ($insertid) {
            echo json_encode(array('status'=>'ok','msg'=>'添加成功!','action'=>'add_card'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'添加失败!'));
        }
    }

    public function add_nav()
    {
        $n_name=$this->input->post('n_name');
        $n_link=$this->input->post('n_link');
        $n_nt_sn=$this->input->post('n_nt_sn');
        $n_description=$this->input->post('n_description');

        $insertid=$this->Navigation_model->add_nav($n_name,$n_link,$n_description,$n_nt_sn,$this->uid,1);
        if ($insertid) {
            echo json_encode(array('status'=>'ok','msg'=>'添加成功!','action'=>'add_nav'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'添加失败!'));
        }
    }

    public function edit()
    {
        $n_name=$this->input->post('n_name');
        $n_link=$this->input->post('n_link');
        $n_sn=$this->input->post('n_sn');
        $n_description=$this->input->post('n_description');
        $result=$this->Navigation_model->update_nav($n_name,$n_link,$n_sn,$n_description,$this->uid);
        if ($result) {
            echo json_encode(array('status'=>'ok','msg'=>'编辑成功!','action'=>'edit'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'编辑失败!'));
        }
    }

    public function delete()
    {
        $n_sn=$this->input->post('n_sn');
        $result=$this->Navigation_model->delete_nav($n_sn,$this->uid);
        if ($result) {
            echo json_encode(array('status'=>'ok','msg'=>'删除成功!','action'=>'delete'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'删除失败!'));
        }
    }

    public function edit_type()
    {
        $nt_name=$this->input->post('nt_name');
        $nt_sn=$this->input->post('nt_sn');
        $result=$this->Navigation_model->update_type($nt_name,$nt_sn,$this->uid);
        if ($result) {
            echo json_encode(array('status'=>'ok','msg'=>'更新成功!','action'=>'edit_type'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'更新失败!'));
        }
    }

    public function delete_type($nt_sn)
    {
        $result=$this->Navigation_model->delete_type($nt_sn,$this->uid);
        if ($result) {
            echo json_encode(array('status'=>'ok','msg'=>'删除成功!','action'=>'delete_type'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'删除失败!'));
        }
    }
}