<?php
/**
 * Created by PhpStorm.
 * User: blues
 * Date: 2018/10/30
 * Time: 15:09
 */

    namespace App\Http\Controllers\Admin;
    use App\Http\Controllers\Controller;
    use App\Models\SystemAppupdate;
    use Illuminate\Http\Request as Req;
    use Request;

    class SystemAppupdateController   extends Controller
    {

        public function index(Req $req)
        {
            $request['title'] = isset($req->title) ? trim($req->title) : null;
            $request['type'] = isset($req->type) ? trim($req->type) : null;
            $update = new SystemAppupdate();
            $dt = \App\Lib\DataTable\DataTable::init();
            $dt->setDataSource('/admin/SystemAppupdate/getlist');
            $dt->setLang('update_record');
            // $dt->addColsFields('id');
            $dt->addColsFields('id');
            // $dt->addColsFields('content');
            $dt->addColsFields('version_no');
            $dt->addColsFields('settype');//层级
//            $dt->addColsFields('version_details');
//            $dt->addColsFields('version_remark');
//            $dt->addColsFields('download_url');
            $dt->addColsFields('update_num');
            $dt->addColsFields('status');
            $dt->addColsFields('create_date');
            $dt->enableCheckBox();
            if (checkRriv('/admin/SystemAppupdate/edit')) {
                $arr[] = 'edit';
            }
            $dt->setToolBar($arr, array('width' => 140));

            $request["classtype"]    = [
                ["id" => 0, "name" => 'WEB'],
                ["id" => 1, "name" => '苹果'],
                ["id" => 2, "name" => '安卓'],
            ];

            return view('admin.systemSet/sysAppUpdateRecord', $dt->render($request));
        }

        function records(){
               return $this->index();
        }
        function add(Req $req){


                if (!$req->isMethod('post')) {
                    $datas['version_no'] = 'v'.'-'.time();
                    return view('admin.systemSet/ayaAppUpdateAdd', ['select' => null, 'datas'=>$datas, 'data'=>[],"selectdata" => []]);
                } else {


                    $update =   new SystemAppupdate();
                    $version_no = $req->input("version_no") ? $req->input("version_no") : '';
                   
                    $version_details = $req->input("version_details");
                    $version_remark = $req->input("version_remark");
                    $settype = $req->input("settype");
                    $status = $req->input("status");
                    $download_url = $req->input("download_url");
					$app_version = $req->input("app_version");
                    $type_data = [
                        "version_details" => $version_details,
                        "version_remark" => $version_remark,
                        "settype" => $settype,
                        "status" => $status,
                        "download_url" => $download_url,
                        "create_date" => date("Y-m-d H:i:s"),
                        "other" =>$app_version,
                    ];

                    if (empty($version_no)) {
                        $type_data["version_no"] = 'v'.'-'.time();
                    }else{
                        $type_data["version_no"] = $version_no;
                    }
                    $add = $update->addRecord($type_data);
                    $type_log = [
                        session('adminInfo.admin_name'),
                        $req['version_no']];
                    OperationLog(session('adminInfo.admin_id'), 'system_appupdate_add', $type_log);

                    return responseToJson(1);
                }
            }


        function edit(Req $req){
            $id = $req->id;

            if (empty($id)) {
                abort(404);
            }
            $db = new SystemAppupdate();
            if (!$req->isMethod('post')) {
                $data = $db->getIdData($id, 1);
                return view('admin.systemSet/ayaAppUpdateAdd', ['data' => $data,]);
            } else {

                $version_no = $req->input("version_no");
                $version_details = $req->input("version_details");
                $version_remark = $req->input("version_remark");
                $settype = $req->input("settype");
                $status = $req->input("status");
                $download_url = $req->input("download_url");
                $app_version = $req->input("app_version");

                $type_data = [
                    "version_no" => $version_no,
                    "version_details" => $version_details,
                    "version_remark" => $version_remark,
                    "settype" => $settype,
                    "status" => $status,
                    "download_url" => $download_url,
                    "create_date" => date("Y-m-d H:i:s"),
                    "other" =>$app_version,
                ];

                $update = $db->updateIdData(intval($id), $type_data);
                $type_log = [
                    session('adminInfo.admin_name'),
                    $req['version_no,'] ];
                OperationLog(session('adminInfo.admin_id'), 'system_appupdate_update', $type_log);

                return responseToJson(1);
            }
        }

        public function delete(Req $req) {
        $id = $req->id;
        if (empty($id)) {
            return responseToJson(-2001); //id不能为空
        }
        $ids = explode(',', $id);
        if (!is_array($ids) && intval($ids) < 0) {
            return responseToJson(-2002); //id错误
        }
        if (is_array($ids) && count($ids) > 0) {
            foreach ($ids as $k => $v) {
                if (intval($v) < 1) {
                    unset($ids[$k]);
                }
            }
        }
        $rows = \App\Models\SystemAppupdate::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003); //id错误
        }
        return responseToJson(1, trans('priv_code.delete_success')); //id错误

    }



        //获取列表信息
        function getlist() {

            //设备0=wap,1=ios,2=androi
            $settype    =   [
                0=>'WEB',
                1=>'苹果',
                2=>'安卓'
            ];

            $limit = Request::has('limit') ? Request::get('limit') : 50;

            $title = Request::has('title') ? Request::get('title') : '';
            $type = Request::has('type') ? Request::get('type') : '';

            $add_startime = Request::has('add_startime') ? Request::get('add_startime') : '';
            $add_endtime = Request::has('add_endtime') ? Request::get('add_endtime') : '';

            $where = array();
            if (isset($title)) {
                $where[] = array('version_no', 'like', '%' . $title . '%');
            }
            if (isset($type)) {
                $where[] = array('settype', $type);
            }

            if (!empty($add_startime)) {
                $add_startime = date('Y-m-d H:i:s', strtotime($add_startime));
                $where[] = array('create_time', '>=', $add_startime);
            }
            if (!empty($add_endtime)) {
                $add_endtime = date('Y-m-d H:i:s', strtotime($add_endtime));
                $where[] = array('create_time', '<=', $add_endtime);
            }

            $db = new SystemAppupdate();
            $data = $db->getlist($where,$limit );

            $c  =   count($data);
            if ($c>0) {
                foreach ($data as $k=>$v){
                    $data[$k]["settype"]=$settype[$v['settype']];
                    $data[$k]["status"]=empty($v['status'])?'禁用':'启用';
//                    $data[$k]["status"]=empty($v['status'])?'禁用':'<b style="color: green">启用</b>';
                }
                return \App\Lib\DataTable\DataTable::init()->toJson($data, $c);
            }
            return \App\Lib\DataTable\DataTable::init()->toJson($data['data']=[], $data['total']=null);
        }
    }