<?php

class CountryController extends Controller
{
    public function accessRules()
    {
        return array(

            array('allow',
                'actions' => array('login', 'error', 'logout','newlogin'),
                'users' => array('*'),
            ),

            array('allow',
                'actions' => array('Index','GetList','Create','SaveCountry','Disable'),
                'expression' => '$user->isMember()',
            ),

            array('deny',
                'users' => array('*'),
            ),
        );
    }

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetList()
    {
        $model = new Country();

        $draw   = $_POST['draw'];
        $offset = $_POST['start'];
        $limit  = $_POST['length'];

        $condition = '1=1';

        if (isset($_POST['search']['value']) && $_POST['search']['value'] != "")
        {
            $searchString = $_POST['search']['value'];
            $condition .= " AND (id LIKE '%".$searchString."%' 
            OR country_name LIKE '%".$searchString."%')";
        }

        $count = $model->count(array('condition'=>$condition));

        $model = $model->findAll(array(
            'condition'=>$condition,
            'limit'=>$limit,
            'offset'=>$offset,
            'order'=>'country_name ASC'
        ));

        $data['draw'] = $draw;
        $data['recordsTotal'] = $count;
        $data['recordsFiltered'] = $count;

        $rowData = array();
        $m = $offset;

        foreach ($model as $row)
        {
            $rowData[] = array(
                ++$m,
                $row->country_name, 
                array(
                    'id'=>$row->id,
                    'status'=>$row->isActive
                )
            );
        }

        $data['data'] = $rowData;
        echo json_encode($data);
    }

    public function actionCreate()
    {
        $model = new Country();
        $id = (int) Yii::app()->request->getParam('id');

        if($id>0){
            $model = $model->findByPk($id);
        }

        Yii::app()->clientScript->scriptMap = array(
            '*jquery.js' => false,
            '*jquery.min.js' => false,
            'jquery.min.js' => false,
            '*jquery-ui.js' => false,
            '*jquery-ui.min.js' => false
        );

        $this->render('create', array('model'=>$model), false, true);
    }

    public function actionSaveCountry()
    {
        if(isset($_POST['Country']))
        {
            $id = (int) $_POST['Country']['id'];

            if($id > 0)
            {
                
                $model = Country::model()->findByPk($id);
                $model->attributes = $_POST['Country'];

                $model->updated_by = Yii::app()->user->id;
                $model->updated_datetime = date('Y-m-d H:i:s');
            }
            else
            {
                $model = new Country();
                $model->attributes = $_POST['Country'];

                $model->created_by = Yii::app()->user->id;
                $model->updated_by = Yii::app()->user->id;
                $model->created_datetime = date('Y-m-d H:i:s');
                $model->updated_datetime = date('Y-m-d H:i:s');
            }

            if($model->save())
            {
                $jsonData['status'] = 'success';
                $jsonData['frm'] = $_POST['country-form'];
                $jsonData['id'] = $model->id;

                echo CJSON::encode($jsonData);
            }
            else
            {
                echo CActiveForm::validate($model);
            }
        }
    }

    public function actionDisable()
    {
        $id = Yii::app()->request->getParam('id');
        $status = Yii::app()->request->getParam('status');

        $model = Country::model()->findByPk($id);
        $model->isActive = ($status == "Y") ? "N" : "Y";
        $model->save();

        echo json_encode(array("status"=>"success"));
    }

    
}