<?php

namespace Card\Lib;

use Dever;

class Manage
{
    # 获取用户信息
    public function user($id)
    {
        $info = Dever::db('card/order')->one($id);

        if ($info['uid'] && $info['uid'] > 0) {
            $user = Dever::db('passport/user')->one($info['uid']);

            $result = $user['username'];
            if ($info['mobile']) {
                $result .= '('.$info['mobile'].')';
            }
        } else {
            $result = $info['mobile'];
        }
        
        return $result;
    }

    /**
     * 获取礼品卡数量
     *
     * @return mixed
     */
    public function getNum($id)
    {
        //$info = Dever::db('card/info')->find($id);
        $where['card_id'] = $id;
        return Dever::db('card/code')->total($where);
    }

    /**
     * 显示用户信息
     *
     * @return mixed
     */
    public function showUserInfo($id)
    {
        $info = Dever::db('card/code')->find($id);
        $table = array();
        if ($info && $info['status'] > 1 && $info['uid'] && $info['uid'] > 0) {
            $user = Dever::load('passport/user-one', $info['uid']);
            if ($user) {
                $table['购买人'] = $user['username'] . '('.$user['mobile'].')';
                $table['下单时间'] = date('Y-m-d H:i:s', $info['cdate']);
                $table['支付时间'] = date('Y-m-d H:i:s', $info['bdate']);
            }

            if (isset($info['dh_uid']) && $info['dh_uid'] && $info['dh_uid'] > 0) {
                $user = Dever::load('passport/user-one', $info['dh_uid']);
                if ($user) {
                    $table['兑换人'] = $user['username'] . '('.$user['mobile'].')';
                    $table['兑换时间'] = date('Y-m-d H:i:s', $info['ddate']);
                }
            }

            if (!$info['total_cash']) {
                $card_info = Dever::db('card/info')->find($info['card_id']);
                $info['total_cash'] = $card_info['value'];
            }
            if (!$info['use_cash']) {
                $info['use_cash'] = 0;
            }
            $table['总面值'] = $info['total_cash'];
            $table['已用面值'] = $info['use_cash'];
            $table['剩余面值'] = $info['total_cash'] - $info['use_cash'];

            if ($table) {
                return Dever::table($table);
            }
        }

        return '暂无';
    }

    /**
     * 更新信息
     *
     * @return mixed
     */
    public function orderPs($id, $name, $data)
    {
        $order_id = Dever::param('order_id', $data);
        if ($order_id) {
            $update['where_id'] = $order_id;
            $update['set_status'] = 3;
            Dever::db('card/order')->update($update);
        }
    }

    /**
     * 更新信息
     *
     * @return mixed
     */
    public function orderSh($id, $name, $data)
    {
        $order_id = Dever::param('order_id', $data);
        if ($order_id) {
            $update['where_id'] = $order_id;
            $update['set_status'] = 4;
            Dever::db('card/order')->update($update);
        }
    }

    # 收货
    public function send_api()
    {
        return Dever::load('card/lib/buy')->send();
    }

    /**
     * 作废
     *
     * @return mixed
     */
    public function drop_api($id)
    {
        $update['where_id'] = $id;
        $update['type'] = 4;
        Dever::db('card/code')->update($update);
        return 'ok';
    }

    public function recovery_api($id)
    {
        $update['where_id'] = $id;
        $update['type'] = 1;
        Dever::db('card/code')->update($update);
        return 'ok';
    }
}