<?php
namespace app\admin\controller;

use app\common\model\MateGoodsLog;
use think\App;
use think\Db;
use think\Exception;

class CompanyDispatch extends Auth
{
    public function __construct(App $app = null) {
        parent::__construct($app);
        $this->model = new \app\common\model\MateApply();
        $this->table = $this->model->table;
    }
    /**
     * 公司仓库入库记录
     *
     * @author wst
     * @date   2021/9/10 8:37
     */
    public function show(){
        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;

            $title = input('title','','trim');
            if($title){
                $map[] = ['sn','like','%'.$title.'%'];
            }
            $enable = input('enable','','trim');
            if($enable != ''){
                $map[] = ['enable','=',$enable];
            }
            $map[] = ['org_id','=',$this->orgId];
            $map[] = ['type','=',1];
            $map= empty($map) ? true: $map;
            //数据查询
            $lists =db($this->table)
                ->where($map)->limit($start,$length)
                ->order($order)->select();
            foreach ($lists as $k=>$v){
                $lists[$k]['userName'] = db('user')
                    ->where('id',$v['user_id'])
                    ->value('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{
            $this->assign('meta_title','入库记录');
            return $this->fetch();
        }
    }

    /**
     * 新增入库
     */
    public function add(){
        if(request()->isPost()){
            $res = $this->model->updates($this->userId);
            if($res){
                $this->success('操作成功',url('show'));
            }else{
                $this->error($this->model->getError());
            }
        }else{
            $meta_title = '新增入库';
            $this->assign('meta_title',$meta_title);
            return $this->fetch();
        }
    }

    //详情
    public function info($id){
        $info = $this->model->getInfo($id);
        if(!$info) $this->error('记录不存在');
        $this->assign('info',$info);
        return $this->fetch();
    }

}