<?php

namespace Modules\Team\Controllers;

use App\Controllers\Auth\AuthController;
use Modules\Posts\Models\PostsCategoryModel;
use Modules\Posts\Models\PostsModel;
use Modules\Team\Models\TeamModel;
use Modules\Team\Models\TeamCategoryModel;
use Modules\Posts\Models\PostBlock_CodeModel;
use Modules\Posts\Models\PostBlock_HeadingModel;
use Modules\Posts\Models\PostBlock_ImageModel;
use Modules\Posts\Models\PostBlock_ListModel;
use Modules\Posts\Models\PostBlock_TextModel;
use Modules\Posts\Models\PostBlock_VideoModel;
use Modules\Posts\Models\PostBlock_AdvertCodeModel;


class TeamBlocks extends AuthController
{

  public $postscategoryModel;
  public $postsModel;
  public function __construct()
  {
    parent::init();
    $this->postscategoryModel           = new PostsCategoryModel();
    $this->postsModel                   = new PostsModel();
    $this->teamModel                    = new TeamModel();
    $this->teamcategoryModel            = new TeamCategoryModel();
    $this->postblock_codeModel          = new PostBlock_CodeModel();
    $this->postblock_headingModel       = new PostBlock_HeadingModel();
    $this->postblock_imageModel         = new PostBlock_ImageModel();
    $this->postblock_listModel          = new PostBlock_ListModel();
    $this->postblock_textModel          = new PostBlock_TextModel();
    $this->postblock_videoModel         = new PostBlock_VideoModel(); 
    $this->postblock_advertcodeModel    = new PostBlock_AdvertCodeModel();
    $this->data['categoryTree']         = $this->postscategoryModel->getCategoryList();

  }

  // Add Text Block
    public function addTextBlock()
    {
        if ($this->request->getMethod() == 'post') {
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);

            $rules = [
                'content' => ['label'=>'Text', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $blockCat = 'team';
                $textData = [
                    'post_id'       => $this->request->getVar('post_id'), // Use post_id to associate the text block with the post
                    'block_cat'     => $blockCat,
                    'content'       => $this->request->getVar('content'),
                ];

                $results = $this->postblock_textModel->save($textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Text saved successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                } else {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'error'   => true,
                            'msg'     => 'Text NOT saved',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Text Area cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function editTextBlock($id)
    {
        $data = $this->postblock_textModel->where('id', $id)->first($id);
        echo json_encode($data);
    }
    public function updateTextBlock()
    {
        $id = $this->request->getVar('id');

        if ($this->request->getMethod() == 'post') {
            
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);
            $blockCat = 'team';

            $rules = [
                'content' => ['label'=>'Text', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $textData = [
                    'post_id'   => $this->request->getVar('post_id'), // Use post_id to associate the text block with the post
                    'block_cat'     => $blockCat,
                    'content'      => $this->request->getVar('content'),
                ];

                $results = $this->postblock_textModel->update($id, $textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Text updated successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Text Area cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function deleteTextBlock($id)
    {
        $postId = $this->postblock_textModel->where('id',$id)->first($id);
        $this->postblock_textModel->where('id', $id)->delete($id);

        if($this->request->isAJAX())
        {
          $response = [
            'error' => true,
            'msg' =>'Text Block deleted successfully',	   
          ];
          return $this->response->setJSON($response);
        }else{
          session()->setFlashdata('error', 'Text Block deleted  successfully', 3);
                return redirect()->to('administrator/member/block/edit/'.$postId['post_id']);
        }
    }


    // Add Heading Block
    public function addHeadingBlock()
    {
        if ($this->request->getMethod() == 'post') {
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);

            $rules = [
                'heading' => ['label'=>'Heading Title', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $blockCat = 'team';
                $textData = [
                    'post_id'       => $this->request->getVar('post_id'),
                    'block_cat'     => $blockCat, 
                    'heading'       => $this->request->getVar('heading'),
                    'heading_type'  => $this->request->getVar('heading_type'),
                    'color'         => $this->request->getVar('color'),
                ];

                $results = $this->postblock_headingModel->save($textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Heading saved successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                } else {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'error'   => true,
                            'msg'     => 'Heading NOT saved',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Heading cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function editHeadingBlock($id)
    {
        $data = $this->postblock_headingModel->where('id', $id)->first($id);
        echo json_encode($data);
    }
    public function updateHeadingBlock()
    {
        $id = $this->request->getVar('id');
        if ($this->request->getMethod() == 'post') {
            
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);
            $blockCat = 'team';
            $rules = [
                'heading' => ['label'=>'Title', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $textData = [
                    'post_id'       => $this->request->getVar('post_id'), 
                    'block_cat'     => $blockCat,
                    'heading'       => $this->request->getVar('heading'),
                    'heading_type'  => $this->request->getVar('heading_type'),
                    'color'         => $this->request->getVar('color'),
                ];

                $results = $this->postblock_headingModel->update($id, $textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Heading updated successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Heading cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function deleteHeadingBlock($id)
    {
        $postId = $this->postblock_headingModel->where('id',$id)->first($id);
        $this->postblock_headingModel->where('id', $id)->delete($id);

        if($this->request->isAJAX())
        {
          $response = [
            'error' => true,
            'msg' =>'Heading Block deleted successfully',	   
          ];
          return $this->response->setJSON($response);
        }else{
          session()->setFlashdata('error', 'Heading Block deleted  successfully', 3);
                return redirect()->to('administrator/member/block/edit/'.$postId['post_id']);
        }
    }


    // Add Heading Block
    public function addListBlock()
    {
        if ($this->request->getMethod() == 'post') {
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);

            $rules = [
                'content' => ['label'=>'List', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $blockCat = 'team';
                $textData = [
                    'post_id'       => $this->request->getVar('post_id'),
                    'block_cat'     => $blockCat,
                    'content'       => $this->request->getVar('content'),
                    'list_type'     => $this->request->getVar('list_type'),
                ];

                $results = $this->postblock_listModel->save($textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'List saved successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                } else {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'error'   => true,
                            'msg'     => 'List NOT saved',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'List cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function editListBlock($id)
    {
        $data = $this->postblock_listModel->where('id', $id)->first($id);
        echo json_encode($data);
    }
    public function updateListBlock()
    {
        $id = $this->request->getVar('id');
        if ($this->request->getMethod() == 'post') {
            
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);
            $blockCat = 'post';

            $rules = [
                'content' => ['label'=>'List', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $textData = [
                    'post_id'       => $this->request->getVar('post_id'),
                    'block_cat'     => $blockCat,
                    'list_type'     => $this->request->getVar('list_type'),
                    'content'       => $this->request->getVar('content'),
                ];

                $results = $this->postblock_listModel->update($id, $textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'List updated successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'List Area cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function deleteListBlock($id)
    {
        $postId = $this->postblock_listModel->where('id',$id)->first($id);
        $this->postblock_listModel->where('id', $id)->delete($id);

        if($this->request->isAJAX())
        {
          $response = [
            'error' => true,
            'msg' =>'List Block deleted successfully',	   
          ];
          return $this->response->setJSON($response);
        }else{
          session()->setFlashdata('error', 'List Block deleted  successfully', 3);
                return redirect()->to('administrator/member/block/edit/'.$postId['post_id']);
        }
    }

    // Add Image Block
    public function addImageBlock()
    {
        if ($this->request->getMethod() == 'post') {
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);

            $rules = [
                'caption' => ['label'=>'Caption', 'rules'=>'required'],
              ];
              $photoRules = [
                'photo' => [
                    'label' => 'Image File',
                    'rules' => 'uploaded[photo]'
                        . '|is_image[photo]'
                        . '|mime_in[photo,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
                        . '|max_size[photo,1024]'
                ],
            ];
              if($this->validate($rules))
              {
                $blockCat = 'team';
                $file1 = $this->request->getFile('photo');
                if($this->validate($photoRules))
                { 
                    $newName4 = $file1->getRandomName();
                    $file1->move(ROOTPATH . 'media/posts', $newName4);
                    $nwn1 = base_url('/media/posts/')."/".$newName4;
    
                    $postData = [
                      'post_id'         => $this->request->getVar('post_id'),
                      'block_cat'       => $blockCat,
                      'photo'           =>$nwn1,
                      'caption'         => $this->request->getVar("caption"),
                  ];
                  $results = $this->postblock_imageModel->save($postData);
                    if ($results) {
                        if ($this->request->isAJAX()) {
                            $response = [
                                'success' => true,
                                'msg'     => 'Image saved successfully',
                            ];
                            return $this->response->setJSON($response);
                        }
                    } else {
                        if ($this->request->isAJAX()) {
                            $response = [
                                'error'   => true,
                                'msg'     => 'Image NOT saved',
                            ];
                            return $this->response->setJSON($response);
                        }
                    }
        
                }else{
                    if ($this->request->isAJAX()) {
                        $response = [
                            'error'   => true,
                            'msg'     => 'Image is either too big or not accepted format.',
                        ];
                        return $this->response->setJSON($response);
                    }
                }     
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Image cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function editImageBlock($id)
    {
        $data = $this->postblock_imageModel->where('id', $id)->first($id);
        echo json_encode($data);
    }
    public function updateImageBlock()
    {
        $id = $this->request->getVar('id');
        if ($this->request->getMethod() == 'post') {
            
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);

            $rules = [
                'caption' => ['label'=>'Caption', 'rules'=>'required'],
              ];
              $photoRules = [
                'photo' => [
                    'label' => 'Image File',
                    'rules' => 'uploaded[photo]'
                        . '|is_image[photo]'
                        . '|mime_in[photo,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
                        . '|max_size[photo,1024]'
                ],
            ];
              if($this->validate($rules))
              {
                $blockCat = 'team';
                $file1 = $this->request->getFile('photo');
                if($this->validate($photoRules))
                { 
                    $newName4 = $file1->getRandomName();
                    $file1->move(ROOTPATH . 'media/posts', $newName4);
                    $nwn1 = base_url('/media/posts/')."/".$newName4;
    
                    $postData = [
                      'post_id'         => $this->request->getVar('post_id'),
                      'block_cat'       => $blockCat,
                      'photo'           =>$nwn1,
                      'caption'         => $this->request->getVar("caption"),
                  ];
                  $results = $this->postblock_imageModel->update($id, $postData);
                    if ($results) {
                        if ($this->request->isAJAX()) {
                            $response = [
                                'success' => true,
                                'msg'     => 'Photo changed updated successfully',
                            ];
                            return $this->response->setJSON($response);
                        }
                    }
                }else{
                    if ($this->request->isAJAX()) {
                        $response = [
                            'error'   => true,
                            'msg'     => 'Image is either too big or not accepted format.',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'caption cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function deleteImageBlock($id)
    {
        $postId = $this->postblock_imageModel->where('id',$id)->first($id);
        $this->postblock_imageModel->where('id', $id)->delete($id);

        if($this->request->isAJAX())
        {
          $response = [
            'error' => true,
            'msg' =>'Photo deleted successfully',	   
          ];
          return $this->response->setJSON($response);
        }else{
          session()->setFlashdata('error', 'Photo deleted  successfully', 3);
                return redirect()->to('administrator/member/block/edit/'.$postId['post_id']);
        }
    }

    // Add Video Block
    public function addVideoBlock()
    {
        if ($this->request->getMethod() == 'post') {
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);

            $rules = [
                'url' => ['label'=>'Video Link', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $blockCat = 'team';
                $textData = [
                    'post_id'       => $this->request->getVar('post_id'),
                    'block_cat'     => $blockCat,
                    'content'       => $this->request->getVar('content'),
                    'url'           => $this->request->getVar('url'),
                ];

                $results = $this->postblock_videoModel->save($textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Video added successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                } else {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'error'   => true,
                            'msg'     => 'Video NOT saved',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Link cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function editVideoBlock($id)
    {
        $data = $this->postblock_videoModel->where('id', $id)->first($id);
        echo json_encode($data);
    }
    public function updateVideoBlock()
    {
        $id = $this->request->getVar('id');
        if ($this->request->getMethod() == 'post') {
            
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);
            $blockCat = 'post';

            $rules = [
                'url' => ['label'=>'Video Link', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $textData = [
                    'post_id'       => $this->request->getVar('post_id'),
                    'block_cat'     => $blockCat,
                    'url'           => $this->request->getVar('url'),
                    'content'       => $this->request->getVar('content'),
                ];

                $results = $this->postblock_videoModel->update($id, $textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Video updated successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Video Link cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function deleteVideoBlock($id)
    {
        $postId = $this->postblock_videoModel->where('id',$id)->first($id);
        $this->postblock_videoModel->where('id', $id)->delete($id);

        if($this->request->isAJAX())
        {
          $response = [
            'error' => true,
            'msg' =>'Video Block deleted successfully',	   
          ];
          return $this->response->setJSON($response);
        }else{
          session()->setFlashdata('error', 'Video Block deleted  successfully', 3);
                return redirect()->to('administrator/member/block/edit/'.$postId['post_id']);
        }
    }


    // Add Code Block
    public function addCodeBlock()
    {
        if ($this->request->getMethod() == 'post') {
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);

            $rules = [
                'code-content' => ['label'=>'Code Area', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $blockCat = 'team';
                $codeData = [
                    'post_id'       => $this->request->getVar('post_id'),
                    'block_cat'     => $blockCat,
                    'code'          => $this->request->getVar('code-content'),
                ];

                $results = $this->postblock_codeModel->save($codeData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Code snippet saved successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                } else {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'error'   => true,
                            'msg'     => 'Code snippet NOT saved',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Code Area cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function editCodeBlock($id)
    {
        $data = $this->postblock_codeModel->where('id', $id)->first($id);
        echo json_encode($data);
    }
    public function updateCodeBlock()
    {
        $id = $this->request->getVar('id');
        if ($this->request->getMethod() == 'post') {
            
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);
            $blockCat = 'team';

            $rules = [
                'code' => ['label'=>'Code', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $textData = [
                    'post_id'       => $this->request->getVar('post_id'),
                    'block_cat'     => $blockCat,
                    'code'           => $this->request->getVar('code'),
                ];

                $results = $this->postblock_codeModel->update($id, $textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Code updated successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Code Area cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function deleteCodeBlock($id)
    {
        $postId = $this->postblock_codeModel->where('id',$id)->first($id);
        $this->postblock_codeModel->where('id', $id)->delete($id);

        if($this->request->isAJAX())
        {
          $response = [
            'error' => true,
            'msg' =>'Code Block deleted successfully',	   
          ];
          return $this->response->setJSON($response);
        }else{
          session()->setFlashdata('error', 'Code Block deleted  successfully', 3);
                return redirect()->to('administrator/member/block/edit/'.$postId['post_id']);
        }
    }

    // Add Advert Code Block
    public function addAdvertCodeBlock()
    {
        if ($this->request->getMethod() == 'post') {
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);

            $rules = [
                'advert_code' => ['label'=>'Advert Area', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $blockCat = 'post';
                $codeData = [
                    'post_id'       => $this->request->getVar('post_id'),
                    'block_cat'     => $blockCat, 
                    'advert_code'   => $this->request->getVar('advert_code'),
                ];

                $results = $this->postblock_advertcodeModel->save($codeData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Advert Code saved successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                } else {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'error'   => true,
                            'msg'     => 'Advert code NOT saved',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Advert Area cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function editAdvertCodeBlock($id)
    {
        $data = $this->postblock_advertcodeModel->where('id', $id)->first($id);
        echo json_encode($data);
    }
    public function updateAdvertCodeBlock()
    {
        $id = $this->request->getVar('id');
        if ($this->request->getMethod() == 'post') {
            
            $postId = $this->request->getVar('post_id');
            $article = $this->teamModel->where('id', $postId)->first($postId);
            $blockCat = 'post';

            $rules = [
                'advert_code' => ['label'=>'Advert Code', 'rules'=>'required'],
            ];

            if($this->validate($rules))
            {
                $textData = [
                    'post_id'       => $this->request->getVar('post_id'),
                    'block_cat'     => $blockCat,
                    'code'          => $this->request->getVar('code'),
                ];

                $results = $this->postblock_advertcodeModel->update($id, $textData);

                if ($results) {
                    if ($this->request->isAJAX()) {
                        $response = [
                            'success' => true,
                            'msg'     => 'Advert Code updated successfully',
                        ];
                        return $this->response->setJSON($response);
                    }
                }
            } else {
                if ($this->request->isAJAX()) {
                    $response = [
                        'error'   => true,
                        'msg'     => 'Advert Code Area cannot be empty',
                    ];
                    return $this->response->setJSON($response);
                }
            }
        }
    }
    public function deleteAdvertCodeBlock($id)
    {
        if($this->userdata->group_id != 1){
            session()->setFlashdata('warning', 'Access denied');
            return redirect()->to(previous_url());
        }
        $postId = $this->postblock_advertcodeModel->where('id',$id)->first($id);
        $this->postblock_advertcodeModel->where('id', $id)->delete($id);

        if($this->request->isAJAX())
        {
          $response = [
            'error' => true,
            'msg' =>'Advert Block deleted successfully',	   
          ];
          return $this->response->setJSON($response);
        }else{
          session()->setFlashdata('error', 'Advert Block deleted  successfully', 3);
                return redirect()->to('administrator/member/block/edit/'.$postId['post_id']);
        }
    }



}