<?php
namespace app\admin\controller;

use think\App;
use think\Db;

class HouseLock extends Auth
{

    public function __construct(App $app = null) {
        parent::__construct($app);
        $this->table='house_lock';
        $this->model= new \app\common\model\HouseLock();
    }
    public function index(){
        if(request()->isAjax()){
            //分页参数
            $length = input('rows',10,'intval');   //每页条数
            $page = input('page',1,'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            //排序
            $sortRow = input('sidx','id','trim');      //排序列
            $sort = input('sord','desc','trim');        //排序方式
            $order = $sortRow.' '.$sort;


            $status = input('status','','trim');
            if($status != ''){
                $map[] = ['status','=',$status];
            }

            $b = input('start','','trim');
            $e = input('end','','trim');
            if($b){
                $b = date('Y-m-d 00:00:00',strtotime($b));
                $map[] = ['create_time','>=',$b];
            }
            if($e){
                $e = date('Y-m-d 23:59:59',strtotime($e));
                $map[] = ['create_time','<=',$e];
            }

            $map[] = ['org_id','=',$this->orgId];
            $map[] = ['user_id','=',$this->userId];
            $map= empty($map) ? true: $map;
            //数据查询
            $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
            foreach ($lists as $k=>$v){
                $lists[$k]['real_name'] = $this->getTableField('user',['id'=>$v['user_id']],'real_name');
                $lists[$k]['deal_real_name'] = $this->getTableField('user',['id'=>$v['user_id']],'real_name');
                $lists[$k]['cur_user_id'] = $this->userId;
            }
            //数据返回
            $totalCount = db($this->table)->where($map)->count();
            $totalPage = ceil($totalCount/$length);

            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $lists;
            return json($result);
        }else{

            return $this->fetch();
        }
    }

    public function add()
    {
        $start = input('start');
        $end =  input('end');
        if(!$start){
            // 获取开始时间,为空的没有数据
            $start = $this->model->getStartTime($this->userId);
            $end = date('Y-m-d H:i:s');
        }

        $data = $this->model->getDataInfo($start,$end,$this->userId,$this->orgId);
        $error = '';
        if(!$data){
            $error = $this->model->getError();
        }
        $this->assign('data',$data);
        $this->assign('error',$error);
        $this->assign('start',$start);
        $this->assign('end',$end);
        return $this->fetch();
    }
    public function addSave(){
        $start = input('start');
        $end = input('end');
        $remark = input('remark');
        $userId = $this->userId;
        $orgId = $this->orgId;

        $ret = $this->model->addSave($start,$end,$remark,$userId,$orgId);
        if(!$ret){
            $error = $this->model->getError();
            $this->error($error);
        }else{
            $this->success('操作成功');
        }
    }
    public function cancel($id){
        $ret = $this->model->deal($id,3,$this->userId);
        if(!$ret){
            $this->error($this->model->getError());
        }else{
            $this->success('操作是吧');
        }
    }
    public function detail($id)
    {
        $data = $this->model->getDataById($id);
        if(!$data){
            $this->error('记录不存在',url('HouseLock/index'));
        }
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function edit($id)
    {
        $data = $this->model->getDataById($id);
        if(!$data){
            $this->error('记录不存在',url('HouseLock/index'));
        }
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function apply(){
        if(request()->isAjax()){
            //分页参数
            $length = input('rows',10,'intval');   //每页条数
            $page = input('page',1,'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            //排序
            $sortRow = input('sidx','id','trim');      //排序列
            $sort = input('sord','desc','trim');        //排序方式
            $order = $sortRow.' '.$sort;


            $status = input('status','','trim');
            if($status != ''){
                $map[] = ['status','=',$status];
            }

            $b = input('start','','trim');
            $e = input('end','','trim');
            if($b){
                $b = date('Y-m-d 00:00:00',strtotime($b));
                $map[] = ['create_time','>=',$b];
            }
            if($e){
                $e = date('Y-m-d 23:59:59',strtotime($e));
                $map[] = ['create_time','<=',$e];
            }

            $map[] = ['org_id','=',$this->orgId];
            $map= empty($map) ? true: $map;
            //数据查询
            $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
            foreach ($lists as $k=>$v){
                $lists[$k]['real_name'] = $this->getTableField('user',['id'=>$v['user_id']],'real_name');
                $lists[$k]['deal_real_name'] = $this->getTableField('user',['id'=>$v['user_id']],'real_name');
            }
            //数据返回
            $totalCount = db($this->table)->where($map)->count();
            $totalPage = ceil($totalCount/$length);

            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $lists;
            return json($result);
        }else{

            return $this->fetch();
        }
    }

    public function detail2($id)
    {
        $data = $this->model->getDataById($id);
        if(!$data){
            $this->error('记录不存在',url('HouseLock/apply'));
        }
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function agree($id){
        $ret = $this->model->deal($id,1,$this->userId);
        if(!$ret){
            $this->error($this->model->getError());
        }else{
            $this->success('操作成功');
        }
    }

    public function disagree($id){
        $ret = $this->model->deal($id,2,$this->userId);
        if(!$ret){
            $this->error($this->model->getError());
        }else{
            $this->success('操作成功');
        }
    }
    public function detail2print($id){
        $data = $this->model->getDataById($id);
        if(!$data){
            $this->error('记录不存在',url('HouseLock/apply'));
        }

        $data['count_logs'] = count($data['logs']);
        $this->assign('data',$data);
        return $this->fetch();
    }

}