<?php
namespace app\admin\controller;

use think\facade\Session;
use app\admin\BaseController;
use app\common\model\MallCate as MallCateModel;
use app\common\model\MallGoods as MallGoodsModel;
use app\common\model\MallOrder as MallOrderModel;

use app\common\model\User as UserModel;
use app\common\model\UserIntegral as UserIntegralModel;

use PHPExcel_IOFactory;
use PHPExcel;

class Mall extends BaseController
{
    
    // 商品
    public function orderList()
    {
        return view('mall/orderlist', []);
    }
    
    public function orderForm()
    {
        $id = input('id/d, 0');
        $order = MallOrderModel::findOrEmpty($id);
        if ($order->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "订单信息不存在"
            )));
        }
        return view('mall/orderform', [
            'order'		=> $order
        ]);
    }
    
    public function listOrder()
    {
        $limit = input('limit');
        $page = input('page');
        $map = array();
        $keywords = input('keywords/s');
        if (!empty($keywords)) {
            $map[] =['ordersn|title', 'like', '%'.$keywords.'%'];
        }
        $goodsid = input('goodsid/d');
        if (!empty($goodsid)) {
            $map[] = ['goodsid', '=', $goodsid];
        }
        $status = input('status/d');
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $list = MallOrderModel::with('user', 'mallGoods')->where($map)->order(['id'=>'desc'])->limit($limit)->page($page)->select()->append(['status_text']);
        $count = MallOrderModel::where($map)->count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }
    
    public function exportOrder()
    {
        $map = array();
        $keywords = input('keywords/s');
        if (!empty($keywords)) {
            $map[] =['ordersn|title', 'like', '%'.$keywords.'%'];
        }
        $goodsid = input('goodsid/d');
        if (!empty($goodsid)) {
            $map[] = ['goodsid', '=', $goodsid];
        }
        $status = input('status/d');
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $xlsData = MallOrderModel::with('user', 'mallGoods')->where($map)->order(['id'=>'desc'])->select()->append(['status_text'])->toArray();
        $xlsCell = array(
            array('id','表ID'),
            array('ordersn','订单编号'),
            array('user.nickname','用户昵称'),
            array('user.mobile','用户手机号'),
            array('goodsid','商品ID'),
            array('title','商品名称'),
            array('gintegral','单量积分'),
            array('gpaymoney','单量金额'),
            array('buynumber','兑购量'),
            
            array('integral','订单积分'),
            array('paymoney','订单金额'),
            array('status_text','订单状态'),
            
            array('username','收货人姓名'),
            array('usermobile', '收货人手机号'),
            array('province', '省'),
            array('city', '市'),
            array('county', '区县'),
            array('detailinfo', '详细地址'),
            array('remark', '用户备注'),
            array('createtime', '下单时间')
        );
        export_excel("积分商城订单", $xlsCell, $xlsData);
    }
    
    public function delOrder()
    {
        $idarr = input('idarr/a');
        $info = MallOrderModel::whereIn('id', $idarr)->select();
        $info->delete();
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> ""
        )));
    }
    
    public function editOrder()
    {
        $id = input('id/d', 0);
        $order = MallOrderModel::findOrEmpty($id);
        if ($order->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "订单信息不存在"
            )));
        }
        $data = array(
            'id'			=> $id,
            'ordersn'		=> input('ordersn/s', ""),
            'title'			=> input('title/s', ""),
            'tilpic'		=> input('tilpic/s', ""),
            'gintegral'		=> input('gintegral/d', 0),
            'gpaymoney'		=> input('gpaymoney/f', 0.00),
            'buynumber'		=> input('buynumber/d', 0),
            
            'integral'		=> input('integral/d', 0),
            'paymoney'		=> input('paymoney/f', 0.00),
            'status'		=> input('status/d', 1),
            
            'username'		=> input('username/s', ""),
            'usermobile'	=> input('usermobile/s', ""),
            'province'		=> input('province/s', ""),
            'city'			=> input('city/s', ""),
            'county'		=> input('county/s', ""),
            'detailinfo'	=> input('detailinfo/s', ""),
            'remark'		=> input('remark/s', "")
        );
        MallOrderModel::update($data);
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function fieldOrder()
    {
        $id = input('id/d', 0);
        $info = MallOrderModel::findOrEmpty($id);
        if ($info->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "信息不存在"
            )));
        } else {
            $info->save([
                input('field/s')  => input('value')
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    
    
    
    // 商品
    public function goodsList()
    {
        $catelist = MallCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
        ;
        return view('mall/goodslist', [
            'catelist'	=> $catelist
        ]);
    }
    
    public function goodsForm()
    {
        $catelist = MallCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
        $id = input('id/d, 0');
        $goods = MallGoodsModel::findOrEmpty($id);
        return view('mall/goodsform', [
            'catelist'	=> $catelist,
            'goods'		=> $goods
        ]);
    }
    
    public function listGoods()
    {
        $limit = input('limit');
        $page = input('page');
        $map = array();
        $keywords = input('keywords/s');
        if (!empty($keywords)) {
            $map[] =['title', 'like', '%'.$keywords.'%'];
        }
        $cateid = input('cateid/d');
        if (!empty($cateid)) {
            $map[] = ['cateid', '=', $cateid];
        }
        $status = input('status/d');
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $list = MallGoodsModel::with('mallCate')->withCount(['mallCate','mallOrder'])->where($map)->order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->select()->append(['status_text']);
        $count = MallGoodsModel::where($map)->count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }
    
    public function delGoods()
    {
        $idarr = input('idarr/a');
        $info = MallGoodsModel::whereIn('id', $idarr)->select();
        $info->delete();
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> ""
        )));
    }
    
    public function editGoods()
    {
        $id = input('id/d');
        $data = array(
            'cateid'		=> input('cateid/d', 0),
            'title'			=> input('title/s', ""),
            'tilpic'		=> input('tilpic/s', ""),
            'picall'		=> input('picall/a', array()),
            'stock'			=> input('stock/d', 0),
            'sales'			=> input('sales/d', 0),
            'integral'		=> input('integral/d', 0),
            'paymoney'		=> input('paymoney/f', 0.00),
            'summary'		=> input('summary/s', ""),
            'oremark'		=> input('oremark/s', ""),
            'details'		=> input('details/s', ""),
            'priority'		=> input('priority/d', 0),
            'createtime'	=> input('createtime/s', ""),
            'status'		=> input('status/d', 1)
        );
        if (empty($id)) {
            MallGoodsModel::create($data);
        } else {
            $data['id'] = $id;
            MallGoodsModel::update($data);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function fieldGoods()
    {
        $id = input('id/d', 0);
        $goods = MallGoodsModel::findOrEmpty($id);
        if ($goods->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "信息不存在"
            )));
        } else {
            $goods->save([
                input('field/s')  => input('value')
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }


    // 分类
    public function cateList()
    {
        return view('mall/catelist');
    }
    
    public function cateForm()
    {
        $id = input('id/d, 0');
        $cate = MallCateModel::findOrEmpty($id);
        return view('mall/cateform', [
            'cate'	=> $cate
        ]);
    }
    
    public function listCate()
    {
        $limit = input('limit');
        $page = input('page');
        $list = MallCateModel::order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->select()->append(['status_text']);
        $count = MallCateModel::count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }
    
    public function fieldCate()
    {
        $id = input('id/d');
        $cate = MallCateModel::find($id);
        if ($cate==null) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "分类信息不存在"
            )));
        } else {
            $cate->save([
                input('field/s')  => input('value/d')
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function editCate()
    {
        $id = input('id/d');
        if (empty($id)) {
            $cate = MallCateModel::create([
                'title' 	=> input('title/s'),
                'status'    => input('status/d')==1 ? 1 : 2,
                'priority'  => input('priority/d')
            ]);
        } else {
            $cate = MallCateModel::find($id);
            $cate->save([
                'title' 	=> input('title/s'),
                'status'    => input('status/d')==1 ? 1 : 2,
                'priority'  => input('priority/d')
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function delCate()
    {
        $id = input('id/d');
        $cate = MallCateModel::where('id', $id)->select();
        $result = $cate->delete();
        if ($result) {
            exit(json_encode(array(
                'code' 	=> 0,
                'msg'	=> ""
            )));
        }
        exit(json_encode(array(
            'code' 	=> 1,
            'msg'	=> "删除失败，请稍后重试"
        )));
    }
}
