/**
  * Edit Dailyspecail And Campaign Action
  * @param Request $request
  * @param $did
  * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  * @throws \Exception
  * @author: Vini Dubey<*****@*****.**>
  * @since: xx-xx-xxxx
  */
 public function editDailyspecial(Request $request, $did)
 {
     $objProductCategory = ProductCategory::getInstance();
     $objCampaignModel = Campaigns::getInstance();
     $objProductModel = Products::getInstance();
     $supplierId = Session::get('fs_supplier')['id'];
     if ($request->isMethod('post')) {
         $rules = array('campaign_name' => 'unique:campaigns,campaign_name,' . $did . ',campaign_id', 'dailyspecial_image' => 'image');
         $validator = Validator::make($request->all(), $rules);
         if ($validator->fails()) {
             return Redirect::back()->withErrors($validator)->withInput();
         }
         $postData = $request->all();
         if (sizeof($postData['product']) >= 10) {
             if (Input::hasFile('dailyspecial_image')) {
                 $filePath = uploadImageToStoragePath(Input::file('dailyspecial_image'), 'flashsale', 'flashsale_' . $supplierId . '_' . time() . ".jpg");
                 $data['campaign_banner'] = $filePath;
             }
             $data['campaign_name'] = $postData['campaign_name'];
             $data['campaign_type'] = 2;
             $data['discount_type'] = '2';
             //            if ($data['discount_type'] == '1') {
             //                $data['discount_value'] = $postData['flatdiscount'];
             //            } else {
             $data['discount_value'] = $postData['percentagediscount'];
             //            }
             $validFrom = strtotime(str_replace("-", "", $postData['availablefromdate']));
             $validTo = strtotime(str_replace("-", "", $postData['availableuptodate']));
             $data['available_from'] = $validFrom;
             $data['available_upto'] = $validTo;
             $categ = $postData['productcategories'];
             if (isset($postData['productsubcategories'])) {
                 $subcat = $postData['productsubcategories'];
             } else {
                 $subcat = [];
             }
             $tmp = [];
             foreach ($categ as $index => $item) {
                 $tmp[$item] = array_values(array_filter(array_map(function ($cat) use($item) {
                     if (explode('_', $cat)[0] == $item) {
                         return explode('_', $cat)[1];
                     }
                 }, $subcat)));
             }
             $data['for_category_ids'] = json_encode($tmp);
             $data['by_user_id'] = $supplierId;
             //                $product = $postData['product'];
             $data['for_product_ids'] = implode(",", $postData['product']);
             $where = ['rawQuery' => 'campaign_id = ?', 'bindParams' => [$did]];
             $campaigns = 'Flashsale';
             //                print_a($data);
         } else {
             if (Input::hasFile('dailyspecial_image')) {
                 $filePath = uploadImageToStoragePath(Input::file('dailyspecial_image'), 'dailyspecial', 'dailyspecial_' . $supplierId . '_' . time() . ".jpg");
                 $data['campaign_banner'] = $filePath;
             }
             $data['campaign_name'] = $postData['campaign_name'];
             $data['campaign_type'] = $postData['campaign_type'];
             $data['discount_type'] = '2';
             $data['discount_value'] = $postData['percentagediscount'];
             $validFrom = strtotime(str_replace("-", "", $postData['availablefromdate']));
             $validTo = strtotime(str_replace("-", "", $postData['availableuptodate']));
             $data['available_from'] = $validFrom;
             $data['available_upto'] = $validTo;
             $categ = $postData['productcategories'];
             if (isset($postData['productsubcategories'])) {
                 $subcat = $postData['productsubcategories'];
             } else {
                 $subcat = [];
             }
             //                print_r($categ);
             //                print_a($subcat);
             $tmp = [];
             foreach ($categ as $index => $item) {
                 $tmp[$item] = array_values(array_filter(array_map(function ($cat) use($item) {
                     if (explode('_', $cat)[0] == $item) {
                         return explode('_', $cat)[1];
                     }
                 }, $subcat)));
             }
             //                print_a(json_encode($tmp));
             $data['for_category_ids'] = json_encode($tmp);
             $data['by_user_id'] = $supplierId;
             //                $product = $postData['product'];
             $data['for_product_ids'] = implode(",", $postData['product']);
             //                $data['for_product_ids'] = $postData['product'][0];
             $where = ['rawQuery' => 'campaign_id = ?', 'bindParams' => [$did]];
             $campaigns = 'Dailyspecial';
             //                print_a($data);
         }
         $campaignUpdate = $objCampaignModel->updateFlashsaleStatus($data, $where);
         if ($campaignUpdate) {
             if (isset($filePath)) {
                 deleteImageFromStoragePath($postData['oldImage']);
             }
             return Redirect::back()->with(['status' => 'success', 'msg' => $campaigns . ' ' . 'Updated Successfully.']);
         } else {
             return Redirect::back()->with(['status' => 'error', 'msg' => $campaigns . ' ' . 'Some Error try again.']);
         }
     }
     $where = ['rawQuery' => 'campaign_id = ? AND by_user_id = ?', 'bindParams' => [$did, $supplierId]];
     $selectedColumn = ['campaigns.*', 'users.username'];
     $dailyspecialInfo = $objCampaignModel->getAllFlashsaleDetails($where, $selectedColumn);
     //        print_a($dailyspecialInfo);
     if (isset($dailyspecialInfo) && !empty($dailyspecialInfo)) {
         foreach ($dailyspecialInfo as $flashkey => $flashval) {
             $categoryIds = json_decode($flashval->for_category_ids, true);
             $categoryMerg = array_merge(array_keys($categoryIds));
             $categoryMergee = array_merge(array_flatten($categoryIds));
             $categoryMerge = array_merge(array_keys($categoryIds), array_flatten($categoryIds));
             //                echo'<pre>';print_r($categoryMerg);
             //                echo'<pre>';print_r($categoryMergee);
             //                print_a($categoryMerge);
             $where = ['rawQuery' => 'category_id IN(' . implode(",", $categoryMerge) . ')'];
             $selectedColumn = [DB::raw('GROUP_CONCAT(DISTINCT category_name) AS category_name'), DB::raw('GROUP_CONCAT(DISTINCT category_id) AS category_id')];
             $getcategory = $objProductCategory->getCategoryNameById($where, $selectedColumn);
             foreach ($getcategory as $catkey => $catval) {
                 $dailyspecialInfo[$flashkey]->category = $catval->category_name;
                 $dailyspecialInfo[$flashkey]->category_ids = $catval->category_id;
             }
             $whereProduct = ['rawQuery' => 'product_id IN(' . $flashval->for_product_ids . ')'];
             $selectedColumn = [DB::raw('GROUP_CONCAT(DISTINCT product_name) AS product_name'), DB::raw('GROUP_CONCAT(DISTINCT product_id) AS product_id')];
             $getproduct = $objProductModel->getProductNameById($whereProduct, $selectedColumn);
             foreach ($getproduct as $prodkey => $prodval) {
                 $dailyspecialInfo[$flashkey]->product_name = $prodval->product_name;
                 $dailyspecialInfo[$flashkey]->product_id = $prodval->product_id;
             }
         }
         $where = ['rawQuery' => 'category_status = ? AND parent_category_id = ?', 'bindParams' => [1, 0]];
         $selectedColumn = ['category_id', 'category_name', 'category_status', 'for_shop_id'];
         $allactivecategories = $objProductCategory->getAllMainCategories($where, $selectedColumn);
         $where = ['rawQuery' => 'category_status = ?', 'bindParams' => [1]];
         $selectedColumn = ['product_categories.*', DB::raw('GROUP_CONCAT(category_id)AS main_category_id'), DB::raw('GROUP_CONCAT(category_name)AS main_category_name')];
         $allActiveSubcategories = $objProductCategory->getSubCategoriesForMaincategory($where, $selectedColumn);
         $mainCategory = array_filter(array_map(function ($category) {
             if ($category->parent_category_id == 0) {
                 return $category;
             }
         }, $allActiveSubcategories))[0];
         $finalCatData = [];
         foreach (explode(',', $mainCategory->main_category_id) as $index => $mainCatID) {
             foreach ($allActiveSubcategories as $subCatKey => $allActiveSubcategory) {
                 if ($allActiveSubcategory->parent_category_id == $mainCatID) {
                     $allActiveSubcategory->main_cat_name = explode(',', $mainCategory->main_category_name)[$index];
                     $finalCatData[$mainCatID] = $allActiveSubcategory;
                 }
             }
         }
         $where = ['rawQuery' => 'added_by = ? AND product_type = ?', 'bindParams' => [$supplierId, 0]];
         $selectedColumn = ['product_id', 'product_name'];
         $allproducts = $objProductModel->getAllSupplierProducts($where, $selectedColumn);
         return view('Supplier/Views/dailyspecial/editDailySpecial', ['dailyspecialInfo' => $dailyspecialInfo[0], 'activeCategory' => $allactivecategories, 'allProducts' => $allproducts, 'allcategories' => $finalCatData]);
     } else {
         return view('Supplier/Views/dailyspecial/editDailySpecial');
     }
 }
 public function editFlashsale(Request $request, $fid)
 {
     $objCategoryModel = ProductCategory::getInstance();
     $objCampaignModel = Campaigns::getInstance();
     $postData = $request->all();
     $supplierId = Session::get('fs_supplier')['id'];
     if ($request->isMethod('POST')) {
         $rules = array('campaign_name' => 'unique:campaigns,campaign_name,' . $fid . ',campaign_id', 'flashsale_image' => 'image');
         $validator = Validator::make($request->all(), $rules);
         if ($validator->fails()) {
             return Redirect::back()->withErrors($validator)->withInput();
         } else {
             if (Input::hasFile('flashsale_image')) {
                 $filePath = uploadImageToStoragePath(Input::file('flashsale_image'), 'flashsale', 'flashsale_' . $supplierId . '_' . time() . ".jpg");
                 $data['campaign_banner'] = $filePath;
             }
             $postData = $request->all();
             $data['campaign_name'] = $postData['campaign_name'];
             $data['for_shop_id'] = $postData['suppliershop'];
             $data['discount_type'] = $postData['discounttype'];
             if ($postData['discounttype'] == 1) {
                 $data['discount_value'] = $postData['flatdiscount'];
             } else {
                 $data['discount_value'] = $postData['percentagediscount'];
             }
             $validFrom = strtotime(str_replace("-", "", $postData['availablefromdate']));
             $validTo = strtotime(str_replace("-", "", $postData['availableuptodate']));
             $data['available_from'] = $validFrom;
             $data['available_upto'] = $validTo;
             $categ = $postData['productcategories'];
             $data['for_category_ids'] = implode(",", $categ);
             $where = ['rawQuery' => 'campaign_id = ?', 'bindParams' => [$fid]];
             $campaignUpdate = $objCampaignModel->updateFlashsaleStatus($data, $where);
             if ($campaignUpdate) {
                 if (isset($filePath)) {
                     deleteImageFromStoragePath($postData['oldImage']);
                 }
                 return Redirect::back()->with(['status' => 'success', 'msg' => 'FlashSale Added Successfully.']);
             } else {
                 return Redirect::back()->with(['status' => 'error', 'msg' => 'Some Error try again.']);
             }
         }
     }
     $where = ['rawQuery' => 'campaign_id = ?', 'bindParams' => [$fid]];
     $selectedColumn = ['campaigns.*', 'users.username'];
     $flashsaleInfo = $objCampaignModel->getAllFlashsaleDetails($where, $selectedColumn);
     if (isset($flashsaleInfo) && !empty($flashsaleInfo)) {
         foreach ($flashsaleInfo as $flashkey => $flashval) {
             $categoryIds = $flashval->for_category_ids;
             $where = ['rawQuery' => 'category_id IN(' . $categoryIds . ')'];
             $getcategory = $objCategoryModel->getCategoryNameById($where);
             foreach ($getcategory as $catkey => $catval) {
                 $flashsaleInfo[$flashkey]->category = $catval->category_name;
             }
         }
         //            echo'<pre>';print_r($flashsaleInfo);die("dxv");
         $where = ['rawQuery' => 'category_status = ? AND parent_category_id = ?', 'bindParams' => [1, 0]];
         $selectedColumn = ['category_id', 'category_name', 'category_status', 'for_shop_id'];
         $allactivecategories = $objCategoryModel->getAllMainCategories($where, $selectedColumn);
         return view('Supplier/Views/flashsale/editFlashsale', ['flashsaleDetails' => $flashsaleInfo[0], 'activeflashsale' => $allactivecategories]);
     } else {
         return view('Supplier/Views/flashsale/editFlashsale');
     }
 }