<?php
namespace store\Admin;

use Dever;

class Factory_order extends Core
{
	# 订单列表
    public function getList()
    {
        $data = Dever::load('mshop/lib/buy')->set(2, 1)->getList($this->id, '4,5,6');

        $html = '';
        $out_status = Dever::db('shop/buy_order')->config['out_status'];
        $excel = Dever::input('excel', 1);
        if ($data && $data['order']) {
            if ($excel == 2) {
                Dever::load('store/admin/factory_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'];
        			$ydate = $v['ps_info']['ydate'] ? $v['ps_info']['ydate'] : '-';
        		} else {
        			$ps_date = $ydate = '-';
        		}
        		$view = Dever::url('order_view?type=3&order_id=' . $v['id'] . '&cash=2', 'store');

        		$send = Dever::url('order_send?id=' . $v['id'], '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>';

                $print = Dever::url('admin/order.print?id=' . $v['id'] . '&type=2', 'store');
        		$button .= '&nbsp;&nbsp;<a href="'.$print.'" 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'] == 4) {
        			$url = Dever::url('admin/factory_order.action_commit&order_id='.$v['id'], 'store');
        			$button .= '&nbsp;&nbsp;<a href="javascript:;" onclick="load(\''.$url.'\')"><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['refund_status'] == 2 && $v['status'] != 8) {
                    $v['status_name'] .= '[缺货报损中]';
                }

                $url = Dever::url('lib/buy.excel_one?type='.$v['type'].'&type_id='.$v['type_id'].'&id='.$v['id'], 'mshop');

                if ($v['status'] == 5 || $v['status'] == 6 || $v['status'] == 8) {
                    //$button .= '&nbsp;&nbsp;<a href="'.$url.'"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">导出</button></a>';
                }

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

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


        		$html .= '<tr><td>'.$v['order_num'].'</td><td>'.$ps_date.'</td><td>'.$v['source_info']['name'].'</td><td>'.$ydate.'</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' => 4, 'name' => '待收货确认'),
            array('value' => '5,6', '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('factory_order', 'store');

    	# 确认收货
    	$data['action'] = Dever::url('admin/factory_order.action_commit', 'store');
    	return $data;
    }

    # 确认收货
    public function action_commit_api()
    {
    	$order_id = Dever::input('order_id');

        Dever::load('mshop/lib/buy')->set(2,1)->finish($this->id, $order_id);

    	return 'reload';
    }
}