<?php

namespace Mshop\Src;

use Dever;
use Mshop\Lib\Core;

class Buy extends Core
{
    # 获取所有商品列表
    public function getGoods()
    {
        Dever::config('base')->buy = 1;
        $this->data['data'] = Dever::load('shop/lib/info')->getGoods($this->shop_id, false, '1,2,3', false, 1);

        return $this->data;

        return Dever::load('mshop/out')->getGoods();
        $where = array();

        $name = Dever::input('name');
        if ($name) {
            $where['name'] = $name;
        }
        $this->data['data'] = Dever::db('goods/info')->getPageAll($where);
        if ($this->data['data']) {
            foreach ($this->data['data'] as $k => $v) {
                $this->data['data'][$k] = Dever::load('shop/lib/info')->getGoodsInfo($this->shop_id, $v);
            }
        }

        return $this->data;
    }

    # 采购订单列表
    public function getList()
    {
        return Dever::load('mshop/lib/buy')->set(1,1)->getList($this->shop_id);
    }

    # 查看订单详情
    public function view()
    {
        $order_id = Dever::input('order_id');

        return Dever::load('mshop/lib/buy')->set(1,2)->getView($this->shop_id, $order_id);
    }

    # 确认订单页面
    public function confirm()
    {
        //$this->data['user'] = $this->user;
        $this->goods();

        return $this->data;
    }

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

        $sku_id = Dever::input('price_id');
        if ($sku_id) {
            $sku_id = explode(',', $sku_id);
        }
        
        $num = Dever::input('num');
        if (!$num) {
            Dever::alert('请传入商品数量');
        }
        $num = explode(',', $num);
        #新加：
        $method = Dever::input('method');#1是资金账户抵扣

        $this->data['price'] = 0;
        $this->data['num'] = 0;
        $this->data['name'] = array();
        $count = count($goods_id);
        # 计算总价格
        foreach ($goods_id as $k => $v) {
            $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
            $n = isset($num[$k]) ? $num[$k] : 1;
            $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, 2);

            $w = array();
            $w['goods_id'] = $v;
            $w['sku_id'] = $s;
            $w['shop_id'] = $this->shop_id;
            $goods_sku = Dever::db('shop/goods_sku')->find($w);
            if ($goods_sku) {
                if (isset($goods_sku['price_id']) && $goods_sku['price_id'] > 0) {
                    $price_template = Dever::load('price/lib/data')->get($v, $s, $goods_sku['price_id']);
                    if ($price_template) {
                        if ($price_template['price_num'] > 0) {
                            $this->data['list'][$k]['min'] = $price_template['price_num'];
                        }
                        if ($price_template['price_buy'] && $price_template['price_buy'] > 0) {
                            $this->data['list'][$k]['price'] = $price_template['price_buy'];
                        }
                    }
                }
            }

            if (isset($this->data['list'][$k]['min']) && $this->data['list'][$k]['min'] > 0) {
                if ($n < $this->data['list'][$k]['min']) {
                    $n = $this->data['list'][$k]['min'];
                }
            }

            $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['wallet'] =array();
        if($method == 1){
            $this->data['wallet']['method'] = $method;
            if($this->shop && $this->shop['price']){
                if($this->shop['price']  >= $this->data['price']){
                    $this->data['wallet']['type'] = 1;
                    #花的钱
                    $this->data['wallet']['price'] = round($this->data['price'],2);
                    $this->data['price'] = 0;
                    #余额
                    $this->data['wallet']['yue'] = round($this->shop['price'] - $this->data['wallet']['price'],2);
                } else {
                    $this->data['wallet']['type'] = 2;
                    $this->data['wallet']['price'] = $this->shop['price'];
                    $this->data['price'] = round($this->data['price'] - $this->shop['price'],2);
                    $this->data['wallet']['yue'] = 0;
                }
            }
        }else{
            $this->data['wallet']['yue'] = $this->shop['price'];
        }
        $this->data['name'] = implode(',', $this->data['name']);
    }

    # 开始下单
    public function pay_commit()
    {
        $refer = Dever::input('refer');
        
        $info = Dever::input('info');

        $this->goods();

        $pay = Dever::load('mshop/lib/buy')->set(1,1)->action($this->passport_uid, 1, $this->shop_id, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $info, $refer, $this->shop['area'],$this->data['wallet']);
        return $pay;
    }

    # 再次付款
    public function r_pay_commit()
    {
        $refer = Dever::input('refer');
        $order_id = Dever::input('order_id');

        $pay = Dever::load('mshop/lib/buy')->set(1,2)->raction($order_id, $refer);

        return $pay;
    }

    # 查询是否支付
    public function c_pay()
    {
        $refer = Dever::input('refer');
        $order_id = Dever::input('order_id');

        $pay = Dever::load('mshop/lib/buy')->cpay($order_id, 5, $refer);

        return $pay;
    }

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

        return Dever::load('mshop/lib/buy')->set(1,1)->cancel($this->shop_id, $order_id);
    }

    # 获取订单退款记录
    public function order_tui_log()
    {
        $order_id = Dever::input('order_id');

        return Dever::load('shop/lib/refund')->set('buy')->getList(1, $this->shop_id, $order_id);
    }

    # 货物未发出，仅退款
    public function order_refund_commit()
    {
        $order_id = Dever::input('order_id');

        return Dever::load('shop/lib/refund')->set('buy')->apply(1, $this->shop_id, $order_id, false, 3, 0, '门店退款', '', 2);
    }

    # 货物已收到 全部退款 报损 需要审核
    public function order_tui_commit()
    {
        $order_id = Dever::input('order_id');
        $status = Dever::input('status');
        $desc = Dever::input('desc');
        $pic = Dever::input('pic');

        return Dever::load('shop/lib/refund')->set('buy')->apply(1, $this->shop_id, $order_id, false, $status, 0, $desc, $pic, 1);
    }

    # 部分退款信息 报损
    public function order_tui_one_info()
    {
        $order_id = Dever::input('order_id');
        $order_goods_id = Dever::input('order_goods_id');

        return Dever::load('shop/lib/refund')->set('buy')->getInfo(1, $this->shop_id, $order_id, $order_goods_id);
    }

    # 部分退款 报损
    public function order_tui_one_commit()
    {
        $order_id = Dever::input('order_id');
        $order_goods_id = Dever::input('order_goods_id');
        $num = Dever::input('num', 0);
        $status = Dever::input('status');
        $desc = Dever::input('desc');
        $pic = Dever::input('pic');

        return Dever::load('shop/lib/refund')->set('buy')->apply(1, $this->shop_id, $order_id, $order_goods_id, $status, $num, $desc, $pic, 1);
    }

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

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

    # 打印
    public function printer()
    {
        $id = Dever::input('printer_id');
        if (!$id) {
            Dever::alert('请选择打印机');
        }
        $where['id'] = $id;
        $where['shop_id'] = $this->shop_id;
        $info = Dever::db('shop/print')->find($where);
        if (!$info) {
            Dever::alert('请选择打印机');
        }

        $order = $this->view();

        $manage_config = Dever::db('main/manage_config')->find();
        $sell_config = Dever::db('main/sell_config')->find();
        $main_config = Dever::db('main/config')->find();
        $config = Dever::db('shop/buy_order')->config;

        $user = $this->user;

        $left = 70;
        $right = 30;
        $head = array('名称', '数量', '单价', $left, $right);
        $goods = array();
        $tgoods = array();
        foreach ($order['goods'] as $k => $v) {
            $sku = isset($v['info']['sku_name']) ? '-' . str_replace(',', '', $v['info']['sku_name']) : '';
            $d  = array
            (
                $v['info']['name'] . $sku,
                $v['num'],
                $v['price'],
                $left,
                $right
            );
            $goods[] = $d;
            if ($v['status'] == 3) {
                $tgoods[] = $d;
            }
        }

        $print = Dever::load('mshop/lib/feieyun');

        $print->hr('-', $main_config['name'])->br();
        $print->text('单号：' . $order['order_num'], 'CB')->br();
        $print->text('门店名称：' . $order['type_info']['name']);
        $print->text('制 单 人：'.$user['name']);
        $print->text('下单时间：'.$order['cdate']);
        if ($order['fdate']) {
            $print->text('完成时间：'.$order['fdate']);
        }
        
        if ($goods) {
            $print->table($head, $goods, '订单商品');
        }
        
        if (isset($order['ps_cash']) && $order['ps_cash']) {
            $print->lr('配送费：', $order['ps_cash']);
        }
        //$print->lr('应    付：', $order['price']);
        //$print->hr();
        $print->lr('微信支付：', $order['price']);
        
        if ($tgoods) {
            $print->table($head, $tgoods, '退款商品');
            $print->lr('退款金额：', $order['coupon_cash']);
        }

        $print->hr();

        if (isset($order['source_info']) && $order['source_info']) {
            $print->text('供应商');
            $print->text($order['source_info']['name'] . '，' . $order['source_info']['mobile']);

            $print->hr();
        }


        if (isset($order['type_info']) && $order['type_info']) {
            $print->text('配送地址');
            $print->text($order['type_info']['truename'] . '，' . $order['type_info']['mobile']);

            $area = Dever::load("area/api.string", $order['type_info']['area'], '');
            $print->text($order['type_info']['address']);
            $print->hr();
        }

        if ($order['info']) {
            $print->lr('备    注：', $order['info']);

            $print->hr();
        }
        
        $print->center('如遇任何问题请致电客服');
        $print->center('电话：'.$manage_config['phone']);
        $print->center($main_config['worktime']);
        //$content .= '<QR>http://www.feieyun.com</QR>';//把二维码字符串用标签套上即可自动生成二维码

        $content = $print->getText();
        echo $content;die;
        $print->printOrder($info['number'], $content, 1);

        return 'ok';
    }
}