<?php

namespace app\admin\controller;
use think\Lang;

class HeadNav extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/product.lang.php');
    }

    /**
     * 网站首页头部导航栏
     */
    public function index(){
        $model_headnav = Model('HeadNav');
        $headnav_keywords = input('post.product_keywords');
        $headnav_type = input('post.product_type');
        $condition = array();
        if($headnav_keywords){
            $condition['product_keywords'] = $headnav_keywords;
        }
        if($headnav_type){
            $condition['product_type'] = $headnav_type;
        }
        $headnav_list = $model_headnav->getHeadNav($condition,'*',10);
        for($i=0;$i<count($headnav_list);$i++){
            $headnav_list[$i]['nav_add_time'] = date("Y-m-d h:i:s",$headnav_list[$i]['nav_add_time']);
            $headnav_list[$i]['nav_update_time'] = date("Y-m-d h:i:s",$headnav_list[$i]['nav_update_time']);
        }

        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('headnav_list', $headnav_list);
        //$this->assign('show_page', $headnav_list->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();

    }

    /**
     * 添加头部导航
     * @return mixed
     */
    public function addHeadNav()
    {
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        if (request()->isPost()) {
            $data = array(
                'nav_name' => input('post.nav_name'),
                'nav_sort' => input('post.nav_sort'),
                'nav_add_time' => time(),
                'nav_update_time' => time(),
                'status' => input('post.status'),
            );

            $result = model('HeadNav')->addHeadNav($data);
            if ($result){
                $this->success(lang('add_succ'), url('HeadNav/index'));
            }
            $this->error(lang('add_fail'));
        } else {
            $product = array(
                'product_isjump' => 0,
                'product_status' => 0,
                'product_type' => '',
                'product_usetime' => '',
            );
            $onlygs = array(
                'member_id' => 0,
                'member_name' =>"请选择所属公司",
            );
            $pic_list = model('pic')->getPicList(array('pic_id' => 0));
            $allgs = model('member')->allcompany('');
            $this->assign('allgs',$allgs);//所有开启的公司
            $this->assign('product', $product);
            $this->assign('onlygs',$onlygs);
            $this->assign('product_pic_type', ['pic_type' => 'product']);
            $this->assign('pic_list', $pic_list);
            $this->setAdminCurItem('add');
            return $this->fetch('form');
        }
    }

    /**
     * 修改头部导航
     * @return mixed
     */
    public function edit()
    {
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $id = input('param.id');
        if ($id <= 0) {
            $this->error('系统错误');
        }
        $condition['id'] = $id;
        if (request()->isPost()) {
            $data = array(
                'nav_name' => input('post.nav_name'),
                'nav_sort' => input('post.nav_sort'),
                'nav_update_time' => time(),
                'status' => input('post.status'),
            );

            $result = model('HeadNav')->editHeadNav(['id' => $id], $data);
            if ($result >= 0) {
                $this->success(lang('edit_succ'), 'HeadNav/index');
            } else {
                $this->error(lang('edit_fail'));
            }
        } else {

            $headnav = model('HeadNav')->getOneHeadNav(['id' => $id]);
            $this->assign('headnav', $headnav);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        }
    }


    /**
     * 删除头部导航
     * @return mixed
     */
    function del()
    {
        $id = intval(input('param.id'));
        if ($id) {
            $condition['id'] = $id;
            $result = model('HeadNav')->delHeadNav($condition);
            if ($result) {
                ds_json_encode(10000, lang('del_succ'));
            } else {
                ds_json_encode(10001, lang('del_fail'));
            }
        } else {
            ds_json_encode(10001, lang('param_error'));
        }
    }
}