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

/**
 * Class 告警信息管理类
 */
class Warning extends MY_Controller{

    function __construct(){
        parent::__construct();
        $this->load->helper('form');
        $this->load->model("warning_model");
        $this->load->library('MY_pagination');
        $this->load->helper('url');
        $this->assign("warning_type",$this->warning_type);
        $this->assign("warning_level",$this->warning_level);
        $this->assign("warning_status",$this->warning_status);
    }

    /**
     * 状态数量统计
     */
    private function counts()
    {
        $count10 = $this->warning_model->count_warning(null, $wheres = array('status' => "10"));
        $count20 = $this->warning_model->count_warning(null, $wheres = array('status' => "20"));
        $count30 = $this->warning_model->count_warning(null, $wheres = array('status' => "30"));
        $count40 = $this->warning_model->count_warning(null, $wheres = array('status' => "40"));
        $this->assign("count10", $count10);
        $this->assign("count20", $count20);
        $this->assign("count30", $count30);
        $this->assign("count40", $count40);
    }

    /**
     * 告警列表页
     */
    public function index(){
        $url = site_url("warning/index?");
        $wheres = array();
        $order_info = array("status"=>"ASC","create_time"=>"DESC");
        $warning_name = $this->input->get("warning_name",TRUE);
        $branch_id = $this->input->get("branch_id",TRUE);
        $type = $this->input->get("type",TRUE);
        $status = $this->input->get("status",TRUE);
        $order = $this->input->get("order",TRUE);
        $page_num = $this->input->get("per_page",TRUE);
        $page_size = $this->input->get("page_size",TRUE);
        if($warning_name){
            $url .= "&warning_name=".$warning_name;
        }
        if($branch_id){
            $wheres['branch.branch_id'] = $branch_id;
            $url .= "&branch_id=".$branch_id;
        }
        if($type){
            $wheres['type'] = $type;
            $url .= "&type=".$type;
        }
        if($status) {
            $wheres['status'] = $status;
            $url .= "&status=".$status;
        }
        if($order){
            $orders = explode(" ",$order);
            if(count($orders)==2) {
                $order_info[$orders[0]] = $orders[1];
                $url .= "&order=".$order;
            }
        }
        if($page_size){
            $this->page_size = $page_size;
            $url .= "&page_size=".$page_size;
        }
        $count = $this->warning_model->count_warning($warning_name,$wheres);
        $this->assign("count",$count);
        $config = $this->page_config($count,$this->page_size,$url);
        $this->my_pagination->initialize ($config);
        if($page_num && $page_num>1) {
            $offset = (intval($page_num)-1)*$this->page_size;
        }else {
            $offset = 0;
        }
        $warning_list = $this->warning_model->list_warning($this->page_size,$offset,$warning_name,$wheres,$order_info);
        $this->counts();
        $this->assign("warning_name",$warning_name);
        $this->assign("type",$type);
        $this->assign("branch_id",$branch_id);
        $this->assign("status",$status);
        $this->assign("order",$order);
        $this->assign("page_size",$this->page_size);
        $this->assign("page",$this->my_pagination->create_pages());
        $this->assign("warning_list",$warning_list);
        $this->display("warning/index.html");
    }

    public function view($warning_id){
        $warning_info = $this->warning_model->get_warning_with_id($warning_id);
        $this->assign("warning",$warning_info);
        $this->display("warning/view.html");
    }

    /**
     * 已忽略告警信息列表页
     */
    public function ignoreindex(){
        $url = site_url("warning/ignoreindex?");
        $wheres = array();
        $order_info = array();
        $warning_name = $this->input->get("warning_name",TRUE);
        $type = $this->input->get("type",TRUE);
        $order = $this->input->get("order",TRUE);
        $page_num = $this->input->get("per_page",TRUE);
        $page_size = $this->input->get("page_size",TRUE);
        if($warning_name){
            $url .= "&warning_name=".$warning_name;
        }
        if($type){
            $wheres['type'] = $type;
            $url .= "&type=".$type;
        }
        $wheres['status'] = "20";
        if($order){
            $orders = explode(" ",$order);
            if(count($orders)==2) {
                $order_info[$orders[0]] = $orders[1];
                $url .= "&order=".$order;
            }
        }
        if($page_size){
            $this->page_size = $page_size;
            $url .= "&page_size=".$page_size;
        }
        $count = $this->warning_model->count_warning($warning_name,$wheres);
        $config = $this->page_config($count,$this->page_size,$url);
        $this->my_pagination->initialize ($config);
        if($page_num && $page_num>1) {
            $offset = (intval($page_num)-1)*$this->page_size;
        }else {
            $offset = 0;
        }
        $warning_list = $this->warning_model->list_warning($this->page_size,$offset,$warning_name,$wheres,$order_info);
        $this->assign("count",$count);
        $this->assign("warning_name",$warning_name);
        $this->assign("type",$type);
        $this->assign("order",$order);
        $this->assign("page_size",$this->page_size);
        $this->assign("page",$this->my_pagination->create_pages());
        $this->assign("warning_list",$warning_list);
        $this->display("warning/ignoreindex.html");
    }

    /**
     * 忽略告警信息
     * @param $warning_id 告警信息ID
     */
    public function ignore($warning_id){
        $msg = array();
        $warning_info = $this->warning_model->get_warning_with_id($warning_id);
        if($warning_info){
            $warning_info['status'] = "20";
            $this->warning_model->update_warning($warning_info);
            $msg['msg'] = "忽略告警信息成功!";
            $msg['icon'] = 1;
        }else{
            $msg['msg'] = "告警信息不存在或者已经被删除!";
            $msg['icon'] = 2;
        }
        $this->response($msg);
    }

    /**
     * 批量忽略告警信息
     */
    public function ignoreselect(){
        $warning_ids = $this->input->post("warning_ids",true);
        $msg['msg'] = "忽略告警信息成功!";
        $msg['icon'] = 1;
        $ids = explode(",",$warning_ids);
        foreach($ids as $key=>$val){
            if($val){
                $warning_info = $this->warning_model->get_warning_with_objectid($val);
                if($warning_info){
                    $warning_info['status'] = "20";
                    $this->warning_model->update_warning($warning_info);
                }
            }
        }
        $this->response($msg);
    }
}