<?php
class Common_TypeCategoryController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "common/views/type/";
        $this->serviceClass = new Service_TypeCategory();
    }

    public function listAction()
    {
        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(
                    
                'tc_code',
                'tc_description',
                'tc_description_en',
                'tc_update_time',
                'tc_id',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition,$showFields, $pageSize, $page, array('tc_id desc'));
                if(!empty($rows)){
                    $wh=Common_DataCache::getWarehouse();
                    $display=Common_Type::display('auto');
                  $valueObj= new Service_TypeCategoryValue();
                    foreach($rows as $key =>$val){
                        $rows[$key]['E002']=$valueObj->getByCondition(array('tc_id'=>$val['E0']),'*',0,0,array('tcv_sort asc'));
                        if(!empty($rows[$key]['E002'])){
                            foreach($rows[$key]['E002'] as $k =>$v){
                                $rows[$key]['E002'][$k]['warehouse']=isset($wh[$v['warehouse_id']])?$wh[$v['warehouse_id']]['warehouse_code']:'All';
                                $rows[$key]['E002'][$k]['display']=isset($display[$v['tcv_display']])?$display[$v['tcv_display']]:'';
                            }
                        }
                    }
                }

                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $this->view->display=Common_Type::display('auto');
        echo Ec::renderTpl($this->tplDirectory . "type_category_index.tpl", 'layout');
    }

    public function editAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'errorMessage'=>array('Fail.')
        );

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            $row = array(
                
              'tc_id'=>'',
              'tc_code'=>'',
              'tc_description'=>'',
              'tc_description_en'=>'',
            );
            $row=$this->serviceClass->getMatchEditFields($params,$row);
            $paramId = $row['tc_id'];
            if (!empty($row['tc_id'])) {
                unset($row['tc_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, 'tc_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));
    }
}