<?php

namespace Admin\Controller;

use User\Api\BusinessAffairsApi;
use User\Api\UserApi;

/**
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class  BusinessAffairsController extends ThinkController
{

    public function lists()
    {
        if (isset($_REQUEST['account'])) {
            $map['account'] = $_REQUEST['account'];
            unset($_REQUEST['account']);
        }
        if (isset($_REQUEST['status'])) {
            $map['status'] = $_REQUEST['status'];
            unset($_REQUEST['status']);
        }
				
        $this->m_title = '商务专员';
        $this->assign('commonset',M('Kuaijieicon')->where(['url'=>'BusinessAffairs/lists','status'=>1])->find());

        unset($_REQUEST['phone']);
        parent::order_lists("BusinessAffairs", $_GET["p"], $map);
    }

    public function add()
    {
        if (IS_POST) {
            $user = new BusinessAffairsApi();
            $result = $user->add($_POST);
            if ($result > 0) {
                \Think\Log::actionLog('BusinessAffairs/add','BusinessAffairs',$result);
                $this->success("添加成功", U('lists'));
            } else {
                $this->error($result);
            }
        } else {
            $this->meta_title = "添加商务专员";
						
						$this->m_title = '商务专员';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'BusinessAffairs/lists','status'=>1])->find());
				
						
            $this->display();
        }

    }

    public function edit($model = '', $id = 0)
    {
        $id || $this->error('请选择要查看的用户!');
        $data = array();
        if (IS_POST) {
            $data['status'] = I('post.status');
            $data['phone'] = I('post.phone');
            $data['sw_name'] = I('post.sw_name');
            $data['qq'] = I('post.qq');
            $pwd = trim($_POST['password']);
            $use = new UserApi();
            if (!empty($pwd)) {
                $data['password'] = $pwd;
            }

            $ba_model = M("BusinessAffairs", "tab_");
            // 自动验证规则
            $validate = array(
                array('password', '6,30', "密码长度不合法", 0, 'length'), //密码长度不合法
                array('sw_name', '1,16', '姓名长度为1-16个字符', 1, 'length'),
                array('phone', '/^1[3|4|5|8][0-9]\d{4,8}$/', '手机号码错误!', '0', 'regex', 1),
            );
            $ba_model->setProperty("_validate", $validate);
            if ($ba_model->create($data)) {
								if (!empty($pwd)) {
									$data['password'] = think_ucenter_md5($pwd, UC_AUTH_KEY);
								}
                $result = $ba_model->where(array("id" => $_POST['id']))->save($data);
                if ($result !== false) {
                    \Think\Log::actionLog('BusinessAffairs/edit','BusinessAffairs',$id);
                    $this->success("修改成功", U('lists'));
                } else {
                    $this->error("修改失败");
                }
            } else {
                $this->error($ba_model->getError());
            }

        } else {
            $d = M("BusinessAffairs", "tab_")->find($id);
            $this->assign("data", $d);
            $this->meta_title = "查看商务专员";
						
						$this->m_title = '商务专员';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'BusinessAffairs/lists','status'=>1])->find());
				
						
            $this->display();
        }
    }

    /**
     * 商务专员状态修改
     * @author 小纯洁
     */
    public function changeStatus($method = null)
    {
        $id = array_unique((array)I('id', 0));
        $id = is_array($id) ? implode(',', $id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] = array('in', $id);
        \Think\Log::actionLog('BusinessAffairs/changeStatus','BusinessAffairs',$id);
        switch (strtolower($method)) {
            case 'forbid':
                $this->forbid('BusinessAffairs', $map);
                break;
            case 'resume':
                $this->resume('BusinessAffairs', $map);
                break;
            case 'delete':
                $this->delete('BusinessAffairs', $map);
                break;
            default:
                $this->error('参数非法');
        }
    }

    /**
     *删除商务专员
     * @author 小纯洁
     */
    public function delete($id)
    {
        M()->startTrans();
        \Think\Log::actionLog('BusinessAffairs/delete','BusinessAffairs',$id);
        $res1 = M('BusinessAffairs', 'tab_')->delete($id);
        $data['ba_id'] = 0;
        $res2 = M("promote", "tab_")->where("ba_id=" . $id)->save($data);
        if ($res1 && $res2 !== false) {
            M()->commit();
            $this->success('删除成功');
        } else {
            M()->rollback();
            $this->error('删除失败' . M()->getError());
        }
    }

    /**
     *展示商务专员下级推广员信息 
     * @author 小纯洁
     */
    public function showPromote()
    {
        $_GET['id'] || $this->error('请选择要查看的用户!');
        if (isset($_REQUEST['promote_name'])) {
            $map['account'] = array('like', '%' . $_REQUEST['promote_name'] . '%');
            unset($_REQUEST['promote_name']);
        }
        $map['ba_id'] = $_GET['id'];
        $promote_model = M("promote", "tab_");
        $num  = $promote_model->where($map)->count();
        $data = $promote_model->where($map)->field("id,account,ba_id")->select();
        $this->assign("data", $data);
        $this->assign("sw_name", $_GET['sw_name']);
        $this->assign("num", $num);
        $this->assign("ba_id", $_GET["id"]);
        $this->display();
    }

    /**
     *删除此商务专员下级推广员
     * @author 小纯洁
     */
    public function delPromote($ids=0,$ba_id=0)
    {
        if (empty($ids)) {
            $this->error('请选择要操作的数据!');
        }
        $ids = array_unique((array)I('ids', 0));
        $Model = M("promote", "tab_");
        $map = array('id' => array('in', $ids));
        $data['ba_id'] = 0;
        D('BusinessAffairs')->del_child($ba_id,$ids);
        if ($Model->where($map)->save($data) !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败!');
        }
    }

    /**
     *清空此商务专员下的所以子集推广员
     * @author 小纯洁
     */
    public function delAllPromote($ba_id)
    {
        $Model = M("promote", "tab_");
        $map = array('ba_id' => $ba_id);
        $data['ba_id'] = 0;
        //D('BusinessAffairs')->del_child($ba_id,$ids);
        if ($Model->where($map)->save($data) !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败!');
        }
    }

    public function summaryQuery($p = 0)
    {
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据arraypage
        $arraypage = $page ? $page : 1; //默认显示第一页数据
				
				if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}

        if (isset($_REQUEST['timestart']) && isset($_REQUEST['timeend'])) {
            if (isset($_GET['ba_id'])) {
                $map['id'] = $_GET['ba_id'];
            }
            $ba = M('business_affairs', 'tab_')->field('id,account,promote_id')->where($map)->select();

            foreach ($ba as $key => $value) {
                $lists[$key]['ba_account'] = $value['account'];
                $pro_map['ba_id'] = $value['id'];
                $pro_count = M('promote', 'tab_')->field('id')->where($pro_map)->count();
                $lists[$key]['pro_count'] = $pro_count;
								$pro_reg = 0;
								$pro_spend = 0;
								if($value['promote_id']) {									
                $pro_reg_map['promote_id'] = ['in', $value['promote_id']];
                $pro_reg_map_time['register_time'] = array('BETWEEN', array(strtotime($_REQUEST['timestart']), strtotime($_REQUEST['timeend']) + 24 * 60 * 60 - 1));
                $pro_reg_map_paytime['pay_time'] = array('BETWEEN', array(strtotime($_REQUEST['timestart']), strtotime($_REQUEST['timeend']) + 24 * 60 * 60 - 1));
                $pro_reg = M('user', 'tab_')->field('id')->where($pro_reg_map)->where($pro_reg_map_time)->count();
                
                $pro_reg_map['pay_status'] = 1;
                $pro_spend = M('spend', 'tab_')->where($pro_reg_map)->where($pro_reg_map_paytime)->sum('pay_amount');
								}
								$lists[$key]['pro_reg_count'] = $pro_reg;
                $lists[$key]['pro_reg_sum'] = empty($pro_spend) ? 0 : $pro_spend;
            }

            $count = count($lists);
						
						$page = set_pagination($count,$row);
						if($page) {$this->assign('_page', $page);}
						
            $lists = array_slice($lists, ($arraypage - 1) * $row, $row);
            $this->assign('list_data', $lists);
        }
				
				$this->m_title = '商务专员汇总查询';
				$this->meta_title = '汇总查询';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'BusinessAffairs/summaryQuery','status'=>1])->find());
				

        $this->display();
    }


}