<?php
namespace store\Admin;

use Dever;

class Order extends Core
{
	# 获取概览数据
	public function getData()
	{
		# 待处理订单
		$where['status'] = 3;
		$where['source_type'] = 2;
		$where['source_id'] = $this->id;

		# 待处理订单
		$data['order_num'] = Dever::db('shop/buy_order')->getOrderNum($where);

		# 超时待处理订单：72小时
		$where['end_o'] = time() - 72*3600;
		$data['cs_order_num'] = Dever::db('shop/buy_order')->getOrderNum($where);

        # 超时待处理订单：72小时
        $where = array();
        $where['status'] = 4;
        $where['type'] = 2;
        $where['type_id'] = $this->id;
        $where['end_o'] = time() - 72*3600;

        $data['sh_order_num'] = Dever::db('shop/buy_order')->getOrderNum($where);

		return $data;
	}

	# 订单列表
    public function getList()
    {
        $data = Dever::load('mshop/lib/buy')->set(12, 1)->getList($this->id);

        $html = '';
        if ($data && $data['order']) {
            $out_status = Dever::db('shop/buy_order')->config['out_status'];
            $excel = Dever::input('excel', 1);
            if ($excel == 2) {
                Dever::load('store/admin/excel')->out($this->id, $this->user, $data['order']);
                die;
                return;
            }

        	foreach ($data['order'] as $k => $v) {
        		if ($v['ps_info']) {
        			$ps_date = $v['ps_info']['cdate'];
        		} else {
        			$ps_date = '-';
        		}
        		$view = Dever::url('order_view?type=3&noprice=1&order_id=' . $v['id'] . '&cash=2&store_type=1', 'store');

        		$send = Dever::url('order_send?id=' . $v['id'] . '&type=2', 'store');

        		$button = '<a href="'.$view.'"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">查看详情</button></a>';

        		if ($v['status'] < 5) {

                    $print = Dever::url('admin/order.print?id=' . $v['id'] . '&type=2', 'store');

        			$button .= '&nbsp;&nbsp;<a href="'.$print.'" id="print_'.$v['id'].'" target="_blank"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">打印备货单</button></a>';
        		}
        		
        		if ($v['status'] == 3) {
        			$button .= '&nbsp;&nbsp;<a href="javascript:;" id="send_'.$v['id'].'" onclick="fastEdit($(this), \''.$send.'\', \'配送发货\', \'\', 1)"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">配送发货</button></a>';
        		}

                if ($v['status_name'] == '待确认收货') {
                    $v['status_name'] = '已发货';
                }

                if ($v['refund_status'] == 2 && $v['status'] != 8) {
                    $v['status_name'] .= '[申请报损中]';
                }
                $order = array();
                if ($v['parent_type'] == 2) {
                    $order = Dever::db('shop/sell_order')->find($v['parent_order_id']);

                    if ($order && $order['uid']) {
                        if ($order['address_id']) {
                            $order['address'] = Dever::load('passport/address')->getOne($order['uid'], $order['address_id']);

                            $v['type_info']['name'] = $order['address']['contact'];
                        } else {
                            $user = Dever::db('passport/user')->find($order['uid']);
                            $v['type_info']['name'] = $user['username'];
                        }
                    }
                } elseif ($v['parent_type'] == 3) {
                    $order = Dever::db('agent/dh_order')->find($v['parent_order_id']);
                    if ($order && $order['mid']) {
                        if ($order['address_id']) {
                            $order['address'] = Dever::load('agent/address')->getOne($order['mid'], $order['address_id']);

                            $v['type_info']['name'] = $order['address']['contact'];
                        } else {
                            $user = Dever::db('agent/member')->find($order['mid']);
                            $v['type_info']['name'] = $user['name'];
                        }
                    }
                }

                $v['out_status_name'] = $out_status[$v['source_out_status']];

                $dao_date = '-';
                if ($v['source_out_date']) {
                    $dao_date = date('Y-m-d H:i', $v['source_out_date']);
                }

        		//$html .= '<tr><td><input type="checkbox" name="mul_where_id[]" class="checkbox-checkall-list" lay-ignore value="'.$v['id'].'"/></td><td>'.$v['order_num'].'</td><td>'.$v['operdate'].'</td><td>'.$v['type_info']['name'].'</td><td>'.$ps_date.'</td><td>'.$v['num'].'</td><td>'.$v['status_name'].'</td><td>'.$v['out_status_name'].'</td><td>'.$dao_date.'</td><td>'.$button.'</td></tr>';

                $html .= '<tr><td>'.$v['order_num'].'</td><td>'.$v['operdate'].'</td><td>'.$v['type_info']['name'].'</td><td>'.$ps_date.'</td><td>'.$v['num'].'</td><td>'.$v['status_name'].'</td><td>'.$v['out_status_name'].'</td><td>'.$dao_date.'</td><td>'.$button.'</td></tr>';
        	}
        }
        return $html;
    }

    # 获取列表页搜索信息
    public function getSearch()
    {
    	$config = array
        (
        	array('value' => '0', 'name' => '全部'),
            array('value' => 3, 'name' => '待发货'),
            array('value' => 4, 'name' => '已发货'),
            array('value' => '5,6', 'name' => '已到货'),
            array('value' => '7,8,11', 'name' => '已取消'),
            array('value' => 'refund', 'name' => '缺货与报损'),
        );

        $status = Dever::input('status');

        $html = '';
        foreach ($config as $k => $v) {
        	$selected = '';
    		if ((!$status && $k == 0) || $status == $v['value']) {
    			$selected = 'selected=""';
    		} 

    		$html .= '<option value="'.$v['value'].'" '.$selected.'>'.$v['name'].'</option>';
    	}

        return $html;
    }

    # 获取列表页搜索信息
    public function getSearchOutStatus()
    {
        $config = array
        (
            array('value' => '0', 'name' => '全部'),
            array('value' => 1, 'name' => '未导出'),
            array('value' => 2, 'name' => '已导出'),
        );

        $status = Dever::input('source_out_status');

        $html = '';
        foreach ($config as $k => $v) {
            $selected = '';
            if ((!$status && $k == 0) || $status == $v['value']) {
                $selected = 'selected=""';
            } 

            $html .= '<option value="'.$v['value'].'" '.$selected.'>'.$v['name'].'</option>';
        }

        return $html;
    }

    # 获取列表页搜索信息
    public function getService()
    {
    	$service = Dever::db('shop/service')->select();
        $html = '';
        foreach ($service as $k => $v) {
        	$selected = '';
    		if ($k == 0) {
    			$selected = 'selected=""';
    		} 

    		$html .= '<option value="'.$v['id'].'" '.$selected.'>'.$v['name'].'</option>';
    	}

        return $html;
    }

    # 获取基本信息
    public function getConfig()
    {
    	$data['search'] = Dever::url('order', 'store');

    	# 发货
    	$data['send'] = Dever::url('admin/order.send', 'store');

        # 退款
        $data['tui'] = Dever::url('lib/buy.refund_apply_info_one_commit?json=1', 'mshop');
    	return $data;
    }

    # 发货
    public function send_api()
    {
    	$input = Dever::input();
        $update = array();

    	$order_id = Dever::input('update_where_id');

    	$where['order_id'] = $order_id;
    	$info = Dever::db('shop/buy_order_ps')->find($where);

    	$update = $where;
        foreach ($input as $k => $v) {
            if (strpos($k, '_c_') !== false) {
                if ($v == 'null') {
                    $v = '';
                }
                $temp = explode('_c_', $k);
                $k = str_replace('update_', '', $temp[0]);
                if (!isset($c[$k])) {
                    $c[$k] = array();
                }
                if (strpos($temp[1], '_i_') !== false) {
                    $temp = explode('_i_', $temp[1]);

                    if (!isset($c[$k][$temp[1]])) {
                        $c[$k][$temp[1]] = 0;
                    } else {
                        $c[$k][$temp[1]]++;
                    }

                    $num = $c[$k][$temp[1]];

                    $update[$k][$num][$temp[1]] = $v;
                }
            }
        }
        if (isset($update['service'])) {
            $update['service'] = Dever::array_encode($update['service']);
        }
        /*
    	$update['service_id'] = $input['update_service_id'];
    	$update['order_num'] = $input['update_order_num'];
        $update['num'] = $input['update_num'];
    	$update['phy'] = $input['update_phy'];
    	$update['dby'] = $input['update_dby'];
    	$update['zl'] = $input['update_zl'];
    	$update['tj'] = $input['update_tj'];
    	$update['price'] = $input['update_price'];
        */
    	$update['cdate'] = $input['update_cdate'];
    	if ($info) {
    		$update['where_id'] = $info['id'];
    		Dever::db('shop/buy_order_ps')->update($update);
    	} else {
    		Dever::db('shop/buy_order_ps')->insert($update);
    	}
    	Dever::db('shop/buy_order')->update(array('status' => 4, 'where_id' => $update['order_id']));

        # 检测是否是关联订单
        $order = Dever::db('shop/buy_order')->find($update['order_id']);
        if ($order && $order['parent_type'] > 1) {
            $data = Dever::db('shop/buy_order')->find(array('status' => 3, 'parent_type' => $order['parent_type'], 'parent_order_id' => $order['parent_order_id']));

            if (!$data) {
                # 更改关联订单状态
                if ($order['parent_type'] == 2) {
                    Dever::db('shop/sell_order')->update(array('where_id' => $order['parent_order_id'], 'status' => 4));

                    $data = Dever::db('shop/buy_order')->getDataToId(array('parent_type' => $order['parent_type'], 'parent_order_id' => $order['parent_order_id']));

                    $data = array_keys($data);
                    

                    $ps_info = Dever::db('shop/buy_order_ps')->getData(array('order_id' => $data));

                    if ($ps_info) {
                        $sell_order = array('id' => $order['parent_order_id']);
                        foreach ($ps_info as $k => $v) {
                            $v['service'] = Dever::array_decode($v['service']);
                            foreach ($v['service'] as $k1 => $v1) {
                                if (isset($v1['order_num']) && $v1['order_num']) {
                                    Dever::load('shop/lib/sell')->updatePs($sell_order, 2, $v1['service_id'], $v1['order_num']);
                                }
                                
                            }
                        }
                    }
                } elseif ($order['parent_type'] == 3) {
                    Dever::db('agent/dh_order')->update(array('where_id' => $order['parent_order_id'], 'status' => 4));

                    $data = Dever::db('shop/buy_order')->getDataToId(array('parent_type' => $order['parent_type'], 'parent_order_id' => $order['parent_order_id']));

                    $data = array_keys($data);
                    

                    $ps_info = Dever::db('shop/buy_order_ps')->getData(array('order_id' => $data));

                    if ($ps_info) {
                        $dh_order = array('id' => $order['parent_order_id']);
                        foreach ($ps_info as $k => $v) {
                            $v['service'] = Dever::array_decode($v['service']);
                            foreach ($v['service'] as $k1 => $v1) {
                                if (isset($v1['order_num']) && $v1['order_num']) {
                                    Dever::load('agent/lib/dhorder')->updatePs($dh_order, 2, $v1['service_id'], $v1['order_num']);
                                }
                            }
                        }
                    }
                }
            }
        }

    	return 'reload';
    }

    public function print_api()
    {
        return Dever::load('mshop/lib/buy')->printer($this->user);
    }

    # 获取报损信息
    public function refund()
    {
        $data = Dever::load('mshop/lib/buy')->refund_apply_info_one_info();

        if (isset($data['num'])) {
            $data['tui_price_info'] = 'x' . $data['num'];
        } else {
            $data['tui_price_info'] = '0';
        }
        return $data;
    }
}