<?php

namespace app\admin\controller;

use think\Controller;

class Download extends Common {

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub

    }

    //显示下载类下所有内容
    public function index(){
        $category_id=$this->request->param('category/d', 0 ,'intval');
        $param=$this->request->param();
        if($category_id != 0){
            $param['category_id']=$category_id;
        }
        $models=model('models')->adminGetModelSelectAndUrl();
        $this->assign('models',$models);
        $data=model('download')->adminGetDownloadAllDataToPage($param);
        $data->appends($param);
        $this->assign('data',$data->toArray());
        $this->assign('page',$data->render());
        //搜索赋值
        $this->assign('search',empty($param['search']) ? '' : $param['search']);
        //模型id
        $this->assign('model_id',$this->model_data['id']);
        return $this->fetch();
    }
    //下载类添加
    public function add(){
        $category_id=$this->request->param('category/d', 0 ,'intval');

        $categorySelect=model('category')->adminCategorySelectDataToModelId(0,$this->model_data['id']);

        $this->assign('category_select',$categorySelect);
        $this->assign('category_id',$category_id);
        return $this->fetch();
    }
    //下载类添加提交
    public function addPost(){
        if($this->request->isPost()){
            $params=$this->request->param();
            $return=['code'=>0,'msg'=>'添加失败'];
            if(empty($params['file_url'])){
                $return['msg']='请上传下载文件';
                return $return;
            }
            $result =$this->validate($params,'Download');
            if($result !== true){
                $return['msg']=$result;
                return $return;
            }
            //密码
            if($params['is_pwd'] == 1){
                empty($params['pwd']) ? $params['is_pwd']=getRandomString(4) : $params['is_pwd']=$params['pwd'];
            }
            //创建时间转义
            $params['create_time']=strtotime($params['create_time']);
            $in=model('download')->add($params);
            if($in !== false){
                //成功
                $return['code']=200;
                $return['msg']='添加成功';
                return $return;
            }
            return $return;
        }
    }

    //编辑
    public function edit(){
        $id=$this->request->param('id/d', 0 ,'intval');
        $data=model('download')->adminFindDataToId($id);
        if($id == 0 || $data == false){
            $this->error('ID错误');
        }
        //获取栏目选中栏
        $categorySelect=model('category')->adminCategorySelectDataToModelId(0,$this->model_data['id']);
        $this->assign('category_select',$categorySelect);
        $this->assign('category_id',$data['category_id']);
        //循环models
        $models=model('models')->adminGetModelSelectAndUrl();
        $this->assign('models',$models);

        $this->assign('data',$data);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        if($this->request->isPost()){
            $params=$this->request->param();
            $return=['code'=>0,'msg'=>'修改失败'];
            if(empty($params['file_url'])){
                $return['msg']='请上传下载文件';
                return $return;
            }
            $result =$this->validate($params,'Download');
            if($result !== true){
                $return['msg']=$result;
                return $return;
            }
            //密码
            if($params['is_pwd'] == 1){
                empty($params['pwd']) ? $params['is_pwd']=getRandomString(4) : $params['is_pwd']=$params['pwd'];
            }
            //创建时间转义
            $params['create_time']=strtotime($params['create_time']);
            $in=model('download')->edit($params);
            if($in !== false){
                //成功
                $return['code']=200;
                $return['msg']='修改成功';
                return $return;
            }
            return $return;
        }
    }

    //删除单个内容
    public function isDel(){
        if($this->request->isPost()){
            $id=$this->request->param('id/d',0,'intval');
            $arr=['code'=>0,'msg'=>'删除失败'];
            $del=model('download')->del($id);
            if($del !== false){
                $arr['code']=200;
                $arr['msg']='删除成功';
                return $arr;
            }
            return $arr;
        }
    }

    //批量删除
    public function delete(){
        if($this->request->isPost()){
            $ids=$this->request->param('ids/a');
            $arr=['code'=>0,'msg'=>'删除失败'];
            $del=model('download')->del($ids);
            if($del !== false){
                $arr['code']=200;
                $arr['msg']='删除成功';
                return $arr;
            }
            return $arr;

        }
    }

    //批量移动栏目
    public function removeCategory(){
        if($this->request->isPost()){
            $param=$this->request->param();
            $arr=['code'=>0,'msg'=>'移动失败'];
            $rm=model('download')->remove_category($param['ids'],$param['to_category_id']);
            if($rm !== false){
                $arr['code']=200;
                $arr['msg']='移动成功';
                return $arr;
            }
            return $arr;
        }
    }

    //推荐
    public function toRecommend(){
        if($this->request->isPost()){
            $param=$this->request->param();
            $return=['code'=>0,'msg'=>'修改失败'];
            $arr=['id'=>$param['id'],'is_recommend'=>$param['is_recommend']==0 ? 1 : 0];
            $up=model('download')->edit($arr);
            if($up !== false){
                //成功
                $return['code']=200;
                $return['msg']='修改成功';
                return $return;
            }
            return $return;
        }
    }

    //置顶
    public function toTop(){
        if($this->request->isPost()){
            $param=$this->request->param();
            $return=['code'=>0,'msg'=>'修改失败'];
            $arr=['id'=>$param['id'],'is_top'=>$param['is_top']==0 ? 1 : 0];
            $up=model('download')->edit($arr);
            if($up !== false){
                //成功
                $return['code']=200;
                $return['msg']='修改成功';
                return $return;
            }
            return $return;
        }
    }

}