<?php

namespace Bill\Lib;

use Dever;

class Sell
{
    # 发放业绩
    public function push_one_commit($mid, $num, $role, $id, $desc, $cdate = false)
    {
        $this->push($mid, $num, $role, $id, $desc, $cdate);
    }

    # 批量发放业绩
    public function push_all_commit($mid, $num, $role, $id, $desc, $cdate = false)
    {
        $this->push($mid, $num, $role, $id, $desc, $cdate);

        $parent = Dever::load('invite/api')->getParentAll($mid);
        if ($parent) {
            foreach ($parent as $k => $v) {
                $this->push($v['uid'], $num, $role, $id, $desc, $cdate);
            }
        }
    }

    # 发放业绩
    private function push($mid, $num, $role, $id, $desc, $cdate = false)
    {
        $this->up(false, $mid, 1, $num, $role, $id, $desc, 2, $cdate);
        $this->up(false, $mid, 2, $num, $role, $id, $desc, 2, $cdate);
        $where['where_id'] = $mid;
        $where['group_sell'] = $num;
        $where['sell'] = $num;
        return Dever::db('agent/member')->upGroupSellOne($where);
    }

    # 更新数据
    public function up($child, $mid, $type, $num, $role, $type_id, $desc, $stype = 1, $cdate = false)
    {
        if ($num == 0) {
            return;
        }
        if ($type > 2) {
            return;
        }
        $member = Dever::db('agent/member')->find(array('id' => $mid, 'clear' => true));
        if ($member && $member['status'] <= 2) {
            $desc = explode(',', $desc);
            $desc = $desc[0];
            $where['mid'] = $mid;
            $where['type'] = $type;
            $where['stype'] = $stype;
            $where['type_id'] = $type_id;
            $where['clear'] = true;
            $info = Dever::db('bill/sell')->find($where);
            if (!$info) {
                $data = $where;
                if ($child && isset($child['id'])) {
                    $child_mid = Dever::load('invite/api')->getRelation($child['id'], $mid);
                    if ($child_mid) {
                        $data['child_mid'] = $child_mid;
                    }
                }
                if ($cdate) {
                    $data['cdate'] = $cdate;
                }
                $data['order_num'] = $this->getOrderId();
                $data['num'] = $num;
                $data['role'] = $role;
                $data['desc'] = $desc;
                return Dever::db('bill/sell')->insert($data);
            }
            return $info['id'];
        }

        return false;
    }

    /**
     * 生成订单号
     *
     * @return mixed
     */
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('S');
        $state = Dever::db('bill/sell')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }

    # 展示详情
    public function show()
    {
        $id = Dever::input('id');

        $config = Dever::db('bill/sell')->config['set'];

        $info = Dever::db('bill/sell')->one($id);

        $type = $config['type'][$info['type']];

        $member = Dever::db('agent/member')->find($info['mid']);
        $role = Dever::db('setting/role')->one($member['role']);
        $level = Dever::db('setting/level')->one($member['level_id']);

        if ($member['shop_id']) {
            $shop = Dever::db('shop/info')->one($member['shop_id']);
        } else {
            $shop['id'] = -1;
            $shop['name'] = '无';
        }

        $cdate = date('Y-m-d H:i', $info['cdate']);

        $result = array();

        $result['代理商信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('代理商', $member['name'] . ' ' . $member['mobile']),
                    array('代理角色', $role['name'] . ($level ? '('.$level['name'].')' : '')),
                    array('所属店铺', $shop['name']),
                ),

                array
                (
                    array('资金余额', '¥' . $member['cash'] . '元'),
                    //array('直推业绩', '¥' . $member['sell'] . '元'),
                    array('团队业绩', '¥' . $member['group_sell'] . '元'),
                ),
            ),
        );

        $result['业绩信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('流水号', $info['order_num']),
                    array('交易时间', $cdate),
                    array('业绩类型', $type),
                ),

                array
                (
                    array('业绩金额', '¥' . $info['num'] . '元'),
                    array('业绩说明', $info['desc']),
                ),
            )
        );
         $head_btn[] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=bill&table=sell&menu=bill&search_option_state=1', 'manage'),
            'name' => '返回上一页',
        );
        $head = array
        (
            'name' => '基本信息',
            'btn' => $head_btn,
        );
        // $head = array
        // (
        //     'name' => '基本信息',
        //     'btn' => array(),
        // );
        $html = Dever::show($head, $result);

        return $html;
    }
    public function out_sell($data){
        $header = array('代理商姓名', '代理商手机号','代理商角色','代理区域', '下级姓名', '下级手机号', '业绩金额', '下单时间');
        $body = array();
        foreach($data as $k => $v){
            $member = Dever::db('agent/member')->find($v['mid']);
            $role = Dever::db('setting/role')->find($member['role']);
            $area = Dever::load("area/api.string", $member['area'], '-', true, '');
            $child_name = '';
            $child_mobile = '';
            if($v['child_mid'] && $v['child_mid'] > 0){
                $child = Dever::db('agent/member')->find($v['child_mid']);
                $child_name = $child['name'];
                $child_mobile = $child['mobile'];
            }
            
            $cdate = '-';
            if($v['cdate']){
                $cdate = date('Y-m-d H:i',$v['cdate']);
            }
            $d = array
            (
                $member['name'],
                $member['mobile'],
                $role['name'],
                $area,
                $child_name,
                $child_mobile,
                $v['num'],
                $cdate,
            );
            $body[] = $d;
        }
         $file = Dever::input('excel_name');
         return Dever::excelExport($body, $header, $file);
    }
}