<?php
namespace app\admin\controller;

use app\common\model\OrderLog;
use app\admin\logic\OrderLogic;
use app\admin\logic\StatcLogic;
use app\admin\logic\UserLogic;
use app\admin\logic\OrderchanelLogic;
use app\admin\logic\StaffLogic;
use app\common\model\Client_;
use app\common\model\Order as CommonOrder;
use app\common\model\Pay;
use think\Db;

class Order extends AdminBase
{
    /**
     * User: 意象信息科技 mjf
     * Desc: 订单列表
     */
    public function lists()
    {
        if ($this->request->isAjax()) {
            $get = $this->request->get();
            $this->_success('', OrderLogic::lists($get));
        }
        $this->assign('order_status', CommonOrder::getOrderStatus(true));
        $this->assign('order_type', CommonOrder::getOrderType(true));
        $this->assign('pay_way', Pay::getPayWay(true));
        $this->assign('delivery_type', CommonOrder::getDeliveryType(true));
        $this->assign('order_source', Client_::getClient(true));
        $ceenl=Db::name('orderchannel')->select();
        $goods=Db::name('goods')->select();
        $this->assign('ceenl',$ceenl);
        $this->assign('goods',$goods);
        $admin=Db::name('admin')->select();
        $this->assign('admin',$admin);
        $this->assign('num', StatcLogic::Numorder());
        return $this->fetch();
    }
   
    public function exportFile()
    {
        $get = $this->request->get();
        $this->_success('', OrderLogic::exportorder($get));
    }

    public function exportFilestaff()
    {
        $get = $this->request->get();
        $this->_success('', OrderLogic::exportFilestaff($get));
    }

    /**
     * User: 杨左
     * Desc: 用户订单导出功能
     */
    public function exportFilesuser()
    {
        $get = $this->request->get();
        $this->_success('', OrderLogic::userexport($get));
    }

    /**
     * User: 意象信息科技 mjf
     * Desc: 订单详情
     */
    public function detail()
    {
        $id = $this->request->get('id');
        $detail = OrderLogic::getDetail($id);
        $this->assign('detail', $detail);
      //  $this->assign('logs', OrderLog::getOrderLog($id));
        return $this->fetch();
    }

    /**
     * User: 意象信息科技 mjf
     * Desc: 取消订单
     */
    public function cancel()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\Order.cancel');
            if (true !== $check) {
                $this->_error($check);
            }
            $res = OrderLogic::cancel($post['order_id'], $this->admin_id);
            if ($res === true) {
                $this->_success('取消成功');
            }
            $this->_error($res);
        }
    }

    /**
     * User: 意象信息科技 mjf
     * Desc: 删除订单
     */
    public function del()
    {
   
        if ($this->request->isAjax()) {
           dump(123);die;
            // $post = $this->request->post('');
            // $order=Db::name('order')->where('id',$post['order_id'])->find();//查询订单信息
    
            // if($this->admin_id!=6){
            //     $this->_error('无权限删除');   
            // }else{
            //     $orderdel=Db::name('order_exe')->where('order_sn',$order['order_sn'])->find();
            //     if($orderdel){
            //       $this->_error('无法删除,存在主订单');  
            //     }else{
            //      $del=Db::name('order')->where('id',$post['order_id'])->delete();
            //      $this->_success('删除成功');   
            //     }
   
            // }
        

        }
    }

    /**
     * User: 意象信息科技 mjf
     * Desc: 发货
     */
    public function delivery()
    {
        $id = $this->request->get('id');
        $detail = OrderLogic::getDetail($id);
        $this->assign('detail', $detail);
        $this->assign('express', OrderLogic::express());
        return $this->fetch();
    }

    /**
     * User: 意象信息科技 mjf
     * Desc: 发货操作
     */
    public function deliveryHandle()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\Order.delivery');
            if (true !== $check) {
                $this->_error($check);
            }
            OrderLogic::deliveryHandle($post, $this->admin_id);
            $this->_success('发货成功');
        }
    }

    /**
     * User: 意象信息科技 mjf
     * Desc: 确认收货
     */
    public function confirm()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\Order.confirm');
            if (true !== $check) {
                $this->_error($check);
            }
            OrderLogic::confirm($post['order_id'], $this->admin_id);
            $this->_success('确认成功');
        }
    }

    /**
     * User: 意象信息科技 mjf
     * Desc: 物流信息
     */
    public function express()
    {
        $id = $this->request->get('id');
        $detail = OrderLogic::getDetail($id);
        $detail['shipping'] = OrderLogic::shippingInfo($detail['id']);
        $this->assign('detail', $detail);
        return $this->fetch();
    }

    public function remarks()
    {
        // 获取的
        if ($this->request->isAjax() && $this->request->isGet()){
            $get = $this->request->get();
            $detail = OrderLogic::remarks($get, 'get');
            $this->_success('获取成功', $detail);
        }

        // 提交的
        if ($this->request->isAjax() && $this->request->isPost()) {
            $post = $this->request->post();
            $result = OrderLogic::remarks($post, 'post');
            if ($result) {
                $this->_success('保存成功');
            }
            $this->error('保存失败');
        }

    }

    /**
     * Notes:打印接口
     */
    public function orderPrint(){
        $id = $this->request->post('id');
        $result = $this->validate(['id'=>$id],'app\admin\validate\OrderPrint');
        if(true === $result){
            $result = OrderLogic::orderPrint($id);
            if(true === $result){
                $this->success('打印机成功，如未出小票，请检查打印机是否在线');
            }
        }
        $this->_error($result);
    }

    /**
     * Notes:增加客户订单
    */
     public  function add(){
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if($post['goods_id']==''){
                $this->_error('服务套餐不能为空');
            }else if($post['user_id']==''){
                $this->_error('请选择用户');
            }
           $list=OrderLogic::add($post);
           $this->_success('增加订单成功', $list);
        }
        $this->assign('ordertype', OrderchanelLogic::infolist(true));
        $this->assign('user', UserLogic::userlist(true));
        $collection=Db::name('collection')->select();
        $this->assign('collection',$collection);

        return $this->fetch();
     }
     
     public function remarksd(){  
     }


     public function userorder(){
        if ($this->request->isAjax()) {
            $get = $this->request->get();
            $this->_success('', OrderLogic::userlists($get));
        }
        $this->assign('order_status', CommonOrder::getOrderStatus(true));
        $this->assign('order_type', CommonOrder::getOrderType(true));
        $this->assign('pay_way', Pay::getPayWay(true));
        $this->assign('delivery_type', CommonOrder::getDeliveryType(true));
        $this->assign('order_source', Client_::getClient(true));
        $ceenl=Db::name('orderchannel')->select();
        $this->assign('ceenl',$ceenl);
        $this->assign('num', StatcLogic::UserNumorder());
        return $this->fetch();
     }


     public function eidt($id){
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $this->_success('修改订单成功', OrderLogic::edit($post));
        }

       $this->assign('info', json_encode(OrderLogic::orderinfoid($id), JSON_UNESCAPED_UNICODE));
        $brand_lists=Db::name('orderchannel')->select();
        $this->assign('brand_lists', json_encode($brand_lists, JSON_UNESCAPED_UNICODE));
        $this->assign('category_lists', json_encode(StaffLogic::getAllTree(), JSON_UNESCAPED_UNICODE));
        $collection=Db::name('collection')->select();
        $this->assign('gord_id', json_encode($collection, JSON_UNESCAPED_UNICODE));
        return $this->fetch();
     }


    //后台操作划卡次数
     public function delnumber()
     {
         // 获取的
         if ($this->request->isAjax() && $this->request->isGet()){
             $get = $this->request->get();
             $detail = OrderLogic::delnumber($get, 'get');
             $this->_success('获取成功', $detail);
         }
 
         // 提交的
         if ($this->request->isAjax() && $this->request->isPost()) {
             $post = $this->request->post();
             $result = OrderLogic::delnumber($post, 'post');
                          $order=Db::name('order')->where('id',$post['id'])->find();
             $admin=$this->admin_id;
             $time=time();
             $logdate=[
                 'admin_id'=>$admin,
                 'order_sn'=>$order['order_sn'],
                 'show'=>'操作划卡次数',
                 'cram_time'=>$time
             ];
             $log=Db::name('log_order')->data($logdate)->insert();
             if ($result) {
                 $this->_success('保存成功');
             }
             $this->error('保存失败');
         }
 
     }
     
    //修改订单的分配人员
     public function allocation(){
        if ($this->request->isAjax()) {
            $id=$this->request->get('id');
            $post = $this->request->post();
            $admin_id=Db::name('order')->where('id',$id)->update(['admin_id'=>$post['admin_id']]);
            $this->_success('修改订单归属人成功');
        }
        $admin_user=Db::name('admin')->select();
        $this->assign('admin_user',$admin_user);
        return $this->fetch();
     }
     
    //修改订单渠道
    public function channel(){
         if ($this->request->isAjax()) {
              $post=$this->request->get();
              $post['channel_id']=$this->request->post('channel_id');
              $data=OrderLogic::channel_update($post);
              if($data){
                $this->_success('修改成功');
              }
              $this->error('保存失败');
         }
         $id=$this->request->get('id');
         $this->assign('channel',OrderchanelLogic::infolist());
         return $this->fetch();
    }

}