<?php
/**
 * 支付方式
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class PaymentController extends BaseController{
	public function index(){
		$map=array();
        $map['is_system']=0;
        $list = $this->lists('Payment',$map);
        $status=array();$paylist=array('mm','alipay','weixin','weixinapp','weixinsmall','weixinpay','chinaumspos');
        foreach($paylist as $val){
			$status[$val]=0;
        }
        foreach($list as $v){
            $code=strtolower($v['code']);
            foreach($paylist as $val){
                if(str_exists($code,$val))$status[$val]=$v['status'];
            }
        }
        $this->assign('status', $status);
        $this->display();
    }
	public function _before_edit(){
        $id = I('get.id');
        if (empty($id)) $this->error(L('PARAM_ERR'));
    }
	public function quickedit(){
        $name = I('param.name');        
        if (empty($name)) $this->error(L('PARAM_ERR'));
        if(IS_POST){
            $postdata=I('post.');
            if(empty($postdata['config_account'])){
                $this->error(L('PARAM_ERR'));
            }
            $map=array();
            $map['code'][]=array('like','%'.$name.'%');
            if($name=='weixin')$map['code'][]=array('notin','weixinapp,weixinsmall');

            $mod=M('Payment');
            $data=$mod->create();
            unset($data['name']);
            $data['status']=1;
            if($name=='chinaumspos'){
                $map=array();
                $map['code'] = $name;
                $pay_count = M('Payment')->where($map)->count();
                if($pay_count>0){
                    $result=$mod->where($map)->save($data);
                }else{
                    $data['code']= $name;
                    $data['is_wap']=0;
                    $data['name']='银联支付';
                    $result=$mod->where($map)->add($data);
                }
            }else{
                $result=$mod->where($map)->save($data);
            }
            if($result!==false){
                $this->success(L('EDIT_OK'));
            }else{
                $this->error(L('EDIT_ERROR'));
            }
        }else{
            $map=array();
            $map['code'][]=array('like','%'.$name.'%');
            if($name=='weixin')$map['code'][]=array('notin','weixinapp,weixinsmall');
            $map['status']=1;
            $info=M('Payment')->where($map)->field(true)->find();

            $this->assign($info);
            $this->assign('name',$name);
            $this->display();
        }
    }
    public function status(){
        $code=I('get.id');
        $map=array();
        $map['code'][]=array('like','%'.$code.'%');
        if($code=='weixin')$map['code'][]=array('notin','weixinapp,weixinsmall');
        $data=array();
        $oldstatus=M('Payment')->where($map)->getField('status');
        $data['status']=($oldstatus+1)%2;
        $res=M('Payment')->where($map)->save($data);
		$this->ajaxReturn($data['status']);
    }
}
?>