<?php
class Warehouse_AreaController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "warehouse/views/default/";
        $this->serviceClass = new Service_WarehouseArea();
    }
	/*分区管理*/
    public function listAction()
    {
        $areaType=Common_Type::warehouseAreaType('auto');
        $wh=Common_DataCache::getWarehouseSimple();
        $category=Common_DataCache::getProductCategory();
        $status=Common_Type::status('auto');
        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(
                'wa_code',
                'wa_name',
                'wa_name_en',
                'wa_type',
                'pc_id',
                'warehouse_id',
                'wa_status',
                'wa_update_time',
                'wa_id',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition,$showFields, $pageSize, $page, array('wa_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['areaType'] = $areaType;
                $return['status'] = $status;
                $return['warehouse'] = $wh;
                $return['category'] = $category;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $this->view->areaType=$areaType;
        $this->view->category=$category;
        echo Ec::renderTpl($this->tplDirectory . "area_index.tpl", 'layout');
    }

    public function editAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'errorMessage'=>array('Fail.')
        );

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            $row = array(
                
              'wa_id'=>'',
              'wa_code'=>'',
              'wa_name'=>'',
              'wa_name_en'=>'',
              'wa_type'=>'',
              'pc_id'=>'',
              'warehouse_id'=>'',
              'wa_status'=>'',
            );
            $row=$this->serviceClass->getMatchEditFields($params,$row);
            $paramId = $row['wa_id'];
            if (!empty($row['wa_id'])) {
                unset($row['wa_id']);
            }
            $errorArr = $this->serviceClass->validator($row);

            if (!empty($errorArr)) {
                $return = array(
                    'state' => 0,
                    'message'=>'',
                    'errorMessage' => $errorArr
                );
                die(Zend_Json::encode($return));
            }
            //已绑定货架的情况下，不允许删除、修改分区代码
            $row['wa_code']=strtoupper($row['wa_code']);
            if (!empty($paramId)) {
                $result = $this->serviceClass->update($row, $paramId);
            } else {
                $count=$this->serviceClass->getByCondition(array('wa_code'=>$row['wa_code'],'warehouse_id'=>$row['warehouse_id']),'count(*)');
                if($count){
                    $result=false;
                    $return['errorMessage']=array(Ec::Lang('waCode').Ec::Lang('exist'));
                }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, 'wa_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));
    }
}