<?php

class Financial_CustomerBankInfoController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "financial/views/bank_info/";
        $this->serviceClass = new Service_CustomerBankAccount();
    }

    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(
                    'cba_id',
                    'customer_code',
                    'bk_code',
                    'cba_bank_branch',
                    'cba_account_name',
                    'cba_bank_account',
                    'cba_creator_id',
                    'cba_add_time',
                    'cba_update_time',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition, $showFields, $pageSize, $page, array('cba_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $this->view->bankAll = Service_Bank::getAll();
        echo Ec::renderTpl($this->tplDirectory . "bank_info_index.tpl", 'layout');
    }

    public function editAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'errorMessage' => array('Fail.')
        );

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            $row = array(
                'cba_id' => '',
                'customer_code' => '',
                'bk_code' => '',
                'cba_bank_branch' => '',
                'cba_account_name' => '',
                'cba_bank_account' => '',
            );
            $row = $this->serviceClass->getMatchEditFields($params, $row);
            $paramId = $row['cba_id'];
            if (!empty($row['cba_id'])) {
                unset($row['cba_id']);
            }
            $errorArr = $this->serviceClass->validator($row);
            if (!empty($errorArr)) {
                $return = array(
                    'state' => 0,
                    'message' => '',
                    'errorMessage' => $errorArr
                );
                die(Zend_Json::encode($return));
            }
            $condition = array(
                'customer_code'     => $row['customer_code'],
                'customer_status'   => 2
            );
            $objCustomer = new Service_Customer();
            $count = $objCustomer->getByCondition($condition, 'count(*)');
            if ($count) {
                if (!empty($paramId)) {
                    $result = $this->serviceClass->update($row, $paramId);
                } else {
                    $row['cba_creator_id'] = Service_User::getUserId();
                    $result = $this->serviceClass->add($row);
                }
                if ($result) {
                    $return['state'] = 1;
                    $return['message'] = array('操作成功');
                }
            } else {
                $return['errorMessage'] = array('客户代码不存在或客户不处于正常状态');
            }
        }
        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, 'cba_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));
    }

    /**
     * @desc 获取客户帐户信息
     */
    public function getAccountAction()
    {
        $code = $this->_request->getParam('code', '');
        $result = array(
            'state' => '0',
            'message' => '没有找到匹配的信息',
            'data' => array()
        );
        if ($this->_request->isPost()) {
            if (!empty($code)) {
                $condition = array(
                    'customer_code' => $code,
                );
                $rows = Service_CustomerBankAccount::getByCondition($condition, '*', 0);
                if (!empty($rows)) {
                    $result = array(
                        'state' => '1',
                        'message' => '',
                        'data' => $rows
                    );
                }
            }
        }
        die(Zend_Json::encode($result));
    }
}