<?php
namespace store\Admin;

use Dever;

class Goods extends Core
{
	# 订单列表
    public function getList()
    {
        $data = Dever::load('store/lib/goods')->getGoodsSku($this->id);

        $html = '';
        if ($data) {
        	foreach ($data as $k => $v) {
                $v['cover'] = '<img src="'.$v['cover'].'" width="50" />';
                $v['sku_name'] = isset($v['sku_name']) ? $v['sku_name'] : '无';
        		$html .= '<tr><td>'.$v['cover'].'</td><td>'.$v['name'].'</td><td>'.$v['sku_name'].'</td><td>'.$v['total'].'</td></tr>';
        	}
        }
        return $html;
    }

    # 获取列表页搜索信息
    public function getSearch()
    {
    	$config = array
        (
        	array('name' => '全部', 'value' => ''),
    		array('name' => '库存<3件', 'value' => '3'),
    		array('name' => '库存<5件', 'value' => '5'),
    		array('name' => '库存<10件', 'value' => '10'),
    		array('name' => '无库存', 'value' => '1'),
        );

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

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

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

        return $html;
    }

    # 获取基本信息
    public function getConfig()
    {
    	$data['search'] = Dever::url('goods', 'store');
        $data['print'] = Dever::url('admin/goods.print', 'store');
    	return $data;
    }

    # 打印订单单
    public function print_api()
    {
        $factory_config = Dever::db('main/factory_config')->find();
        $main_config = Dever::db('main/config')->find();
        $config = Dever::db('main/config')->find();
        $data = Dever::load('store/lib/goods')->getGoodsSku($this->id);
        $store = Dever::db('store/info')->find($this->id);


        $pdf = Dever::load('pdf/lib/base')->init();

        $pdf->hr('-', $config['name']);

        $pdf->br()->font(20)->center('库存清单');
        $pdf->font(10);
        $pdf->br(2);

        $pdf->br()->left('仓库名称：' . $store['name'], 80)->left('制单人：' . $this->user['name'], 60)->left('制单时间：' . date('Y-m-d H:i'), 40);

        $pdf->hr();

        $head = array(array('商品编号', 40), array('商品名称', 70), array('商品属性', 60), array('数量', 20));

        if ($data) {
            $body = array();
            $body_total = array();
            $body_total['num'] = 0;

            foreach ($data as $k => $v) {

                $v['sku_name'] = isset($v['sku_name']) ? $v['sku_name'] : '无';
                $body[] = array
                (
                    $v['id'],
                    $v['name'],
                    $v['sku_name'],
                    'x ' . $v['total'],
                );

                $body_total['num'] += $v['total'];
            }

            $pdf->br();
            foreach ($head as $k => $v) {
                $pdf->left($v[0], $v[1]);
            }

            foreach ($body as $k => $v) {
                $pdf->br();
                foreach ($head as $k1 => $v1) {
                    $pdf->left($v[$k1], $v1[1]);
                }
            }
            $pdf->br();
            $pdf->right('共'.$body_total['num'].'件商品');
            $pdf->hr();
        }

        $pdf->br(1);
        
        $pdf->right('如遇任何问题请致电客服');
        $pdf->br();

        $pdf->font(20);
        $pdf->left($main_config['name'], 160);

        $pdf->font(10);
        $pdf->right('电话：' . $factory_config['phone'], 30);

        $pdf->br();

        $pdf->left($main_config['site'], 160);

        $pdf->font(10);
        $pdf->right($main_config['worktime'], 30);

        $pdf->out('库存清单');
    }
}