<?php
/**
 * 推荐设置控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Admin\Controller;
class ExtensionSettingController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        $is_auth=check_use_auth("NationalCustomers");
        if($is_auth['status']==0){
            $this->error('该版本没有此权限',$is_auth['url']);return;
        }
    }
    /**
     * 读取推荐设置的数据
     */
    public function index()
    {
        // $res = M('Config')->field('value')->where("name='AFFILIATE'")->find();
        // $val = unserialize($res['value']);
        
        // $memberrank = M('MemberRank')->where('status = 1')->field('id,name,min_points,max_points')->select();
        // $this->assign('rank', $memberrank);
        // $this->assign('val', $val);
        // $this->assign('item', $val['item']);
        // $this->display();
    
        $level=M('MemberRank')->where('status=1')->select();
        foreach ($level as $key => $value) {
            $new[$value['id']]=$value['name'];
        }
        $res = M('Config')->field('value')->where("name='AFFILIATE'")->find();
        $val = unserialize($res['value']);

            foreach ($val['item'] as $k => $v) {
                
                if($k==1){
                    $val['item']['level_money1']=$v['level_money'];
                }
                if($k==2){
                    $val['item']['level_money2']=$v['level_money'];
                    $val['item']['level_money_type_2']=$v['level_money_type'];
                }
                if($k==3){
                    $val['item']['level_money3']=$v['level_money'];
                    $val['item']['level_money_type_3']=$v['level_money_type'];
                }
            }
            foreach ($val['pro'] as $k => $v) {
                
                if($k==1){
                    $val['item']['pro_level_money1']=$v['pro_level_money'];
                }
                if($k==2){
                    $val['item']['pro_level_money2']=$v['pro_level_money'];
                    // $val['item']['pro_level_money_type_2']=$v['pro_level_money_type'];
                }
                if($k==3){
                    $val['item']['pro_level_money3']=$v['pro_level_money'];
                    // $val['item']['pro_level_money_type_3']=$v['pro_level_money_type'];
                }
            }
        
         // print_r($val);//exit;
        $this->assign('val', $val);
        $this->assign('level', $new);
        $this->assign($val['item']);
        $this->assign($val['config']);
        $this->assign($val['pro']);
        $this->display();
    }
    public function index_old()
    {
        $res = M('Config')->field('value')->where("name='AFFILIATE'")->find();
        $val = unserialize($res['value']);
        
        $memberrank = M('MemberRank')->where('status = 1')->field('id,name,min_points,max_points')->select();
        $this->assign('rank', $memberrank);
        $this->assign('val', $val);
        $this->assign('item', $val['item']);
        $this->display();
    }
    /**
     * 读取推荐设置的数据
     */
    public function setting(){
        $pro_level = M('Config')->field('value')->where("name='PRO_LEVEL'")->find(); 
        $pro_level = unserialize($pro_level['value']);
    
        $this->assign('pro_level',$pro_level);
        $this->assign($pro_level['item']);
        $res = M('Config')->field('value')->where("name='AFFILIATE'")->find();
        $val = unserialize($res['value']);
        $ex_level_sort = idtoname($val['config']['ex_level'],'MemberRank','sort');

        $map = array();
        $map['status']=1;
        $map['sort']=array('egt',$ex_level_sort);
        $memberrank = M('MemberRank')->where($map)->select();
        $this->assign('memberrank',$memberrank);
        foreach ($pro_level as $key => $value) {
            foreach ($memberrank as $k => $v) {
                if($key==$v['id']){
                    $memberrank[$k]['info']=$value;
                }
            }

        }
        foreach ($memberrank as $key => $value) {
            $pro = unserialize($value['level_pro']);
             if($pro['pro_type']==1){
                $buy_info[$value['id']]=$value['name'];

             }else{
                $nobuy_info[$value['id']]=$value['name'];

             }
             $new[$value['id']]=$value['name'];
        }
		//jxr 2019.10.10 start
		$where['name'] = 'PRO_GOODS';
    	$res_val = M('Config')->where($where)->getField('value');
    	$info=unserialize($res_val);
    	$this->assign('pro_type',$info['config']);
		
		
		$pro_val = M('Config')->where("name='PRO_SETTING'")->getField('value');
    	$pro_info=unserialize($pro_val);
    	$this->assign($pro_info['config']);
		
		//jxr 2019.10.10 end
        $this->assign('nobuy_info',$nobuy_info);
        $this->assign('buy_info',$buy_info);
        $this->assign('level', $new);
        $this->assign('config', $pro_level['config']);
        $this->assign('item', $val['item']);
        $this->display();
    }
    /**
     * 读取推荐设置的数据
     */
    public function aff_setting(){
        $pro_level = M('Config')->field('value')->where("name='AFFILIATE_THREE'")->find(); 
        $pro_level = unserialize($pro_level['value']);
    
        $this->assign('pro_level',$pro_level);
          $this->assign($pro_level['item']);
        $res = M('Config')->field('value')->where("name='AFFILIATE'")->find();
        $val = unserialize($res['value']);
        $ex_level_sort = idtoname($val['config']['ex_level'],'MemberRank','sort');

        $map = array();
        $map['status']=1;
        $map['sort']=array('egt',$ex_level_sort);
        $memberrank = M('MemberRank')->where($map)->select();
        $this->assign('memberrank',$memberrank);
        foreach ($pro_level as $key => $value) {
            foreach ($memberrank as $k => $v) {
                if($key==$v['id']){
                    $memberrank[$k]['info']=$value;
                }
            }

        }
        foreach ($memberrank as $key => $value) {
            $pro = unserialize($value['level_pro']);
             if($pro['pro_type']==1){
                $buy_info[$value['id']]=$value['name'];

             }else{
                $nobuy_info[$value['id']]=$value['name'];

             }
             $new[$value['id']]=$value['name'];
        }
		
		//jxr 2019.10.10 start
		$pro_val = M('Config')->where("name='AFF_SETTING'")->getField('value');
    	$pro_info=unserialize($pro_val);
    	$this->assign($pro_info['config']);
		//jxr 2019.10.10 end
		
        $this->assign('nobuy_info',$nobuy_info);
        $this->assign('buy_info',$buy_info);
        $this->assign('level', $new);
        $this->assign('config', $pro_level['config']);
        $this->assign('item', $val['item']);
        $this->display();
    }
    public function three_update(){
        $postdata = I('post.');
            // print_r($postdata);exit;
        $res = M('Config')->field('value')->where("name='AFFILIATE'")->find();
        $val = unserialize($res['value']);
        $ex_level_sort = idtoname($val['config']['ex_level'],'MemberRank','sort');
        $map = array();
        $map['status']=1;
        $map['sort']=array('egt',$ex_level_sort);
        $memberrank = M('MemberRank')->where($map)->select();

        foreach ($postdata['zhi_level'] as $k => $v) {
            $new[$k]['zhi_level'] = intval($v);
        }
        foreach ($postdata['jian_level'] as $k => $v) {
            $new[$k]['jian_level'] = intval($v);
        }
        foreach ($postdata['t_level'] as $k => $v) {
            $new[$k]['t_level'] = intval($v);
        }
        foreach ($postdata['fen_money'] as $k => $v) {
            $new[$k]['fen_money'] = $v;
        }
        foreach ($postdata['sfen_money'] as $k => $v) {
            $new[$k]['sfen_money'] = $v;
        }
        foreach ($postdata['tfen_money'] as $k => $v) {
            $new[$k]['tfen_money'] = $v;
        }
        
        $item['fen_type']=$postdata['fen_type'];
        // print_r($new);exit;
        $temp['info'] = $new;
        $temp['item'] = $item;
        $temp = serialize($temp);
        $res = M('Config')->field('value')->where("name='AFFILIATE_THREE'")->find();
        $data['value'] = $temp;
        if (empty($res)) {
            $data['name'] = 'AFFILIATE_THREE';
            $save = M('Config')->data($data)->add();
        } else {
            $save = M('Config')->data($data)->where("name='AFFILIATE_THREE'")->save();
        }
		
		//jxr 2019.10.10 start
		if($postdata['fx_money_type'] || $postdata['money_type']){
			$save=array();
			$save['config']['fx_money_type']=$postdata['fx_money_type'];
			$save['config']['money_type']=$postdata['money_type'];
			$save = serialize($save);
			$data['value']=$save;
			$saves = M('Config')->data($data)->where("name='AFF_SETTING'")->save();
		}
		//jxr 2019.10.10 end
		
        // print_r($save);exit;
        if (!empty($save)) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }
    public function pro_update(){
        $postdata = I('post.');
            
        $res = M('Config')->field('value')->where("name='AFFILIATE'")->find();
        $val = unserialize($res['value']);
        $ex_level_sort = idtoname($val['config']['ex_level'],'MemberRank','sort');
        $map = array();
        $map['status']=1;
        $map['sort']=array('egt',$ex_level_sort);
        $memberrank = M('MemberRank')->where($map)->select();
		//jxr 2019.10.10 start
		foreach ($postdata['one_level'] as $k => $v) {
            $new[$k]['one_level'] = intval($v);
        }
		//jxr 2019.10.10 end
        foreach ($postdata['zhi_level'] as $k => $v) {
            $new[$k]['zhi_level'] = intval($v);
        }
        foreach ($postdata['jian_level'] as $k => $v) {
            $new[$k]['jian_level'] = intval($v);
        }
		//jxr 2019.10.10 start
		foreach ($postdata['san_level'] as $k => $v) {
            $new[$k]['san_level'] = intval($v);
        }
		//jxr 2019.10.10 end
        foreach ($postdata['fen_money'] as $k => $v) {
            $new[$k]['fen_money'] = $v;
        }
        foreach ($postdata['sfen_money'] as $k => $v) {
            $new[$k]['sfen_money'] = $v;
        }
		//jxr 2019.10.10 start
		foreach ($postdata['ssfen_money'] as $k => $v) {
            $new[$k]['ssfen_money'] = $v;
        }
		//jxr 2019.10.10 end
        /* foreach ($new as $k => $v) {
            if($v['jian_level']==0){
                $new[$k]['sfen_money']=0;
            }
			//jxr 2019.10.10 start
			if($v['san_level']==0){
                $new[$k]['ssfen_money']=0;
            }
			//jxr 2019.10.10 end
        } */
        $item['zi_one_level']=$postdata['zi_one_level'];
        $item['zi_jian_level']=$postdata['zi_jian_level'];
        $item['zi_fen_money']=$postdata['zi_fen_money'];
        $item['fen_type']=$postdata['fen_type'];
        // print_r($new);exit;
        $temp['info'] = $new;
        $temp['item'] = $item;
        $temp = serialize($temp);
        $res = M('Config')->field('value')->where("name='PRO_LEVEL'")->find();
        $data['value'] = $temp;
        if (empty($res)) {
            $data['name'] = 'PRO_LEVEL';
            $save = M('Config')->data($data)->add();
        } else {
            $save = M('Config')->data($data)->where("name='PRO_LEVEL'")->save();
        }
		//jxr 2019.10.10 start
		if(!empty($postdata['pro_type'])){
			$save=array();
			$res_pro = M('Config')->field('value')->where("name='PRO_GOODS'")->find();
			$info=unserialize($res_pro['value']);
			
			$save['config']=$postdata['pro_type'];
			if(!empty($info['temp'])) $save['temp']= $info['temp'];
			$save = serialize($save);
			$data['value']=$save;
			$saves = M('Config')->data($data)->where("name='PRO_GOODS'")->save();
		}
		if($postdata['fx_money_type'] || $postdata['money_type']){
			$save=array();
			$save['config']['fx_money_type']=$postdata['fx_money_type'];
			$save['config']['money_type']=$postdata['money_type'];
			$save = serialize($save);
			$data['value']=$save;
			$saves = M('Config')->data($data)->where("name='PRO_SETTING'")->save();
		}
		
		//jxr 2019.10.10 end
        // print_r($save);exit;
        if (!empty($save)) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }
    /**
     * 修改推荐设置的数据
     */
    public function update()
    {
        $postdata = I('post.');
        $level_point_money = array();
        $level_point_money[1]['level_money']=$postdata['level_money_1'];
        $level_point_money[2]['level_money']=$postdata['level_money_2'];
        $level_point_money[2]['level_money_type']=$postdata['level_money_type_2'];
        $level_point_money[3]['level_money']=$postdata['level_money_3'];
        $level_point_money[3]['level_money_type']=$postdata['level_money_type_3'];

        $level_pro_money = array();
        $level_pro_money[1]['pro_level_money']=$postdata['pro_level_money_1'];
        $level_pro_money[2]['pro_level_money']=$postdata['pro_level_money_2'];
        // $level_pro_money[2]['pro_level_money_type']=$postdata['pro_level_money_type_2'];
        $level_pro_money[3]['pro_level_money']=$postdata['pro_level_money_3'];
        // $level_pro_money[3]['pro_level_money_type']=$postdata['pro_level_money_type_3'];

        if($postdata['is_level']==1){
            $ex_level=$postdata['ex_level'];
        }else{
            $ex_level=0;
        }
        // if($postdata['is_ngfx']==1){
        //     $is_ngfx_level=$postdata['is_ngfx_level'];
        // }else{
        //     $is_ngfx_level=0;
        // }
        if($postdata['is_weidian']==1){
            $is_weidian_level=$postdata['is_weidian_level'];
        }else{
            $is_weidian_level=0;
        }
        // print_r($level_point_money);exit;
        $temp['config'] = array(  
            'bean' => $postdata['bean'],        //新增粉丝奖励
            'bean_unit' => $postdata['bean_unit'],   //1积分2现金
            'register' => $postdata['register'],        //新增会员注册奖励
            'register_unit' => $postdata['register_unit'],   //1积分2现金
            'get_coupon' => $postdata['get_coupon'],        //微信卡券新用户赠领
            'get_coupon_unit' => $postdata['get_coupon_unit'],   //1积分2现金
            'cancel_coupon' => $postdata['cancel_coupon'],        //赠领卡券核销奖励
            'cancel_coupon_unit' => $postdata['cancel_coupon_unit'],   //1积分2现金
            'update_time'=>NOW_TIME,//更新时间
            'is_dg' => $postdata['is_dg'],  //是否开启导购返佣
            'dg_num' => $postdata['dg_num'],   //导购返佣值
            'is_level' => $postdata['is_level'],  //是否开启等级限定
            'ex_level' => $ex_level,   //推广返利等级限定
            'is_weidian' => $postdata['is_weidian'],  //微店开通  是否可以开通微店卖货
            'is_weidian_level' => $is_weidian_level,   //微店开通等级限制
            'is_ngfx'=>$postdata['is_ngfx'],//是否开启分销内购  
            // 'is_ngfx_level' => $is_ngfx_level,   //内购分销 开启情况下设定等级限制
            'fx_money_type' => $postdata['fx_money_type'],  //分销金额类型 订单金额 利润
            'fx_type'=>$postdata['fx_type'],//分销类型 按固定比例分 按下级获得佣金的 百分比
            'money_type' => $postdata['money_type'],  //分销金额类型 订单金额 利润
            'lshopview_img' => $postdata['lshopview_img'],  //小店分享图
        );
        $temp['pro'] = $level_pro_money;
        $temp['item'] = $level_point_money;
        $temp = serialize($temp);
        $res = M('Config')->field('value')->where("name='AFFILIATE'")->find();
        $data['value'] = $temp;
        if (empty($res)) {
            $data['name'] = 'AFFILIATE';
            $save = M('Config')->data($data)->add();
        } else {
            $save = M('Config')->data($data)->where("name='AFFILIATE'")->save();
        }
        if (!empty($save)) {
            $this->success(L('EXTENSION_SETTING_SUCCESS'));
        } else {
            $this->error(L('EXTENSION_SETTING_FAILED'));
        }
    }

    /**
     * 修改推荐设置的数据
     */
    // public function update()
    // {
    //     $postdata = I('post.');
    //     // dump($postdata);exit();
    //     $level_point_money = array();
    //     foreach ($postdata['point'] as $key => $v) {
    //         if (empty($v) && empty($postdata['money'][$key])) continue;
    //         $level_point_money[$key]['point'] = $v;
    //         $level_point_money[$key]['point_status'] = $postdata['point_status'][$key];
    //         $level_point_money[$key]['money'] = $postdata['money'][$key];
    //         $level_point_money[$key]['money_status'] = $postdata['money_status'][$key];
    //     }
    //     $temp['config'] = array(
    //         'bean' => $postdata['bean'],        //新增粉丝奖励
    //         'bean_unit' => $postdata['bean_unit'],   //1积分2现金
    //         'register' => $postdata['register'],        //新增会员注册奖励
    //         'register_unit' => $postdata['register_unit'],   //1积分2现金
    //         'get_coupon' => $postdata['get_coupon'],        //微信卡券新用户赠领
    //         'get_coupon_unit' => $postdata['get_coupon_unit'],   //1积分2现金
    //         'cancel_coupon' => $postdata['cancel_coupon'],        //赠领卡券核销奖励
    //         'cancel_coupon_unit' => $postdata['cancel_coupon_unit'],   //1积分2现金
    //         'memberrank' => $postdata['memberrank'],   //分佣1普通会员2VIP会员3黄金VIP4钻石VIP
    //     );
    //     $temp['item'] = $level_point_money;
    //     $temp = serialize($temp);
    //     $where['name'] = 'AFFILIATE';
    //     $res = M('Config')->field('value')->where($where)->find();
    //     $data['value'] = $temp;
    //     if (empty($res)) {
    //         $data['name'] = 'AFFILIATE';
    //         $save = M('Config')->data($data)->add();
    //     } else {
    //         $save = M('Config')->data($data)->where($where)->save();
    //     }
    //     if (!empty($save) || $save == 0) {
    //         $this->success('修改拓客奖励成功',U('Admin/extension_setting/index'));
    //     } else {
    //         $this->error('修改拓客奖励失败',U('Admin/extension_setting/index'));
    //     }
    // }
}
?>