<?php
class Common_PackageController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "common/views/package/";
        $this->serviceClass = new Service_ProductPackage();
    }

    public function listAction()
    {
       $packageType= Common_Type::packageType();
        $wh=Common_DataCache::getWarehouseSimple();
        if ($this->_request->isPost()) {
            $page = $this->_request->getParam('page', 1);
            $pageSize = $this->_request->getParam('pageSize', 20);

            $page = $page ? $page : 1;
            $pageSize = $pageSize ? $pageSize : 20;

            $return = array(
                "state" => 0,
                "message" => "No Data"
            );

            $params = $this->_request->getParams();

            $condition = $this->serviceClass->getMatchFields($params);

            $count = $this->serviceClass->getByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields=array(
                    
                'warehouse_id',
                'customer_code',
                'pp_barcode',
                'pp_type',
                'pp_status',
                'pp_quantity',
                'pp_cost',
                'pp_price',
                'currency_code',
                'pp_name',
                'pp_weight',
                'pp_update_time',
                'pp_id',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition,$showFields, $pageSize, $page, array('pp_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['packageType'] = $packageType;
                $return['warehouse'] = $wh;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $this->view->packageType=$packageType;
        echo Ec::renderTpl($this->tplDirectory . "package_index.tpl", 'layout');
    }

    public function editAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'errorMessage'=>array('Fail.')
        );

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            $row = array(
                
              'pp_id'=>'',
              'warehouse_id'=>'',
              'customer_code'=>'',
              'pp_barcode'=>'',
              'pp_type'=>'',
              'pp_status'=>'',
              'pp_quantity'=>'',
              'pp_cost'=>'',
              'pp_price'=>'',
              'currency_code'=>'',
              'pp_name_en'=>'',
              'pp_name'=>'',
              'pp_length'=>'',
              'pp_width'=>'',
              'pp_height'=>'',
              'pp_weight'=>'',
            );
            $row=$this->serviceClass->getMatchEditFields($params,$row);
            $paramId = $row['pp_id'];
            if (!empty($row['pp_id'])) {
                unset($row['pp_id']);
            }
            $errorArr = $this->serviceClass->validator($row);

            if (!empty($errorArr)) {
                $return = array(
                    'state' => 0,
                    'message'=>'',
                    'errorMessage' => $errorArr
                );
                die(Zend_Json::encode($return));
            }

            if (!empty($paramId)) {
                $result = $this->serviceClass->update($row, $paramId);
            } else {
                $result = $this->serviceClass->add($row);
            }
            if ($result) {
                $return['state'] = 1;
                $return['message'] = array('Success.');
            }

        }
        die(Zend_Json::encode($return));
    }

    public function getByJsonAction()
    {
        $result = array('state' => 0, 'message' => 'Fail', 'data' => array());
        $paramId = $this->_request->getParam('paramId', '');
        if (!empty($paramId) && $rows = $this->serviceClass->getByField($paramId, 'pp_id')) {
            $rows=$this->serviceClass->getVirtualFields($rows);
            $result = array('state' => 1, 'message' => '', 'data' => $rows);
        }
        die(Zend_Json::encode($result));
    }

    public function deleteAction()
    {
        $result = array(
            "state" => 0,
            "message" => "Fail."
        );
        if ($this->_request->isPost()) {
            $paramId = $this->_request->getPost('paramId');
            if (!empty($paramId)) {
                if ($this->serviceClass->delete($paramId)) {
                    $result['state'] = 1;
                    $result['message'] = 'Success.';
                }
            }
        }
        die(Zend_Json::encode($result));
    }
}