<?php

namespace Agent\Lib;

use Dever;

class Member
{
    public function getOne($id, $table = 'agent/member')
    {
        if ($id < 0) {
            return '无';
        }
        $member = Dever::db($table)->find($id);
        if ($member) {
            $member['role'] = Dever::db('setting/role')->find($member['role']);
            $string = $member['name'] . '<br />' . $member['mobile'] . '<br />' . $member['role']['name'];
            if (isset($member['level_id']) && $member['level_id'] && $member['level_id'] > 0) {
                $member['level'] = Dever::db('setting/level')->find($member['level_id']);
                $string .= '（'.$member['level']['name'].'）';
            }
            return $string;
            
        } else {
            return '';
        }
    }

    public function getInfoString($member)
    {
        $member = $this->getInfo($member);

        $info = '代理商信息：'.$member['name'].' '.$member['mobile'].' ' . $member['role']['name'];
        if ($member && isset($member['level']) && $member['level']) {
            $info .= '（'.$member['level']['name'].'）';
        }

        return $info;
    }

    public function getInfo($member)
    {
        Dever::setInput('getInfo', 1);
        if (is_numeric($member)) {
            $member = Dever::db('agent/member')->find($member);
        }
        $member['area_string'] = Dever::load("area/api.string", $member['area'], '/', true, '');
        $member['role'] = Dever::db('setting/role')->find($member['role']);
        $member['level'] = Dever::db('setting/level')->find($member['level_id']);

        $member['shop'] = array();
        if ($member['shop_id'] && $member['shop_id'] > 0) {
            $member['shop'] = Dever::db('shop/info')->getOne($member['shop_id']);
        }

        $data = Dever::load('passport/reg')->getSign($member['id']);
        if ($data['signature']) {
            $member['signature'] = $data['signature'];
        }

        $member['birthday_string'] = date('Y-m-d', $member['birthday']);

        return $member;
    }

    /**
     * 检测邀请码
     *
     * @return mixed
     */
    public function checkCode()
    {
        $code = Dever::input('code');
        if (!$code) {
        	Dever::alert('请输入邀请码');
        }
        $uid = Dever::load('invite/api')->getUid($code);
        if (!$uid) {
        	Dever::alert('邀请码错误');
        }

        $user = Dever::db('agent/member')->find($uid);
        if (!$user) {
        	Dever::alert('邀请码错误');
        }

        if ($user['status'] == 2) {
        	return $user;
        }

        Dever::alert('邀请人状态不正常');
    }

	/**
     * 验证手机号
     * @return mixed
     */
    public function checkMobile($mobile)
    {
        $rule = Dever::rule('mobile');

        if (!$mobile) {
            Dever::alert('手机号不能为空');
        }
        if (!preg_match($rule, $mobile)) {
            Dever::alert('手机号码格式错误');
        }

        return $mobile;
    }

    /**
     * 验证验证码
     * @return mixed
     */
    public function checkMcode($mobile)
    {
        return;
        $code = Dever::input('mcode');
        if (!$code) {
            Dever::alert('请输入验证码');
        }
        $code = Dever::load('passport/reg')->mcode($mobile, $code);
        if (!$code) {
            Dever::alert('验证码输入错误');
        }
    }

    /**
     * 更新余额和业绩
     * @return mixed
     */
    public function up($member, $order_id, $role, $type, $mid, $cash, $sell, $level_id = false, $desc = '')
    {
        # 增加日志
        Dever::load('bill/lib/cash')->up($mid, $type, $cash, $role, $order_id, $desc, 2);

        Dever::load('bill/lib/sell')->up($mid, $type, $sell, $role, $order_id, $desc);

        $data['where_id'] = $mid;
        $data['cash'] = $cash;
        $data['upcol'] = $sell;
        if ($level_id) {
            $data['level_id'] = $level_id;
        }
        if ($type == 1) {
            $method = 'upSell';
        } else {
            $method = 'upGroupSell';
        }
        $data['clear'] = true;
        Dever::db('agent/member')->$method($data);

        if ($level_id) {
            Dever::load('agent/lib/level_log')->up($mid, $order_id, $member['level_id'], $level_id, $sell, $member['sell'] + $sell);
        }
    }

    /**
     * 查看详情
     * @return mixed
     */
    public function show()
    {
        $mid = Dever::input('mid');
        $member = Dever::db('agent/member')->one($mid);

        $config_type = Dever::db('agent/member')->config['config_type'];
        $config_status = Dever::db('agent/member')->config['config_status'];

        $result['代理商信息'] = $this->getShowInfo($member);

        if ($member['parent_mid'] && $member['parent_mid'] > 0) {
            $parent = Dever::db('agent/member')->one($member['parent_mid']);
            $result['邀请人信息'] = $this->getShowInfo($parent);
        }

        $idcard_pic = '<a href="'.$member['idcard_front'].'" target="_blank"><img src="'.$member['idcard_front'].'" width="150" /></a>';
        if ($member['idcard_back']) {
            $idcard_pic .= '&nbsp;&nbsp;<a href="'.$member['idcard_back'].'" target="_blank"><img src="'.$member['idcard_back'].'" width="150" /></a>';
        }
        $member_name = $member['name'];
        $member_number = $member['idcard'];

        $result['认证信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('类型', $config_type[$member['type']]),
                    array('姓名', $member_name),
                    array('身份证号', $member_number),
                ),
            ),
        );

        if ($member['type'] == 2) {
            $company_pic = '<a href="'.$member['company_pic'].'" target="_blank"><img src="'.$member['company_pic'].'" width="150" /></a>';
            $member_name = $member['company_name'];
            $member_number = $member['company_number'];

            $result['认证信息']['content'][] = array
            (
                array('企业名称', $member_name),
                array('营业执照', $member_number),
                array('营业执照照片', $company_pic),
            );
        }

        $result['认证信息']['content'][] = array
        (
            array('身份证照片', $idcard_pic),
        );

        $button = array();
        $button[0]['btn'][] = array
        (
            'type' => 'edit',
            'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=name,mobile,password,email,shop_id,area,role,level_id', 'manage'),
            'name' => '修改基础资料',
        );
        $button[0]['btn'][] = array
        (
            'type' => 'edit',
            'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=type,idcard,idcard_front,idcard_back,company_name,company_pic,company_number', 'manage'),
            'name' => '修改认证资料',
        );

        /*
        $button[] = array
        (
            'type' => 'edit',
            'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=shop_id', 'manage'),
            'name' => '修改归属门店',
        );
        */
        $url = '';
        if ($member['status'] == 5) {
            $button[0]['btn'][] = array
            (
                'type' => 'action',
                'link' => Dever::url('lib/member.setStatus?id=' . $member['id'] . '&value=2', 'agent'),
                'name' => '恢复账号',
            );
        } elseif ($member['status'] == 2) {
            $button[0]['btn'][] = array
            (
                'type' => 'action',
                'link' => Dever::url('lib/member.setStatus?id=' . $member['id'] . '&value=5', 'agent'),
                'name' => '禁用账号',
            );
        }

        $button[0]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=order&search_option_mid='.$member['id'].'', 'manage'),
            'name' => '代理订单',
        );

        $button[0]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=order&search_option_parent_mid='.$member['id'].'', 'manage'),
            'name' => '直推代理订单',
        );

        $button[0]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=member_level_log&search_option_mid='.$member['id'].'', 'manage'),
            'name' => '升级日志',
        );

        $button[1]['btn'][] = array
        (
            'type' => 'edit',
            'link' => Dever::url('project/database/update?project=bill&table=push_cash&search_option_mid='.$member['id'], 'manage'),
            'name' => '发放资金',
        );

        $button[1]['btn'][] = array
        (
            'type' => 'edit',
            'link' => Dever::url('project/database/update?project=bill&table=push_sell&search_option_mid='.$member['id'], 'manage'),
            'name' => '发放业绩',
        );

        $button[1]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=bill&table=cash&search_option_mid='.$member['id'].'', 'manage'),
            'name' => '资金账户',
        );

        $button[1]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=bill&table=sell&search_option_mid='.$member['id'].'', 'manage'),
            'name' => '业绩流水',
        );

        $button[1]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=member_goods&search_option_mid='.$member['id'].'', 'manage'),
            'name' => '代理商权益',
        );

        $button[1]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=dh_order&search_option_mid='.$member['id'].'', 'manage'),
            'name' => '权益订单',
        );

        $button[2]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=member_agreement&search_option_mid='.$member['id'].'', 'manage'),
            'name' => '代理商合同',
        );

        $button[2]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=member&search_option_parent_mid='.$member['id'].'', 'manage'),
            'name' => '查看直推团队',
        );

        /*
        $button[2]['btn'][] = array
        (
            'type' => 'edit',
            'link' => Dever::url('lib/member.setStatus?id=' . $member['id'] . '&value=5', 'agent'),
            'name' => '查看邀请关系',
        );
        */

        $furl = '';
        $button[2]['btn'][] = array
        (
            'type' => 'edit',
            'link' => Dever::url('project/database/update?project=agent&table=member_parent&mid='.$member['id'], 'manage'),
            'name' => '设置直推上级',
        );

        $head_btn[] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=member', 'manage'),
            'name' => '返回上一页',
        );
        $head = array
        (
            'name' => '基本信息',
            'btn' => $head_btn,
        );
        $foot = $button;
        $html = Dever::show($head, $result, $foot);

        return $html;
    }

    private function getShowInfo($member)
    {
        $role = Dever::db('setting/role')->one($member['role']);
        $level = Dever::db('setting/level')->one($member['level_id']);
        $area = Dever::load("area/api.string", $member['area'], '-', true, '');
        $code = Dever::load("invite/api.code", $member['id']);

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

        $result = array();

        if ($level) {
            $role['name'] .= '（' . $level['name'] . '）';
        }

        if (!$member['cash']) {
            $member['cash'] = 0;
        }

        if (!$member['sell']) {
            $member['sell'] = 0;
        }

        if (!$member['group_sell']) {
            $member['group_sell'] = 0;
        }

        $result = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('代理商姓名', $member['name']),
                    array('手机号', $member['mobile']),
                    array('邀请码', $code),
                ),

                array
                (
                    array('代理角色', $role['name']),
                    array('代理区域', $area),
                    array('所属店铺', $shop['name']),
                ),

                array
                (
                    array('资金余额', '<span style="color:blue">¥' . $member['cash'] . '元</span>'),
                    array('直推业绩', '<span style="color:blue">¥' . $member['sell'] . '元</span>'),
                    array('团队业绩', '<span style="color:blue">¥' . $member['group_sell'] . '元</span>'),
                ),
            ),
        );

        return $result;
    }

    public function setStatus_api()
    {
        $where['where_id'] = Dever::input('id');
        $where['status'] = Dever::input('value');

        Dever::db('agent/member')->update($where);

        return 'reload';
    }
}