<?php
class MasinvrackController extends Controller
{
    public function accessRules()
    {
        return array(
            array('allow',
                'actions' => array('login', 'error', 'logout', 'newlogin'),
                'users' => array('*'),
            ),
            array('allow',
                'actions' => array('index', 'GetList', 'Create', 'SaveRack', 'Disable'),
                'expression' => '$user->isMember()',
            ),
            array('deny',
                'users' => array('*'),
            ),
        );
    }

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

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

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

        $criteria = new CDbCriteria();
        $criteria->condition = '1=1';

        if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
            $searchString = $_POST['search']['value'];
            $criteria->addCondition(
                "(id LIKE :search OR rack_name LIKE :search)"
            );
            $criteria->params[':search'] = '%' . $searchString . '%';
        }

        $count = $model->count($criteria);

        $criteria->limit  = $limit;
        $criteria->offset = $offset;
        $criteria->order  = 'rack_name ASC';

        $models = $model->findAll($criteria);

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

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

        foreach ($models as $row) {
            $status = $row->isActive == 'Y' ? 'Active' : 'Inactive';
            $rowData[] = array(
                ++$m,
                $row->rack_name,
                $status,
                array(
                    'id'         => $row->id,
                    'status'     => $row->isActive,
                    'is_default' => $row->is_base_rack
                )
            );
        }

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

    public function actionCreate()
    {
        $model = new MasInvRack();
        $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 actionSaveRack()
    {
        if (isset($_POST['MasInvRack'])) {
            $id = (int) $_POST['MasInvRack']['id'];

            if (empty(trim($_POST['MasInvRack']['rack_name']))) {
                echo CJSON::encode(array('status' => 'error', 'message' => 'Rack Name should not be empty.'));
                return;
            }
            if (empty($_POST['MasInvRack']['isActive'])) {
                echo CJSON::encode(array('status' => 'error', 'message' => 'Status should not be empty.'));
                return;
            }

            if ($id > 0) {
                $model = MasInvRack::model()->findByPk($id);
                $model->attributes       = $_POST['MasInvRack'];
                $model->updated_by       = Yii::app()->user->id;
                $model->updated_datetime = date('Y-m-d H:i:s');
            } else {
                $model = new MasInvRack();
                $model->attributes       = $_POST['MasInvRack'];
                $model->total_bins       = 0;
                $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->is_base_rack == 'Y') {
                Yii::app()->db->createCommand()
                    ->update('{{mas_inv_rack}}', array('is_base_rack' => 'N'));
                $model->isActive = 'Y'; 
            }

            if ($model->save()) {
                $jsonData['status'] = 'success';
                $jsonData['frm']    = $_POST['rack-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 = MasInvRack::model()->findByPk($id);
        if ($model) {
            
            if ($model->is_base_rack == 'Y' && $status == 'Y') {
                echo json_encode(array("status" => "error", "message" => "Default rack cannot be set as inactive."));
                return;
            }
            $model->isActive         = ($status == "Y") ? "N" : "Y";
            $model->updated_by       = Yii::app()->user->id;
            $model->updated_datetime = date('Y-m-d H:i:s');
            $model->save();
        }

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