<?php

namespace Admin\Controller;
use Think\Controller;
class PaymentController extends CommonController {
    
    public function index() {
        $M = M("Pay");
        $count = $M->count();
        $pConf = page($count,C('PAGE_SIZE'));
        $this->page = $pConf['show']; 

        $this->order = D("Payment")->payList($pConf['first'], $pConf['list']);
        C('TOKEN_ON',false);
        $this->display();
    }
    
    public function del() {
        $where=array('out_trade_no'=>I('get.order_no'));
        if (M("Pay")->where($where)->delete()) {
            $this->success("成功删除");
            
        } else {
            $this->error("删除失败，可能是不存在该ID的记录");
        }
    }
    
    public function search(){
        $search = I('get.');
        $this->keys =$search;
        $where = array();
        if($search['status']!='') $where['status'] = $search['status'];
        if(!empty($search['start_time']) && !empty($search['end_time'])){
            $where['update_time'] = array(array('egt',strtotime($search['start_time'])),array('elt',strtotime($search['end_time'])), 'and');
        }elseif(!empty($search['start_time'])){
            $where['update_time'] = array('elt',strtotime($search['end_time']));
        }elseif(!empty($search['end_time'])){
            $where['update_time'] = array('egt',strtotime($search['start_time']));
        }
        $M = M("Pay");
        $count = $M->where($where)->count();
        $pConf = page($count,C('PAGE_SIZE'));
        $this->page = $pConf['show']; 
        $this->order = D("Payment")->payList($pConf['first'], $pConf['list'],$where);
        C('TOKEN_ON',false);
        $this->display('index');
    }
    
    public function pay_gallery(){
        $payment = include APP_PATH . 'Common/Conf/payment.php'; 
        $this->assign("pay", $payment);
        $this->display(); 
    }
    
    public function edit(){
        if (IS_POST) {
            $payName = I('post.type');
            $setPay = I('post.');
            $this->checkToken();
            $payment = APP_PATH . "Common/Conf/payment.php";
            $payment = file_exists($payment) ? include "$payment" : array();
            $payment = is_array($payment) ? $payment : array();
            $payment = array_merge($payment, array($payName=>$setPay[$payName]));
            if (set_config("payment", $payment, APP_PATH . "Common/Conf/")) {
                delDirAndFile(WEB_CACHE_PATH . "Cache/Admin/");
                echo json_encode(array('status' => 1, 'info' => $str . '已更新', 'url' =>U('Payment/pay_gallery')));
            } else {
                echo json_encode(array('status' => 0, 'info' => $str . '失败，请检查', 'url' => __ACTION__));
            }
        } else {
            $payment = include APP_PATH . 'Common/Conf/payment.php'; 
            $type=I('get.paytype');
            if(isset($payment[$type])){
                $this->type=$type;
                $this->paycon=$payment[$type];
                $this->display();
            }else{
                E('页面不存在');
            }
        }
        
    }
}?>
