<?php

class Default_SystemController extends Ec_Controller_DefaultAction
{
    public function preDispatch()
    {
        $this->tplDirectory = "default/views/default/";
        $this->_userAuth = new Zend_Session_Namespace('userAuthorization');
        $action = strtolower($this->getRequest()->getParam('action'));
        $validAction = array('view-product-img', 'get-customer-logo'); // 合法的ACTION，无需验证权限
		
        if (!in_array($action, $validAction, TRUE) && !$this->_userAuth->isLogin) { // 查看产品图片无需登录验证
            $this->_redirect('/default/index/login');
            exit();
        }
    }

    /*
     * 布局头部
     */
    public function headerAction()
    {
        $userId = isset($this->_userAuth->userId) ? $this->_userAuth->userId : '-1';
        $this->view->userId = $userId;
        $warehouseId = $this->_userAuth->warehouseId;
        $warehouseRow = Service_Warehouse::getByField($warehouseId,"warehouse_id","*");
        $this->view->warehouse_code = $warehouseRow['warehouse_code'];
        $this->view->user_code = $this->_userAuth->userCode;
        echo $this->view->render($this->tplDirectory . 'header.tpl');

    }

    /*
     * 后台左导航
     */
    public function leftMenuAction()
    {
        $result = array('state' => 0, 'data' => array());
        $userMenu = isset($this->_userAuth->Acl['menuArr']) ? $this->_userAuth->Acl['menuArr'] : array();
        if ($this->_request->isPost()) {
            if (count($userMenu)) {
                $result = array('state' => 1, 'data' => $userMenu);
            }
            die(Zend_Json::encode($result));
        }
        $this->view->menu = $userMenu;
		$userAuth = new Zend_Session_Namespace('userAuthorization');
		$warehouse = $userAuth->warehouse;        
        $this->view->user = $userAuth->user;
		$this->view->warehouse_code = $warehouse['warehouse_code'];			
        echo $this->view->render($this->tplDirectory . 'left_menu.tpl');
    }

    public function homeAction()
    {
        echo Ec::renderTpl($this->tplDirectory . "home.tpl", 'layout');
    }
    
    public function rightGuildAction()
    {
        echo $this->view->render($this->tplDirectory . "right_guild.tpl");
    }

    /**
     * @查看产品图片
     * @desc WMS 全站使用
     */
    public function viewProductImgAction()
    {
        $productId = $this->_request->getParam('id', "");
        if ($productId) {
            $productAttach = Service_ProductAttached::getByField($productId, 'product_id');
            if ($productAttach) {
                if ($productAttach['pa_file_type'] == 'link') {
                    header("Location: " . $productAttach ['pa_path']);
                    exit ();
                }
                if ($productAttach['pa_file_type'] == 'img') {
                    header("Location: http://develop-oms.ez-wms.com/product-image/view-attach/productId/" . $productId);
                    exit ();
                }
            }
        }
        header("Location: /images/base/noimg.jpg");
        exit ();
    }

    public function getSearchFilterAction()
    {
        $result = array('state' => 0, 'message' => 'Fail', 'data' => array());
        $actionId = $this->_request->getParam('quick', '-1');

        $actionId = empty($actionId) ? '-1' : $actionId;
        if (!empty($actionId)) {
            $condition = array(
                "filter_action_id" => $actionId
            );
            $rows = Service_SearchFilter::getByCondition($condition, '*', 50, 1, array('parent_id asc', 'search_sort asc'));
           
		    if (!empty($rows)) {
                $sType = $sFilter = array();
                foreach ($rows as $key => $value) {
                    if ($value['parent_id'] == '0') {
                        $sType[$value['search_sort'] . '_' . $value['sf_id']] = $value;
                        $sFilter[$value['sf_id']] = $value;
                    } elseif (isset($sFilter[$value['parent_id']])) {
                        $sType[$sFilter[$value['parent_id']]['search_sort'] . '_' . $value['parent_id']]['item'][] = $value;
                    }
                }
                ksort($sType);
                $result = array('state' => 1, 'message' => '', 'data' => $sType);
            }
        }
        die(Zend_Json::encode($result));
    }

    /**
     * @desc 获取自定义导航
     */
    public function getSkyeQuiKeyAction()
    {
        $result = array('state' => 1, 'message' => 'Fail', 'data' => array(), 'userId' => 0);
        $data = Service_UserRightHeaderMap::getSkyeQuiKey();
        if (!empty($data)) {
            $userId = isset($this->_userAuth->userId) ? $this->_userAuth->userId : '0';
            $result = array('state' => 1, 'userId' => $userId, 'data' => $data);
        }
		
        $warehouseId = $this->_userAuth->warehouseId;
        $warehouseRow = Service_Warehouse::getByField($warehouseId,"warehouse_id","*");
        $result['warehouse_code'] = $warehouseRow['warehouse_code'];
        $result['user_code'] = $this->_userAuth->userCode;
        die(Zend_Json::encode($result));
    }
    
    public function getExceptionDataAction() {
        $result = array('state' => 1, 'unreadMsg' => 0);
        $userId = isset($this->_userAuth->userId) ? $this->_userAuth->userId : '0';
        $result['unreadMsg']  =   Service_ExceptionUser::getByCondition(array(
            'user_id'   => $userId,
            'eu_status' =>   0
        ),'count(*)');        
        die(Zend_Json::encode($result));
    }
    /**
     * @desc 获取用户权限
     * @desc 设置快捷导航
     */
    public function getSkyeQuiKeyListAction()
    {
        $result = array('state' => 0, 'message' => '', 'data' => array());
        $userId = isset($this->_userAuth->userId) ? $this->_userAuth->userId : '0';
        $data = Service_User::getUserRightCustomByUserId($userId);
        if (!empty($data)) {
            $result = array('state' => 1, 'data' => $data);
        }
        die(Zend_Json::encode($result));
    }

    public function updateSkyeQuiKeyAction()
    {
        $result = array('state' => 0, 'message' => '操作失败.', 'data' => array());
        if ($this->_request->isPost()) {
            $actionIdArr = $this->_request->getParam('actionId', array());
            $result = Service_UserRightHeaderMap::updateSkyeQuiKey($actionIdArr);
            if ($result) {
                $result = array('state' => 1);
            }
        }
        die(Zend_Json::encode($result));
    }

    /**
     * @desc 返回运输方式代码
     * @use 打印机设置
     */
    public function setupPrinterAction()
    {
        $result = array('state' =>1, 'data' => array('A4'));
        if ($this->_request->isPost()) {
            $smArr = Service_ShippingMethod::getShippingMethodSimple();
            if (!empty($smArr)) {
                $result = array('state' => 1, 'data' => $smArr);
            }
        }
        die(Zend_Json::encode($result));
    }

    /**
     * @系统数据看板
     */
    public function getBoardAction()
    {
        $result = array('state' => 0, 'data' => array());
        $type = trim($this->_request->getParam('type', ''));
        $model = trim($this->_request->getParam('model', ''));
        switch ($model . '_' . $type) {
            case 'shipment_0':
                $result['state'] = 1;
                $result['data']['name'] = array('日期', '当日总订单量', '等待拣货', '拣货完成', '打包完成', '已出仓库', '拣货缺失', '操作完成率');
                $result['data']['value'][] = array('2013-07-04', '3523', '143', '48', '210', '3120', '2', '88.6%');
                $result['data']['value'][] = array('2013-07-03', '2980', '156', '120', '256', '2230', '2', '84%');
                $result['data']['value'][] = array('2013-07-02', '1798', '147', '100', '56', '1480', '2', '89%');
                break;
            case 'shipment_1':
                $result['state'] = 1;
                $result['data']['name'] = array('到货SKU', '等待QC', 'QC异常', '等待上架', '已上架', '操作完成率');
                $result['data']['value'][] = array('120', '20', '3', '95', '2', '2%');
                break;
            case 'picking_0':
                $result['state'] = 1;
                $result['data']['name'] = array('', '已完成', '未完成');
                $result['data']['value'][] = array('订单量', '1830', '230');
                $result['data']['value'][] = array('SKU数量', '9054', '212');

                $result['data']['item'][] = array('姓名', '已拣货订单量', '已拣货产品', '耗时（小时）');
                $result['data']['item'][] = array('jason', '131', '120', '5');
                $result['data']['item'][] = array('丁睿', '58', '102', '5');

                $result['data']['elapsedTime'] = "预计全部拣选完成时间是 20:00";

                break;
            case 'picking_1':
                $result['state'] = 1;
                $result['data']['name'] = array('', '已完成', '未完成');
                $result['data']['value'][] = array('订单量', 112, 220);
                $result['data']['value'][] = array('SKU数量', 110, 230);

                $result['data']['item'][] = array('姓名', '已包装订单', '已包装SKU', '耗时（小时）');
                $result['data']['item'][] = array('keith', '100', '180', '5');
                $result['data']['item'][] = array('张为', '100', '150', '5');

                $result['data']['elapsedTime'] = "预计全部包装完成时间是 10:00";
                break;
            case 'picking_2':
                $result['state'] = 1;
                $result['data']['name'] = array('', '已完成', '未完成');
                $result['data']['value'][] = array('SKU数量', 120, 210);

                $result['data']['item'][] = array('姓名', '已QC产品', '耗时（小时）');
                $result['data']['item'][] = array('kim', '140', '5');
                $result['data']['item'][] = array('殷剑辉', '130', '5');

                $result['data']['elapsedTime'] = "预计全部QC完成时间是 19:00";
                break;
            default:
                $result = array();
                break;
        }
        die(Zend_Json::encode($result));
    }
	/**
	 * @author william-fan
	 * @todo 获取客户logo图片
	 */
    public function getCustomerLogoAction(){
    	$customerCode = $this->_request->getParam('customerCode', "");
    	if ($customerCode) {
    		$customer = Service_Customer::getByField($customerCode, 'customer_code');
			if($customer && $customer['customer_logo']!='' && $customer['customer_logo_save_site']!=''){
				header("Location: ".$customer['customer_logo_save_site'].'/'.'index/view-logo-image/customerCode/'.$customer['customer_code']);
				exit ();
			}
    	}
    	exit();
    	//header("Location: /images/base/noimg.jpg");
    }

}