<?php

namespace app\admin\controller;
use think\Db;
use app\admin\logic\{
    LeaveLogic
};
use app\admin\logic\StaffLogic;
class Leave extends AdminBase {
    /**
     * 员工请假列表
     * @return mixed
     */
    public function lists(){
        if ($this->request->isAjax()) {
            $get = $this->request->get();
            $this->_success('',LeaveLogic::lists($get));
        }
        $this->assign('type',LeaveLogic::type());
       
        return $this->fetch();
    }
    /**
     * 增加请假删除
     * @return mixed
     */
    public function add(){
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if($post['privilege']==''){
                $this->_error('保洁师不能为空');
            }
            //dump($post);
            $start=strtotime($post['start_time']);
            $end=strtotime($post['end_time']);
            $lent=Db::name('leave')->where('user_id',$post['privilege'])->where('time','>=',$start)->where('time','<=',$end)->find();
            if($lent){
                $this->_error('占用数据重复');
            }else{
                $order=Db::name('order_exe')->where('user_id',$post['privilege'])
                ->where('autotime','>=',$start)
                ->where('autotime','<=', $end)->find();
                if($order){
                   $this->_error('日期已经存在订单');  
                }else{
                     $post['admin_id']=$this->admin_id;
                     $this->_success('请假成功等待审核',LeaveLogic::add($post));
                }
               
            }
        }
        $this->assign('satff',LeaveLogic::user());
        $this->assign('type',LeaveLogic::type());       
        return $this->fetch();
    }

    /**
     * 编辑文章分类
     * @return mixed
     */
    public function del($id){  
         $del=Db::name('leave')->where('id',$id)->delete();
         if($del){
            $this->_success('删除请假成功');
         }else{$this->_error('请假已经通过审核无法删除');}
    }

    /**
     * 删除请假
     * @return mixed
     */
    public function edit($id)
    {
        if ($this->request->isAjax()) {
            // $result = $this->validate(['id' => $id], 'app\admin\validate\ArticleCategory.del');
            // if ($result === true) {
            //     ArticleCategoryLogic::delArticleCategory($id);
            //     $this->_success('删除成功');
            // }
            // $this->_error($result);
        }
        $this->assign('type',LeaveLogic::type());       
        $this->assign('detail',LeaveLogic::info($id));
        return $this->fetch();
        
    }


    /**
     * 修改状态
     */
    public function Status($id){
        $updata=Db::name('leave')->where('id',$id)->update(['status'=>1]);
        $this->_success('审核通过');
    }

    public function nostatus($id){
        $updata=Db::name('leave')->where('id',$id)->update(['status'=>2]);
        $this->_success('审核不通过');
    }
    /**
     * 保洁师请假列表
     */
    public function staff_leave(){
       if ($this->request->isAjax()) {
            $get = $this->request->get();
            $this->_success('获取数据成功',LeaveLogic::staff_leave($get));
        }
        
        $this->assign('type',LeaveLogic::leave_statype());   
        $this->assign('staff',StaffLogic::stafflist());
        return $this->fetch();  
    }
    
    /**
     * 审核请假通过
     * 
     */
     public function passed(){
          $post = $this->request->post();
          $data=Db::name('leavesd')->where('id',$post['id'])->find();       //获取订单的基本信息
            if($data['addtime']==0){  //判断上午订单
               $ord=Db::name('order_exe')
               ->where('staff_id',$data['staff_id'])
               ->where('autotime',$data['time'])
               ->where('addtime',1)
               ->find();
               if($ord){
                  $this->_error('已有订单占用');  
               }
            }
            if($data['addtime']==1){  //判断上午订单
               $ord=Db::name('order_exe')
               ->where('staff_id',$data['staff_id'])
               ->where('autotime',$data['time'])
               ->where('addtime',2)
               ->find();
               if($ord){
                  $this->_error('已有订单占用');  
               }
            }
            if($data['addtime']==2){
               $date_day=date('Y-m-d',$data['time']);//"2019-10-22"
               $beginDay_ts=strtotime($date_day);//1571673600
           
               $endDay_ts=$beginDay_ts+86400-1;
                $ord=Db::name('order_exe')
                ->where('staff_id',$data['staff_id'])
                ->where('autotime','>=',$data['time'])
                ->where('autotime','<=',$endDay_ts)
                ->select(); 
                if($ord){
                  $this->_error('已有订单占用');  
                 }
            }
            Db::name('leavesd')->where('id',$post['id'])->update(['status'=>1]);
            $this->_success('请假数据审批成功');

     }
    
    /**
     * 拒绝通过请假的获取内容
     * 
     */
     public function refuse(){
        $post = $this->request->get();
        $lists = LeaveLogic::refuse($post['id']);
        $this->_success('',$lists);
     }
     
    /**
     * 更新请假的内容
     * 
     */
     public function refusesd(){
        $post = $this->request->post();
        $lists = LeaveLogic::refusesd($post);
       
        $this->_success('修改数据成功');
     }
    /**
     * 删除请假的数据
     * 
     */
     
     public function dels($id){
        $del=Db::name('leavesd')->where('id',$id)->delete();
         if($del){
            $this->_success('删除请假成功');
         }else{$this->_error('请假已经通过审核无法删除');}
     }
     
    
}