<?php

namespace Bill\Lib;

use Dever;

class Tixian
{
    public function getInfo($type, $type_id)
    {
        if ($type == 1) {
            return Dever::load("agent/lib/member.getOne", $type_id, "agent/member", true);
        }
        if ($type == 2) {
            $shop = Dever::db('shop/info')->find($type_id);
            return $shop['name'] . '<br />' . $shop['sid'];
        }
        if ($type == 3) {
            $user = Dever::db('passport/user')->find($type_id);
            return $user['username'] . '<br />' . $user['mobile'];
        }
    }

    public function up($mid, $ycash, $cash,$sign, $type = 1, $cash_id = false)
    {
        $state = false;
        if ($type == 1) {
            $member = Dever::db('agent/member')->find(array('id' => $mid, 'clear' => true));
            if ($member && $member['status'] <= 2) {
                $state = true;
            }
            $mcash = $member['cash'];
        }
        if ($type == 2) {
            $member = Dever::db('shop/info')->find(array('id' => $mid, 'clear' => true));
            if ($member) {
                $state = true;
            }
            $mcash = $member['price'];
        }
        if ($type == 3) {
            $member = Dever::db('passport/user')->find(array('id' => $mid, 'clear' => true));
            if ($member) {
                $state = true;
            }
            $mcash = $member['cash'];
        }
        
        if ($state) {
        	$cash = -1*$cash;
        	$ycash = -1*$ycash;
        	$data['mid'] = $mid;
    		$data['order_num'] = $this->getOrderId();
    		$data['yue'] = $mcash + $ycash;
    		$data['ycash'] = $ycash;
    		$data['cash'] = $cash;
            $data['sign'] = $sign;
            $data['type'] = $type;
            if ($cash_id) {
                $data['cash_id'] = $cash_id;
            }
            $data['status'] = -1;
    		return Dever::db('bill/tixian')->insert($data);
        }

        return false;
    }

    /**
     * 生成订单号
     *
     * @return mixed
     */
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('C');
        $where['clear'] = true;
        $state = Dever::db('bill/cash')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }
    public function order_num($id){
        $info = Dever::db('bill/tixian')->find($id);
        $cdate = date('Y-m-d H:i',$info['cdate']);
        if ($info['operdate']) {
            $operdate = date('Y-m-d H:i',$info['operdate']);
        } else {
            $operdate = '-';
        }

        $html['name'] = $info['order_num'].'<br />'.$cdate.'<br />'.$operdate;
        $html['money'] = $info['ycash'].'<br />'.$info['cash'].'<br />'.$info['yue'];
        if ($info['type'] == 1) {
            $cash = Dever::db('bill/cash')->find($info['cash_id']);
        } elseif ($info['type'] == 2) {
            $cash = Dever::db('shop/record')->find($info['cash_id']);
        } elseif ($info['type'] == 3) {
            $cash = Dever::db('shop/user_money')->find($info['cash_id']);
        }
        
        if($cash['status']==1){
            $name = '待审核';
        }elseif($cash['status']==2){
            $name = '已审核';
        }elseif($cash['status']==3){
            $name = '已作废';
        }
        if($info['status']==-1){
            $status = '未审核待发放';
        }elseif($info['status']==1){
            $status = '已审核待发放';
        }elseif($info['status']==2){
            $status = '已发放';
        }elseif($info['status']==3){
            $status = '已作废';
        }
        $html['status'] = $name.'<br/>'.$status;
        return $html;
    }
}