<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\admin\controller\bbfxshop\fenxiao;

use app\admin\controller\bbfxshop\Base;
use think\Db;

class Level extends Base
{
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Fenxiao;
    }
    
    public function list(){
        $params = $this->param();
        
        
    	$list = \app\common\model\bbfxshop\FenxiaoLevel::getLevels();
    	
    	foreach ($list as $key => &$val){
    	    if($val['id'] == 0){
    	        $val['id'] = 'default';
    	    }
    	    $val['thumb'] = cdnurl($val['thumb']);
            $val['commission1'] = floatval($val['commission1']);
            $val['commission2'] = floatval($val['commission2']);
            $val['commission3'] = floatval($val['commission3']);
            $val['commission1_score'] = floatval($val['commission1_score']);
            $val['commission2_score'] = floatval($val['commission2_score']);
            $val['commission3_score'] = floatval($val['commission3_score']);
            $val['jc_reward'] = floatval($val['jc_reward']);
            $val['jd_reward'] = floatval($val['jd_reward']);
            $val['pj_reward'] = floatval($val['pj_reward']);
            $val['ab_reward'] = floatval($val['ab_reward']);
            $val['repurchase_reward1'] = floatval($val['repurchase_reward1']);
            $val['repurchase_reward2'] = floatval($val['repurchase_reward2']);
            $val['repurchase_reward3'] = floatval($val['repurchase_reward3']);
            $val['management_reward'] = floatval($val['management_reward']);
            $val['management_assistant_reward'] = floatval($val['management_assistant_reward']);
            $val['discount'] = floatval($val['discount']);
            $val['strikeout_multiple'] = floatval($val['strikeout_multiple']);
            $val['jd_pingji_reward'] = floatval($val['jd_pingji_reward']);
            $val['cy_reward'] = floatval($val['cy_reward']);
            $val['npingji_reward'] = floatval($val['npingji_reward']);
    	}
    // 	$this->model->settlement(67);
    
        // \app\common\model\bbfxshop\Agent::settlement(46);
        
      	__success("操作成功",['list'=>$list]);
    }
    
    public function all(){
        $params = $this->param();
        
        
    	$list = \app\common\model\bbfxshop\FenxiaoLevel::getLevels();
    	
    	foreach ($list as $key => &$val){
    	    $val['id'] = (int)$val['id'];
    	    $val['thumb'] = cdnurl($val['thumb']);
    	}
      	
        $abonus_levels = Db::name("bbfx_fenxiao_abonus_rule")->where(['uniacid'=>UNIACID])->field(['id','name'])->order('single_purchase_money desc')->select();
      	
      	__success("操作成功",['list'=>$list,'abonus_levels'=>$abonus_levels]);
    }

    public function copy(){
        $id = $this->param("id/d");
        $level = Db::name("bbfx_fenxiao_level")->where(['id'=>$id])->find();
        if(empty($level)){
            __error("等级不存在");
        }
        unset($level['id']);
        $level['title'] .= "-复制";
        Db::name("bbfx_fenxiao_level")->insert($level);
        __success("操作成功");
    }

    public function detail(){

        $params = $this->param();
        
        $levels = \app\common\model\bbfxshop\FenxiaoLevel::getLevels();
        
        $id = $params['id'];
        $list = [];
        if($id == 'default'){
            $info = $levels[0];
            $info['id'] = $id;
            $info['thumb'] = cdnurl($info['thumb']);
            $fenxiao = Db::name("bbfx_fenxiao")->where(['uniacid'=>UNIACID])->field("id,become_agent")->find();
            
            $default_data = [
                "become_agent"=>intval($fenxiao['become_agent']),
                // "become_order_type"=>intval($fenxiao['become_order_type']),
                // "become_card"=>intval($fenxiao['become_card']),
                // "become_goods"=>trim($fenxiao['become_goods']),
                // "become_purchase_money"=>floatval($fenxiao['become_purchase_money']),
                // "become_cz_balance"=>floatval($fenxiao['become_cz_balance']),
                // "become_team_achievement"=>intval($fenxiao['become_team_achievement']),
            ];
            $info = array_merge($info,$default_data);

            if($info['become_agent'] != 0){
                $info['become_agent'] = 1;
            }

            $conditions = $info['conditions'];
        }
        else{
            $info = Db::name("bbfx_fenxiao_level")->where(['id'=>$id])->find();
            if(empty($info)){
                $info = "";
            }
            else{
                $info['thumb'] = cdnurl($info['thumb']);
                $conditions = (array)iunserializer($info['conditions']);
                $info['pj_reward_layer'] = (array)iunserializer($info['pj_reward_layer']);
                $info['repurchase_pingji_reward'] = (array)iunserializer($info['repurchase_pingji_reward']);
                
            }
            
            if(!empty($info)){
                foreach ($levels as $key => $val){
                    if($val['id'] == $info['id']){
                        break;
                    }
                    $list[] = $val;
                }
                $info['is_npingji'] = intval($info['is_npingji']);
                $info['is_chaoyue'] = intval($info['is_chaoyue']);
                $info['is_jicha'] = intval($info['is_jicha']);
                $info['is_jiandan'] = intval($info['is_jiandan']);
                $info['is_pingji'] = intval($info['is_pingji']); 
                $info['is_abonus'] = intval($info['is_abonus']);
                $info['is_repurchase'] = intval($info['is_repurchase']);
                $info['is_management'] = intval($info['is_management']);
            }
            else{
                $list = $levels;
            }
        }

        if(!empty($info)){

            if(empty($conditions['purchase_zd_goods1'])) $conditions['purchase_zd_goods1'] = [];
            if(empty($conditions['purchase_zd_goods2'])) $conditions['purchase_zd_goods2'] = [];
            if(empty($conditions['team_purchase_zd_goods'])) $conditions['team_purchase_zd_goods'] = [];

            $info['conditions'] = array(
                "become_goods"=>$conditions['become_goods'],
                "level1_distributor"=>intval($conditions['level1_distributor']),
                "level1_team"=>intval($conditions['level1_team']),
                "level1_achievement"=>floatval($conditions['level1_achievement']),
                "level1_order"=>intval($conditions['level1_order']),
                "team_order"=>intval($conditions['team_order']),
                "team_user"=>intval($conditions['team_user']),
                "zhid_level_user"=>$conditions['zhid_level_user'],
                "zt_zhid_level_user"=>$conditions['zt_zhid_level_user'],
                "upgrade_score"=>intval($conditions['upgrade_score']),
                "team_achievement"=>floatval($conditions['team_achievement']),
                "single_purchase_money"=>floatval($conditions['single_purchase_money']),
                "purchase_money"=>floatval($conditions['purchase_money']),
                "apply"=>intval($conditions['apply']),
                "level2_distributor"=>intval($conditions['level2_distributor']),
                "level12_distributor"=>intval($conditions['level12_distributor']),
                "member_card"=>intval($conditions['member_card']),
                "xiaoqu_achievement"=>intval($conditions['xiaoqu_achievement']),
                "yajin_money"=>round($conditions['yajin_money'],2),
                "daqu_achievement"=>intval($conditions['daqu_achievement']),
                "cz_balance"=>intval($conditions['cz_balance']),
                "purchase_zd_goods1"=>$conditions['purchase_zd_goods1'],
                "purchase_zd_goods2"=>$conditions['purchase_zd_goods2'],
                "team_purchase_zd_goods"=>$conditions['team_purchase_zd_goods'],
                "zhitui_purchase_zd_goods"=>$conditions['zhitui_purchase_zd_goods'],
                "purchase_zd_goods1_single"=>intval($conditions['purchase_zd_goods1_single']),
                "purchase_zd_goods2_single"=>intval($conditions['purchase_zd_goods2_single']),
                "team_purchase_zd_goods_single"=>intval($conditions['team_purchase_zd_goods_single']),
                "zhitui_purchase_zd_goods_single"=>intval($conditions['zhitui_purchase_zd_goods_single']),
                "purchase_zd_goods1_total"=>intval($conditions['purchase_zd_goods1_total']),
                "purchase_zd_goods2_total"=>intval($conditions['purchase_zd_goods2_total']),
                "team_purchase_zd_goods_total"=>intval($conditions['team_purchase_zd_goods_total']),
                "zhitui_purchase_zd_goods_total"=>intval($conditions['zhitui_purchase_zd_goods_total']),
                "zhitui_upgrade_score"=>intval($conditions['zhitui_upgrade_score']),
                "team_upgrade_score"=>intval($conditions['team_upgrade_score']),
                "register_time"=>intval($conditions['register_time']),
            );


            $info['commission1'] = floatval($info['commission1']);
            $info['commission2'] = floatval($info['commission2']);
            $info['commission3'] = floatval($info['commission3']);
            $info['commission1_score'] = floatval($info['commission1_score']);
            $info['commission2_score'] = floatval($info['commission2_score']);
            $info['commission3_score'] = floatval($info['commission3_score']);
            $info['jc_reward'] = floatval($info['jc_reward']);
            $info['jd_reward'] = floatval($info['jd_reward']);
            $info['pj_reward'] = floatval($info['pj_reward']);
            $info['ab_reward'] = floatval($info['ab_reward']);
            $info['repurchase_reward1'] = floatval($info['repurchase_reward1']);
            $info['repurchase_reward2'] = floatval($info['repurchase_reward2']);
            $info['repurchase_reward3'] = floatval($info['repurchase_reward3']);
            $info['management_reward'] = floatval($info['management_reward']);
            $info['management_assistant_reward'] = floatval($info['management_assistant_reward']);
            $info['discount'] = floatval($info['discount']);
            $info['strikeout_multiple'] = floatval($info['strikeout_multiple']);
            $info['jd_pingji_reward'] = floatval($info['jd_pingji_reward']);
            $info['cy_reward'] = floatval($info['cy_reward']);
            $info['npingji_reward'] = floatval($info['npingji_reward']);
            $info['laddermanage_assistant_reward'] = floatval($info['laddermanage_assistant_reward']);
        }
        
        __success('操作成功',['levels'=>$list,'info'=>$info]); 
    }
    
    public function post(){
        $params = $this->param();
        
        
      	if(empty($params['title'])){
        	__error("请填写等级名称");
        }
        
        $id = $params['id'];
      
    	$data = array(
          	"title"=>$params['title'],
          	"thumb"=>$params['thumb'],
          	"commission1"=>floatval($params['commission1']),
          	"commission2"=>floatval($params['commission2']),
          	"commission3"=>floatval($params['commission3']),
          	"commission1_score"=>floatval($params['commission1_score']),
          	"commission2_score"=>floatval($params['commission2_score']),
          	"commission3_score"=>floatval($params['commission3_score']),
          	"jc_reward"=>floatval($params['jc_reward']),
          	"jd_reward"=>floatval($params['jd_reward']),
          	"pj_reward"=>floatval($params['pj_reward']),
          	"ab_reward"=>floatval($params['ab_reward']),
          	"jd_pingji_reward"=>floatval($params['jd_pingji_reward']),
          	"repurchase_reward1"=>floatval($params['repurchase_reward1']),
          	"repurchase_reward2"=>floatval($params['repurchase_reward2']),
          	"repurchase_reward3"=>floatval($params['repurchase_reward3']),
          	"management_reward"=>floatval($params['management_reward']),
          	"discount"=>floatval($params['discount']),
          	"is_jiandan"=>intval($params['is_jiandan']),
          	"is_jicha"=>intval($params['is_jicha']),
          	"is_pingji"=>intval($params['is_pingji']),
          	"is_abonus"=>intval($params['is_abonus']),
          	"is_repurchase_pingji"=>intval($params['is_repurchase_pingji']),
          	"is_management"=>intval($params['is_management']),
          	"cloud_warehouse_stock"=>intval($params['cloud_warehouse_stock']),
          	"strikeout_multiple"=>floatval($params['strikeout_multiple']),
          	"description"=>htmlspecialchars_decode($this->param('description/h')),
          	"user_shenhe_apply"=>intval($params['user_shenhe_apply']),
          	"management_assistant_reward"=>floatval($params['management_assistant_reward']),
          	"management_pingji_reward"=>floatval($params['management_pingji_reward']),
          	"laddermanage_assistant_reward"=>floatval($params['laddermanage_assistant_reward']),
          	"becomedue_time"=>intval($params['becomedue_time']),
          	"abonus_out_amount"=>floatval($params['abonus_out_amount']),
          	"jiandan_recommend_reward"=>floatval($params['jiandan_recommend_reward']),
        );
        
        $discount = floatval($params['discount']);
      	if($discount <= 0 || $discount > 10){
        	__error("请填写会员折扣");
        }
        
        $data['is_chaoyue'] = intval($params['is_chaoyue']);
        $data['cy_reward'] = floatval($params['cy_reward']);
        $data['is_npingji'] = intval($params['is_npingji']);
        $data['npingji_reward'] = floatval($params['npingji_reward']);
        
        $data['thumb'] = replaceSiteroot($data['thumb']);
        if($id == 'default'){
            $data['id'] = 0;
            $data['pj_reward_layer'] = iserializer($params['pj_reward_layer']);
            $data['repurchase_pingji_reward'] = iserializer($params['repurchase_pingji_reward']);
            $data['conditions'] = $params['conditions'];
            $levels = \app\common\model\bbfxshop\FenxiaoLevel::getLevels();
            $AdminLogData = $levels[0];
            $fenxiao_data = [
                'default_level'=>iserializer($data),
                "become_agent"=>intval($params['become_agent']),
                // "become_order_type"=>intval($params['become_order_type']),
                // "become_card"=>intval($params['become_card']),
                // "become_goods"=>trim($params['become_goods']),
                // "become_purchase_money"=>floatval($params['become_purchase_money']),
                // "become_cz_balance"=>floatval($params['become_cz_balance']),
                // "become_team_achievement"=>intval($params['become_team_achievement']),
            ];

            $become_agent = Db::name("bbfx_fenxiao")->where(['uniacid'=>UNIACID])->value("become_agent");
            if($fenxiao_data['become_agent'] == 1 && $become_agent > 0){
                $fenxiao_data['become_agent'] = $become_agent;
            }

            $res = Db::name("bbfx_fenxiao")->where(['uniacid'=>UNIACID])->update($fenxiao_data);
        }
        else{
            $data['uniacid'] = UNIACID;
            $data['level'] = intval($params['level']);
            $data['conditions'] = iserializer($params['conditions']);
            $data['pj_reward_layer'] = iserializer($params['pj_reward_layer']);
            $data['repurchase_pingji_reward'] = iserializer($params['repurchase_pingji_reward']);
            
            if(intval($id) > 0){
                $AdminLogData = Db::name("bbfx_fenxiao_level")->where(["id"=>$id])->find();
                $res = Db::name("bbfx_fenxiao_level")->where(["id"=>$id])->update($data);
            }
            else{
                $AdminLogData = $data;
                $res = Db::name("bbfx_fenxiao_level")->insertGetId($data);
                $AdminLogData['id'] = $res;
            }
        }
      	
      	if($res){
      	    
      	    \app\admin\model\AdminLog::record("","",$AdminLogData,"bbfx_fenxiao_level");
      	    $this->clearCache();
        	__success("编辑成功");
        }
      	else{
        	__success("操作成功");
        }
    }

    public function delete(){
        $this->commonDelete("bbfx_fenxiao_level");
    }
    

}