<?php
namespace Smi\Controller;

class DistributionController extends BaseController 
{ 
  protected $business_id;

  public function __construct()
  {
    parent::__construct();
    $this->business_id = session('admin_id');
  }

  /**
   * [share_apply 分销员申请列表]
   * @AuthorHTL
   * @DateTime  2018-03-14T10:34:10+0800
   * @return    [type]                   [description]
   */
  public function share_apply()
  {
    $op = I('op');

    $map = " 1 ";

    $id = I('id')?:0;

    $nowtime=time();

    if($id){

      $map = " id = {$id} ";

    }else{

      $type = I('type');

      $title = I('title');

    }

    if($title){

      $map .= " and (name like '%{$title}%' ) ";
      $map .= " or (phone like '%{$title}%' ) ";

    }

    if($type == 1){

      $map .= " and status = 1  ";

    }else if($type == 2){

      $map .= " and status = 0 ";

    }

    if($op != 'export'){

      $count = M("distribution_apply")->where($map)->count();

      $Page = new \Think\Page($count,20);

      $this->assign("page",$Page->show());

      $limit = $Page->firstRow.','.$Page->listRows;

    }

    $list = M("distribution_apply")->where($map)->order("status desc")->limit($limit)->select();

    $this->assign("list",$list);
    $this->display();
  }

  /**
   * [cash_apply 佣金提现申请]
   * @AuthorHTL
   * @DateTime  2018-03-14T10:42:05+0800
   * @return    [type]                   [description]
   */
  public function cash_apply()
  {
    $op = I('op');

    $map = " 1 ";

    $id = I('id')?:0;

    $nowtime=time();

    if($id){

      $map = " id = {$id} ";

    }else{

      $type = I('type');

      $title = I('title');

    }

    if($title){

      $map .= " and (name like '%{$title}%' ) ";
      $map .= " or (card_number like '%{$title}%' ) ";

    }

    if($type == 1){

      $map .= " and status = 1  ";

    }else if($type == 2){

      $map .= " and status = 0 ";

    }

    if ($map) {
      $map .= " AND ";
    }
    $map .= "is_del = 0";

    if($op != 'export'){

      $count = M("cash_apply")->where($map)->count();

      $Page = new \Think\Page($count,20);

      $this->assign("page",$Page->show());

      $limit = $Page->firstRow.','.$Page->listRows;

    }

    $list = M("cash_apply")->where($map)->order("status desc")->limit($limit)->select();

    $this->assign("list",$list);

    $this->display();

  }

  /**
   * [share_config 分销配置]
   * @AuthorHTL
   * @DateTime  2018-03-14T10:42:28+0800
   * @return    [type]                   [description]
   */
  public function share_config()
  {
    $data = I('post.');

     if (!empty($data)) {
      $save = array(
          'amount' => $data['amount'],
          'one_share' => $data['one_share'],
          'two_share' => $data['two_share'],
          'switch' => $data['switch']
        );

      $result = M('distribution_config')->where(array('admin_id' => $this->business_id))->save($save);

      if ($result) {
        $this->success('The update is successful');die;
      } else {
        $this->error('Fail to Update');die;
      }
    }

    $info = M('distribution_config')->where(array('admin_id' => $this->business_id))->find();

    if (empty($info)) {
      M('distribution_config')->add(array('admin_id' => $this->business_id));
      $info = M('distribution_config')->where(array('admin_id' => $this->business_id))->find();
    }

    $this->assign('info', $info);
    $this->display();
  }

  /**
   * 分销员申请处理
   * @return [type] [description]
   */
  public function pass_apply(){
    $id=$_REQUEST['id'];
    $status=$_REQUEST['status'];
    $userid=$_REQUEST['userid'];
    // dump($_REQUEST);exit;
    $save['status']=$status;
    $save['endtime']=time();
    $res=M('distribution_apply')->where('id='.$id)->save($save);

    if($status==1){
      $share['status']=1;
      M('users')->where('id='.$userid)->save($share);
    }
    // echo M()->getlastsql();exit;
    if($res){
      echo 10000;exit;
    }else{
      echo 20000;exit;
    }
  }

  /**
   * 分销员申请删除
   * @return [type] [description]
   */
  public function share_apply_del(){
    $id=$_REQUEST['id'];

    $res=M('distribution_apply')->where('id='.$id)->delete();
    if($res){
      echo 10000;exit;
    }else{
      echo 20000;exit;
    }
  }

  /**
   * 分销佣金提现申请处理
   * @return [type] [description]
   */
  public function pass_cash(){
    $id=$_REQUEST['id'];
    $status=$_REQUEST['status'];
    $user_id=$_REQUEST['userid'];
    $money=$_REQUEST['money'];

    $save['status']=$status;
    // $save['endtime']=time();
    $res=M('cash_apply')->where('id='.$id)->save($save);

    if($status==-1){
      // 审核失败佣金退回
      M('users')->where('id='.$user_id)->setInc('share_money',$money);
    }

    if($res){
      echo 10000;exit;
    }else{
      echo 20000;exit;
    }
  }

  /**
   * 删除佣金提现申请记录
   * @param  string $id [description]
   * @return [type]     [description]
   */
  public function del_cash_apply ($id = ""){
    $info = M("cash_apply")->where("id = %d",$id)->find();
    // 未处理记录
    if ($info['status'] == '0') {
      echo 20000;exit;
    }
    $res = M("cash_apply")->where("id = %d",$id)->setField("is_del",1);
    if ($res) {
      echo 10000;exit;
    }else{
      echo 20000;exit;
    }
  }

}