<?php
class Financial_ServiceProviderController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "financial/views/servicer/";
        $this->serviceClass = new Service_ServiceProvider();
    }

    public function listAction()
    {

        /*
         * 结算类型
        */
        $settlementType = Common_Type::SettlementType();

        // 状态
        $serviceProviderStatus = Common_Type::ServiceProviderStatus();

        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);
            $condition['sp_code_like'] = $params['sp_code_like'];
            $condition['sp_name_like'] = $params['sp_name_like'];

            $count = $this->serviceClass->getByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields=array(

                    'sp_code',
                    'sp_name',
                    'sp_contact_name',
                    'sp_contact_phone',
                    'sp_address',
                    'sp_settlement_type',
                    'sp_balance',
                    'currency_code',
                    'sp_add_date',
                    'sp_id',
                    'sp_status',
                    'sp_person_user'
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getJoinLeftByCondition($condition,$showFields, $pageSize, $page, array('sp_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
                $return['settlementType'] = $settlementType;
                $return['serviceProviderStatus'] = $serviceProviderStatus;
            }
            die(Zend_Json::encode($return));
        }
        echo Ec::renderTpl($this->tplDirectory . "service_provider_index.tpl", 'layout');
    }

    public function editAction()
    {

        $return = array(
            'state' => 0,
            'message' => array('Fail.')
        );

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            $row = array(

                'sp_id'=>'',
                'sp_code'=>'',
                'sp_name'=>'',
                'sp_contact_name'=>'',
                'sp_contact_phone'=>'',
                'sp_address'=>'',
                'sp_settlement_type'=>'',
                'sp_balance'=>'',
                'currency_code'=>'',
                'sp_status'=>'',
                'sp_person_user'=>'',
                'sp_tax'=>'',
            );

            $row=$this->serviceClass->getEditFields($row, $params);

            $errorArr = $this->serviceClass->validator($params);

            if (!empty($errorArr)) {
                $return = array(
                    'state' => 0,
                    'message'=>$errorArr
                );
                die(Zend_Json::encode($return));
            }

            // 判断单号是否重复
            $result = $this->serviceClass->verifySpCode($row['sp_code'],$row['sp_id']);
            if($result = "0") {
                $return = array(
                    'state' => 0,
                    'message'=> array("服务商代码已存在 请重新输入")
                );
                die(Zend_Json::encode($return));
            }

            //银行帐号
            $bankAccount = array();
            if(isset($params["bk_code"]) && !empty($params["bk_code"])){
                foreach($params["bk_code"] as $key=>$val){
                    $bankAccount[$key]["sba_id"] = $params["sba_id"][$key];
                    $bankAccount[$key]["bk_code"] = $params["bk_code"][$key];
                    $bankAccount[$key]["sba_bank_branch"] = $params["sba_bank_branch"][$key];
                    $bankAccount[$key]["sba_account_name"] = $params["sba_account_name"][$key];
                    $bankAccount[$key]["sba_bank_account"] = $params["sba_bank_account"][$key];
                }
            }
            //服务商产品
            $product = array();

            if(isset($params["spp_code"]) && !empty($params["spp_code"]) && $params["spp_code"] != "" && false){
                foreach($params["spp_code"] as $key=>$val){
                    $product[$key]["spp_id"] = $params["spp_id"][$key];
                    $product[$key]["spp_code"] = $params["spp_code"][$key];
                    $product[$key]["spp_name"] = $params["spp_name"][$key];
                    $product[$key]["sp_sync_type"] = $params["sp_sync_type"][$key];
                }
            }

            //服务商联系人
            $contact = array();
            if(isset($params["contact_name"]) && !empty($params["contact_name"]) && $params["contact_name"] != ""){
                foreach($params["contact_name"] as $key=>$val){
                    $contact[$key]["contact_name"] = $val;
                    $contact[$key]["contact_tel"] = $params["contact_tel"][$key];
                    $contact[$key]["contact_address"] = $params["contact_address"][$key];
                    $contact[$key]["contact_note"] = $params["contact_note"][$key];
                }
            }


            $row['sp_code']=strtoupper($row['sp_code']);
            $return = $this->serviceClass->excuteAddServiceProvider($row, $bankAccount, $product,$contact);
        }

        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, 'sp_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'] = '操作成功';
                }
            }
        }
        die(Zend_Json::encode($result));
    }


    /**
     * 创建服务商
     */
    public function createServiceProviderAction(){

        /*
         * 结算类型
        */
        $settlementType = Common_Type::SettlementType();
        $this->view->settlementType=$settlementType;

        //币种
        $currency = Common_DataCache::getCurrency();
        $this->view->currency=$currency;

        //总行
        $bank = Common_DataCache::getBank();
        $this->view->bank=$bank;

        // 同步类型
        $syncType = Common_Type::SyncType();
        $this->view->syncType=$syncType;

        // 产品状态
        $serviceProductStatus = Common_Type::ServiceProductStatus();
        $this->view->serviceProductStatus=$serviceProductStatus;

        // 状态
        $serviceProviderStatus = Common_Type::ServiceProviderStatus();
        $this->view->serviceProviderStatus=$serviceProviderStatus;

        //用户列表
        $this->view->userList=Service_User::getByCondition(array(),array('user_id','user_name','user_name_en'));


        echo Ec::renderTpl($this->tplDirectory . "service_provider_create.tpl", 'layout');
    }

    /**
     * 服务商信息
     */
    public function getServiceProviderViewAction(){

        $paramId = $this->_request->getParam("serviceProviderId","");

        // 服务商
        $serviceProvider = array();

        // 银行账号
        $bankAccount = array();

        // 银行账号
        $product = array();

        /*
         * 结算类型
        */
        $settlementType = Common_Type::SettlementType();
        $this->view->settlementType=$settlementType;

        //币种
        $currency = Common_DataCache::getCurrency();
        $this->view->currency=$currency;

        //总行
        $bank = Common_DataCache::getBank();
        $this->view->bank=$bank;

        // 同步类型
        $syncType = Common_Type::SyncType();
        $this->view->syncType=$syncType;

        // 产品状态
        $serviceProductStatus = Common_Type::ServiceProductStatus();
        $this->view->serviceProductStatus=$serviceProductStatus;

        // 状态
        $serviceProviderStatus = Common_Type::ServiceProviderStatus();
        $this->view->serviceProviderStatus=$serviceProviderStatus;

        if(!empty($paramId)){

            $serviceProvider = $this->serviceClass->getByCondition(array("sp_id"=>$paramId,"*",0,1,""));
            $serviceProvider = $serviceProvider[0];

            $bankAccount = Service_ServiceBankAccount::getByCondition(array("sp_id"=>$paramId,"*",0,1,""));


            $product = Service_ServiceProduct::getByCondition(array("sp_id"=>$paramId,"*",0,1,""));

            //联系人
            $serviceContact = Service_ServiceContact::getByCondition(array("sp_id"=>$paramId),"*",0,1,"");
            $this->view->serviceContact=$serviceContact;
            //用户列表
            $this->view->userList=Service_User::getByCondition(array(),array('user_id','user_name','user_name_en'));

        }

        $this->view->serviceProvider=$serviceProvider;

        $this->view->bankAccount=$bankAccount;

        $this->view->product=$product;

        echo Ec::renderTpl($this->tplDirectory . "service_provider_view.tpl", 'layout');
    }

    /**
     * 编辑服务商
     */
    public function editServiceProviderAction(){

        $paramId = $this->_request->getParam("serviceProviderId","");

        // 服务商
        $serviceProvider = array();

        // 银行账号
        $bankAccount = array();

        // 银行账号
        $product = array();

        /*
         * 结算类型
        */
        $settlementType = Common_Type::SettlementType();
        $this->view->settlementType=$settlementType;

        //币种
        $currency = Common_DataCache::getCurrency();
        $this->view->currency=$currency;

        //总行
        $bank = Common_DataCache::getBank();
        $this->view->bank=$bank;

        // 同步类型
        $syncType = Common_Type::SyncType();
        $this->view->syncType=$syncType;

        // 产品状态
        $serviceProductStatus = Common_Type::ServiceProductStatus();
        $this->view->serviceProductStatus=$serviceProductStatus;

        // 状态
        $serviceProviderStatus = Common_Type::ServiceProviderStatus();
        $this->view->serviceProviderStatus=$serviceProviderStatus;

        if(!empty($paramId)){

            $serviceProvider = $this->serviceClass->getByCondition(array("sp_id"=>$paramId),"*",0,1,"");
            $serviceProvider = $serviceProvider[0];

            $bankAccount = Service_ServiceBankAccount::getByCondition(array("sp_id"=>$paramId),"*",0,1,"");


            $product = Service_ServiceProduct::getByCondition(array("sp_id"=>$paramId),"*",0,1,"");


            //联系人
            $serviceContact = Service_ServiceContact::getByCondition(array("sp_id"=>$paramId),"*",0,1,"");
            $this->view->serviceContact=$serviceContact;

            //用户列表
            $this->view->userList=Service_User::getByCondition(array(),array('user_id','user_name','user_name_en'));


        }

        $this->view->serviceProvider=$serviceProvider;

        $this->view->bankAccount=$bankAccount;

        $this->view->product=$product;

        echo Ec::renderTpl($this->tplDirectory . "service_provider_edit.tpl", 'layout');
    }

    /**
     * 验证单号是否重复
     */
    public function verifySpCodeAction() {
        $result = array(
            "state" => 1,
            "message" => ""
        );
        // 获取服务商ID, 代码
        $serviceProviderId = $this->_request->getParam("serviceProviderId","");
        $spCode = $this->_request->getParam("sp_code","");

        // 判断单号是否重复
        $results = $this->serviceClass->verifySpCode($spCode, $serviceProviderId);
        if($results=='0'){
            $result = array(
                "state" => 0,
                "message" => "服务商代码已存在，请重新输入!"
            );
        }

        die(Zend_Json::encode($result));
    }
}