<?php
namespace app\home\controller;

use think\Lang;

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

    public function index()
    {
        $model_product = Model('product');
        $product_keywords = input('post.product_keywords');
        $condition = array();
        if($product_keywords){
            $condition['product_keywords'] = $product_keywords;
        }
        $condition['product_type'] = input('post.product_type')?input('post.product_type'):array('neq',5);
        $condition['product_companyid'] = session('service_pid');
        $product_list = $model_product->getProductList($condition,'*',10);
        if($product_list){
            for ($i=0; $i < count($product_list); $i++) { 
                $where['member_id'] = $product_list[$i]['product_companyid'];
                $gsname_list[] = model('member')->getMemberInfo($where,$field = 'member_name');
            }
            $this->assign('gsname_list',$gsname_list);
            for ($ie=0; $ie < count($product_list); $ie++) { 
                $con = $product_list[$ie]['product_content'];
                $pattern="/<[img|IMG].*?src=[\'\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
                $replacement = '';
                $conlist[] = mb_substr(preg_replace($pattern,$replacement ,$con),0,20);
            }
            $this->assign('conlist',$conlist);
        }
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('product_list', $product_list);
        $this->assign('show_page', $model_product->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    public function add()
    {
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        if (request()->isPost()) {
            $data = array(
                'product_type' => input('post.product_type'),
                'product_companyid' => session('service_pid'),
                'product_keywords' => input('post.product_keywords'),
                'product_usetime' => input('post.product_usetime'),
                'product_status' => input('post.product_status'),
                'product_content' => input('post.product_content'),
            );
            if (!input('param.product_addtime')) {
                $data['product_addtime'] = TIMESTAMP;
            } else {
                $data['product_addtime'] = strtotime(input('param.product_addtime'));
            }

            $result = model('product')->addproduct($data);
            if ($result){
                $this->success(lang('add_succ'), url('product/index'));
            }
            $this->error(lang('add_fail'));
        } else {
            $product = array(
                'product_status' => 0,
                'product_type' => '',
                'product_usetime' => '',
            );
            $pic_list = model('pic')->getPicList(array('pic_id' => 0));
            $this->assign('product', $product);
            $this->assign('product_pic_type', ['pic_type' => 'product']);
            $this->assign('pic_list', $pic_list);
            $this->setAdminCurItem('add');
            return $this->fetch('form');
        }
    }

    public function edit()
    {
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $product_id = input('param.product_id');
        if ($product_id <= 0) {
            $this->error('系统错误');
        }
        $condition['pic_type'] = 'product';
        $condition['pic_type_id'] = $product_id;
        if (request()->isPost()) {
            $data = array(
                'product_type' => input('post.product_type'),
                'product_keywords' => input('post.product_keywords'),
                'product_usetime' => input('post.product_usetime'),
                'product_status' => input('post.product_status'),
                'product_content' => input('post.product_content'),
            );
            if (!input('param.product_updatetime')) {
                $data['product_updatetime'] = TIMESTAMP;
            } else {
                $data['product_updatetime'] = strtotime(input('param.product_updatetime'));
            }

            $result = model('product')->editproduct(['product_id' => $product_id], $data);
            if ($result >= 0) {
                $this->success(lang('edit_succ'), 'product/index');
            } else {
                $this->error(lang('edit_fail'));
            }
        } else {
            $pic_list = model('pic')->getpicList($condition);
            $this->assign('pic_list', $pic_list);

            //获取当前帮助中心的内容
            $product = model('product')->getOneProduct(['product_id' => $product_id]);
            $allgs = model('member')->allcompany($product['product_companyid']);
            $where['member_id'] = $product['product_companyid'];
            $onlygs = model('member')->getMemberInfo($where,$field = 'member_id,member_name');
            $this->assign('onlygs',$onlygs);//所属公司
            $this->assign('allgs',$allgs);//所有开启的公司
            $this->assign('product_pic_type', ['pic_type' => 'product']);
            $this->assign('product', $product);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        }
    }

    function del()
    {
        $product_id = intval(input('param.product_id'));
        if ($product_id) {
            $condition['product_id'] = $product_id;
            $result = model('product')->delproduct($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'));
        }
    }

    function ajax()
    {
        $branch = input('param.branch');
        switch ($branch) {
            case 'product':
                $product_mod = model('product');
                $condition = array('product_id' => intval(input('param.id')));
                $update[input('param.column')] = input('param.value');
                $product_mod->editproduct($condition, $update);
                echo 'true';
        }
    }

    function setproduct()
    {
        $product_type = input('param.product_type');
        $product_id = input('param.product_id');
        $res = model('product')->getOneProduct(['product_id' => $product_id], $product_type);
        $id = $res[$product_type] == 0 ? 1 : 0;
        $update[$product_type] = $id;
        $condition['product_id'] = $product_id;
        if (model('product')->editproduct($condition, $update)) {
            ds_json_encode(10000, lang('edit_succ'));
        } else {
            $this->error(lang('edit_fail'));
        }
    }

    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index', 'text' => '管理', 'url' => url('product/index')
            ), array(
                'name' => 'add', 'text' => '新增', 'url' => url('product/add')
            ),
        );
        if (request()->action() == 'edit') {
            $menu_array[] = array(
                'name' => 'edit', 'text' => '编辑', 'url' => url('product/edit')
            );
        }
        return $menu_array;
    }
}