<?php

namespace Bill\Lib;

use Dever;

class Sell
{
    public function up($mid, $type, $num, $role, $type_id, $desc)
    {
        $member = Dever::db('agent/member')->find(array('id' => $mid, 'clear' => true));
        if ($member['status'] <= 2) {
            $desc = explode('，', $desc);
            $desc = $desc[0];
            $where['mid'] = $mid;
            $where['type'] = $type;
            $where['type_id'] = $type_id;
            $where['clear'] = true;
            $info = Dever::db('bill/sell')->find($where);
            if (!$info) {
                $data = $where;
                $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 = array
        (
            'name' => '基本信息',
            'btn' => array(),
        );
        $html = Dever::show($head, $result);

        return $html;
    }
}