<?php
namespace store\Admin;

use Dever;

class Out extends Core
{
    public function __construct()
    {
        Dever::setInput('search_option_type', 2);
        parent::__construct();
    }
    # 订单列表
    public function getList()
    {
        $data = Dever::load('mshop/lib/out')->set(2, 1)->getList($this->id);

        $html = '';
        $excel = Dever::input('excel', 1);
        if ($data && $data['order']) {
            if ($excel == 2) {
                Dever::setInput('show', 2);
                Dever::load('mshop/lib/out')->set(2, 1)->excel($data['order']);
                die;
                return;
            }
            foreach ($data['order'] as $k => $v) {
                $view = Dever::url('out_view?order_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'] == 1) {
                    $url = Dever::url('admin/out.cancel&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>';
                }

                $url = Dever::url('lib/out.excel_one?show=2&type='.$v['type'].'&id='.$v['id'], 'mshop');
                $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>';

                $html .= '<tr><td>'.$v['order_num'].'</td><td>'.$v['cdate'].'</td><td>'.$v['type_name'].'</td><td>'.$v['num'].'</td><td>'.$v['status_name'].'</td><td>'.$button.'</td></tr>';
            }
        }
        return $html;
    }

    # 获取列表页搜索信息
    public function getStatus()
    {
        $type = Dever::db('shop/out_order')->config['config_status'];
        $config = array
        (
            array('value' => '0', 'name' => '全部')
        );

        foreach ($type as $k => $v) {
            $config[] = array('value' => $k, 'name' => $v);
        }

        $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 getType($state = 1)
    {
        $type = Dever::db('shop/out_order')->config['config_type'];
        $type = $type();
        $config = array
        (
            array('value' => '0', 'name' => '全部'),
        );

        foreach ($type as $k => $v) {
            if ($v['type'] != 1) {
                $config[] = array('value' => $k, 'name' => $v['name']);
            }
        }

        if ($state == 2) {
            unset($config[0]);
        }

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

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

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

        return $html;
    }

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

        # 取消
        $data['cancel'] = Dever::url('admin/out.cancel', 'store');

        # 新增出库单
        $data['add'] = Dever::url('admin/out.add', 'store');
        return $data;
    }

    # 取消
    public function cancel_api()
    {
        $order_id = Dever::input('order_id');

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

        return 'reload';
    }

    # 获取商品列表
    public function getGoods_api()
    {
        $store_id = $this->id;
        return Dever::outDiy(Dever::load('store/lib/goods')->getSkuList($store_id));
    }

    # 设置生产能力
    public function add_api()
    {
        $data = array();
        $data['store_id'] = $this->id;
        $data['host'] = Dever::url('admin/out.add', 'store');
        $data['url'] = Dever::url('admin/out.getGoods', 'store');
        $data['submit'] = Dever::url('admin/out.action_commit?json=1', 'store');
        $data['out_type'] = $this->getType(2);
        $data['user'] = $this->user['name'];
        return Dever::render('setGoods', $data);
    }

    # 得到商品和总价
    private function goods()
    {
        $goods = Dever::input('goods');
        if (!$goods) {
            Dever::alert('请传入商品');
        }
        $goods = Dever::json_decode($goods);

        $this->data['price'] = 0;
        $this->data['num'] = 0;
        $this->data['name'] = array();
        $store_id = $this->id;
        # 计算总价格
        foreach ($goods as $k => $v) {
            if (strstr($v['id'], '-')) {
                $t = explode('-', $v['id']);
                $v['id'] = $t[0];
                $s = $t[1];
            } else {
                $s = -1;
            }
            $n = $v['num'];

            $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v['id'], $s, 2);
            $this->data['list'][$k]['num'] = $n;

            # 2是库存不足
            $this->data['list'][$k]['ku_state'] = 1;

            # 验证是否有货
            $total = Dever::load('store/lib/goods')->checkTotal($n, $v['id'], $store_id, $s);

            if ($total <= 0) {
                $this->data['list'][$k]['ku_state'] = 2;
                $this->data['list'][$k]['buy_num'] = 0;
            }

            if ($this->data['list'][$k]['ku_state'] == 1) {
                $this->data['list'][$k]['buy_num'] = $n;
                $this->data['num'] += $n;
                $this->data['price'] += $this->data['list'][$k]['price'] * $n;
                $this->data['name'][] = $this->data['list'][$k]['name'];
            }
        }

        $this->data['name'] = implode(',', $this->data['name']);
    }

    # 生成出库单
    public function action_commit_api()
    {
        $info = Dever::input('info');

        $out_type = Dever::input('out_type', 1);

        $this->goods();

        Dever::load('mshop/lib/out')->set(2,1)->action($this->id, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $this->user['id'], $out_type, $info, 1);

        return Dever::url('out', 'store');
    }

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