<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 14-5-20
 * Time: 下午4:27
 * To change this template use File | Settings | File Templates.
 */
class Merchant_TaxRebateController extends Ec_Controller_Action
{
    public function preDispatch ()
    {
        $this->tplDirectory = "merchant/views/tax/";
        // $this->tplDirectory = "merchant/account_config/";
        $this->customerAuth = new Zend_Session_Namespace("customerAuth");
    }

    public function listAction(){
        $page = $this->_request->getParam('page', 1);
        $pageSize = $this->_request->getParam('pageSize', 20);

        $page = $page ? $page : 1;
        $pageSize = $pageSize ? $pageSize : 20;

        $taxrebateCode = trim($this->_request->getParam("taxrebate_code",""));
        $uploadTimeStart = $this->_request->getParam("upload_time_start","");
        $uploadTimeEnd = $this->_request->getParam("upload_time_end","");
        $filename = $this->_request->getParam("filename","");
        $sessionData = $this->customerAuth->data;
        $condition = array(
            'taxrebate_code'=>$taxrebateCode,
            'customer_code'=>$sessionData['code'],
            'upload_time_start'=>$uploadTimeStart,
            'upload_time_end'=>$uploadTimeEnd,
            'filename'=>$filename,
        );
        $total = Service_TaxRebate::getByCondition($condition,"count(*)");
        $page = $page>ceil($total/$pageSize)?1:$page;
        $this->view->count = $total;
        $result = Service_TaxRebate::getByCondition($condition,"*",$pageSize, $page,array('tr_id desc'));
        $this->view->condition = $condition;
        $this->view->result = $result;
        $this->view->pageSize	= $pageSize;
        $this->view->page		= $page;
        echo Ec::renderTpl($this->tplDirectory . "tax-rebate-list.tpl", 'noleftlayout');
    }

    public function addAction(){
        //$customer_code = $this->_request->getParam("customer_code","");
        $taxrebate_code = $this->_request->getParam("taxrebate_code","");
        $result = array(
            'state'=>'0',
            'message'=>''
        );
        $row = array();
        $sessionData = $this->customerAuth->data;
        $row['customer_code'] = $sessionData['code'];
        if(empty($taxrebate_code)){
            $result['message'] = '报关单号必须填';
            die(Zend_Json::encode($result));
        }else{
            $existTR = Service_TaxRebate::getByField($taxrebate_code,'taxrebate_code',"*");
            if(!empty($existTR)){
                $result['message'] = '报关单号已存在';
                die(Zend_Json::encode($result));
            }else{
                $row['taxrebate_code'] = $taxrebate_code;
            }
        }
        $imgs	= array();
        $fields	= empty($_FILES) ? '' : array_keys($_FILES);
        $path = APPLICATION_PATH.'/../data/images/taxrebate/'.$row['customer_code'];
        $isDir	= Common_Common::createPath($path);
        if($_FILES['uploadIamgefile']['size']){
            $state	= Common_Common::uploadImg($_FILES, 'uploadIamgefile', $path);
            if ($state['error']) {
                $msg	= strip_tags($state['msg']);

                $result = array(
                    'state'=>'0',
                    'message'=>$msg
                );
                die(Zend_Json::encode($result));
            }
            $row['filename'] = $state['msg']['file_name'];
        }
        $row['upload_time'] = date("Y-m-d H:i:s");
        $row['service_url'] = $_SERVER['SERVER_NAME'];
        $trid = Service_TaxRebate::add($row);
        if($trid){
            $result['state'] = 1;
            $result['message'] = "报关单".$taxrebate_code."上传成功！";
        }else{
            $result['message'] = "报关单".$taxrebate_code."上传失败！";
        }
        die(json_encode($result));
    }

    public function downloadImageAction(){
        $trId = $this->_request->getParam("tr_id","");
        $trRow = Service_TaxRebate::getByField($trId,'tr_id',"*");
        header("Location: http://".$trRow['service_url']."/default/index/download-tax-image/tr_id/" . $trId);
        exit();
    }

    public function taxRebateImageAction(){
        $trId = $this->_request->getParam("tr_id","");
        $trRow = Service_TaxRebate::getByField($trId,'tr_id',"*");
        header("Location: http://".$trRow['service_url']."/default/index/tax-rebate-image/tr_id/" . $trId);
        exit();
    }

    public function deleteImageAction(){
        $trId = $this->_request->getParam("tr_id","");
        $trRow = Service_TaxRebate::getByField($trId,'tr_id',"*");
        $updateRow = array(
            'filename'=>"",
        );
        $id = Service_TaxRebate::update($updateRow,$trId,'tr_id');
        if($id){
            $result = array(
                'state'=>'1',
                'message'=>'删除成功'
            );
        }else{
            $result = array(
                'state'=>'1',
                'message'=>'删除失败'
            );
        }
        die(json_encode($result));
    }

    public function deleteAction(){
        $trId = $this->_request->getParam("tr_id","");
        Service_TaxRebate::delete($trId,"tr_id");
        $result = array(
            'state'=>'1',
            'message'=>'删除成功'
        );
        die(json_encode($result));
    }
}
