<?php

namespace app\admin\controller;

use think\Validate;
use think\Lang;

class Server extends AdminControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/member.lang.php');
    }

    public function index()
    {
        $productModel = Model('Product');
        $fartherProduct = $productModel->getFartherList();
        $this->assign('fartherProduct',  $fartherProduct);
        $fartherId = input('get.id') ?? $fartherProduct[0]->product_id;
        $this->assign('id',  $fartherId);
        $sonProductWhere['product_pid'] = $fartherId;
        $sonProduct = $productModel->getSonList($sonProductWhere);
        $this->assign('sonProduct',  $sonProduct);
        $this->assign('show_page',  $sonProduct->render());

        return $this->fetch();
    }
    public function add()
    {
        $productModel = Model('Product');
        $product_pid = input('param.id');
        if (!request()->isPost()) {
            return $this->fetch('form');
        } else {
            $files = request()->file('product_img');
            $product_name = input('post.product_name');
            $product_content = input('post.product_content');
            $product_money = input('post.product_money');
            $product_status = input('post.product_status');
            $product_TypeOne = input('post.product_TypeOne');
            $product_TypeTwo = input('post.product_TypeTwo');
            $product_TypeFour = input('post.product_TypeFour');
            $product_TypeThree = input('post.product_TypeThree');
            if (!$product_name || !$files || !$product_content || !$product_money || !$product_status
                || !$product_TypeOne || !$product_TypeTwo || !$product_TypeFour || !$product_TypeThree) {
                $this->error("操作失败");
            }
            $addData = array(
                'product_name' => $product_name,
                'product_pid' => $product_pid,
                'product_content' => $product_content,
                'product_money' => $product_money,
                'product_status' => $product_status,
                'product_TypeOne' => $product_TypeOne,
                'product_TypeTwo' => $product_TypeTwo,
                'product_TypeFour' => $product_TypeFour,
                'product_TypeThree' => $product_TypeThree,
            );
            $imgurl = DS_THEME_UPLOADS_URL . '\home';
            $fileName = ImgName();
            $files = $files->setSaveName($fileName);//设置保存文件名
            $imgt = $files->move($imgurl, $savename = $fileName, $replace = true);
            if($imgt){
                $addData['product_img'] = '/home/' . $fileName.'.png';
            }
            $result = $productModel->addProduct($addData);
            if ($result) {
                dsLayerOpenSuccess("新增成功");
            } else {
                $this->error("操作失败");
            }
        }
    }
    public function delete()
    {
        $product_id = intval(input('param.product_id'));
        if ($product_id) {
            $condition['product_id'] = $product_id;
            $result = model('Product')->deleteProduct($condition);
            if ($result) {
                $this->log(lang('ds_product').'-'.lang('del_succ') . '[' . $product_id . ']', null);
                ds_json_encode(10000, lang('del_succ'));
            } else {
                ds_json_encode(10001, lang('del_fail'));
            }
        } else {
            ds_json_encode(10001, lang('del_fail'));
        }
    }
    public function fartherList()
    {
        $productModel = Model('Product');
        $fartherProduct = $productModel->getFartherList();
        $this->assign('fartherProduct',  $fartherProduct);
        return $this->fetch('fartherList');
    }
    public function fartherEdit()
    {
        $productModel = Model('Product');
        $product_id = input('post.product_id');
        $status = input('post.status');
        $product_name= input('post.product_name');
        if ($product_id) {
            $updateData = array(
                'product_name' => $product_name,
                'product_status' => $status,
            );
            $result = $productModel->updateProduct($product_id, $updateData);
            if ($result) {
                return ["msg"=>"编辑成功"];
            } else {
                return ["msg"=>"操作失败"];
            }
        } else {
            $nowDate = date('Y-m-d H:i:s');
            $addData = array(
                'product_name' => $product_name,
                'product_status' => $status,
                'product_content' => '',
                'product_updatetime' => $nowDate,
                'product_addtime' => $nowDate,
                'product_pid' => 0,
            );
            $result = $productModel->addProduct($addData);
            if ($result) {
                return ["msg"=>"新增成功"];
            } else {
                return ["msg"=>"操作失败"];
            }
        }
    }
    public function edit()
    {
        $productModel = Model('Product');
        $product_id = input('param.product_id');
        if (!request()->isPost()) {
            $productWhere['product_id'] = $product_id;
            $product = $productModel->findProduct($productWhere);
            $this->assign('product',  $product);
            return $this->fetch('form');
        } else {
            $updateData = array(
                'product_name' => input('post.product_name'),
                'product_content' => input('post.product_content'),
                'product_money' => input('post.product_money'),
                'product_status' => input('post.product_status'),
                'product_TypeOne' => input('post.product_TypeOne'),
                'product_TypeTwo' => input('post.product_TypeTwo'),
                'product_TypeFour' => input('post.product_TypeFour'),
                'product_TypeThree' => input('post.product_TypeThree'),
            );
            $files = request()->file('product_img');
            if($files){
                $imgurl = DS_THEME_UPLOADS_URL . '\home';
                $fileName = ImgName();
                $files = $files->setSaveName($fileName);//设置保存文件名
                $imgt = $files->move($imgurl, $savename = $fileName, $replace = true);
                if($imgt){
                    $updateData['product_img'] = '/home/' . $fileName.'.png';
                }
            }
            $result = $productModel->updateProduct($product_id, $updateData);
            if ($result) {
                dsLayerOpenSuccess("编辑成功");
            } else {
                $this->error("操作失败");
            }
        }
    }
    public function info()
    {
        $productInfoModel = Model('Productinfo');
        $product_id = input('param.product_id');
        $productInfoWhere['product_id'] = $product_id;
        $productInfo = $productInfoModel->getProductInfo($productInfoWhere);
        $this->assign('productInfo', $productInfo);
        return $this->fetch('information');
    }
    public function deleteInfo()
    {
        $productInfo_id = intval(input('param.productInfo_id'));
        if ($productInfo_id) {
            $condition['productInfo_id'] = $productInfo_id;
            $result = model('Productinfo')->deleteProductInfo($condition);
            if ($result) {
                $this->log(lang('ds_productinfo').'-'.lang('del_succ') . '[' . $productInfo_id . ']', null);
                ds_json_encode(10000, lang('del_succ'));
            } else {
                ds_json_encode(10001, lang('del_fail'));
            }
        } else {
            ds_json_encode(10001, lang('del_fail'));
        }
    }
    public function addInfo()
    {
        $productInfoModel = Model('Productinfo');
        $product_id = input('param.product_id');
        if (!request()->isPost()) {
            return $this->fetch('infoForm');
        } else {
            $productInfo_title = input('post.productInfo_title');
            $productInfo_content = input('post.productInfo_content');
            $files = request()->file('productInfo_img');
            if (!$productInfo_title || !$productInfo_content || !$files) {
                $this->error("操作失败");
            }
            $addData = array(
                'productInfo_title' => $productInfo_title,
                'productInfo_content' => $productInfo_content,
                'product_id' => $product_id,
            );
            $imgurl = DS_THEME_UPLOADS_URL . '\home';
            $fileName = ImgName();
            $files = $files->setSaveName($fileName);//设置保存文件名
            $imgt = $files->move($imgurl, $savename = $fileName, $replace = true);
            if ($imgt) {
                $addData['productInfo_img'] = '/home/' . $fileName . '.png';
            }
            $result = $productInfoModel->addProductInfo($addData);
            if ($result) {
                dsLayerOpenSuccess("添加成功");
            } else {
                $this->error("操作失败");
            }
        }
    }
    public function editInfo()
    {
        $productInfoModel = Model('Productinfo');
        $productInfo_id = input('param.productInfo_id');
        if (!request()->isPost()) {
            $productInfoWhere['productInfo_id'] = $productInfo_id;
            $productInfo = $productInfoModel->findProductInfo($productInfoWhere);
            $this->assign('productInfo', $productInfo);

            return $this->fetch('infoForm');
        } else {
            $updateData = array(
                'productInfo_title' => input('post.productInfo_title'),
                'productInfo_content' => input('post.productInfo_content'),
            );
            $files = request()->file('productInfo_img');
            if($files){
                $imgurl = DS_THEME_UPLOADS_URL . '\home';
                $fileName = ImgName();
                $files = $files->setSaveName($fileName);//设置保存文件名
                $imgt = $files->move($imgurl, $savename = $fileName, $replace = true);
                if($imgt){
                    $updateData['productInfo_img'] = '/home/' . $fileName.'.png';
                }
            }
            $result = $productInfoModel->updateProductInfo($productInfo_id, $updateData);
            if ($result) {
                dsLayerOpenSuccess("编辑成功");
            } else {
                $this->error("操作失败");
            }
        }
    }
    public function renewalList()
    {
        $renewalModel = Model('Renewal');
        $renewal = $renewalModel->getRenewal();
        $this->assign('renewal',  $renewal);
        return $this->fetch('renewalList');
    }
    public function renewalEdit()
    {
        $renewalModel = Model('Renewal');
        $renewal_identity = input('post.renewal_identity');
        $status = input('post.status');
        $renewal_name = input('post.renewal_name');
        $renewal_day = input('post.renewal_day');
        $updateData = array(
            'renewal_name' => $renewal_name,
            'renewal_status' => $status,
            'renewal_day' => $renewal_day,
        );
        $result = $renewalModel->updateRenewal($renewal_identity, $updateData);
        if ($result) {
            return ["msg"=>"编辑成功"];
        } else {
            return ["msg"=>"操作失败"];
        }
    }
    public function userServer()
    {
        $UserProductModel = Model('Userproduct');
        $userName = input('post.user_name');
        $productName = input('post.product_name');
        $userProductIdentity = input('post.userProduct_identity');
        $userProductWhere = [];
        if ($userName) {
            $userProductWhere['user_email'] = $userName;
        }
        if ($productName) {
            $userProductWhere['product_name'] = $productName;
        }
        if ($userProductIdentity) {
            $userProductWhere['userProduct_identity'] = $userProductIdentity;
        }
        $userproduct = $UserProductModel->getUserproduct($userProductWhere);
        $this->assign('userproduct',  $userproduct);
        $this->assign('show_page',  $userproduct->render());

        return $this->fetch('userServer');
    }

}
