/**
 * Edit Shop Product action by ajax
 */
function editProduct()
{
    $userID = buckys_is_logged_in();
    if (!$userID) {
        return;
    }
    $shopProductIns = new BuckysShopProduct();
    $inputValidFlag = true;
    $requiredFields = ['title', 'subtitle', 'description', 'category', 'return_policy', 'shipping_price', 'price'];
    foreach ($requiredFields as $requiredField) {
        if ($_REQUEST[$requiredField] == '') {
            $inputValidFlag = false;
        }
    }
    $categoryClass = new BuckysShopCategory();
    $category = $categoryClass->getCategoryByID($_REQUEST['category']);
    if (!$category['isDownloadable'] && $_REQUEST['location'] == '') {
        $inputValidFlag = false;
    } else {
        if ($category['isDownloadable'] == 1) {
            $_REQUEST['location'] = 0;
        }
    }
    if (isset($_REQUEST['price']) && (!is_numeric($_REQUEST['price']) || $_REQUEST['price'] <= 0)) {
        $inputValidFlag = false;
    }
    $shippingPriceList = [];
    if (isset($_REQUEST['shipping_price'])) {
        $shippingPriceList = json_decode($_REQUEST['shipping_price'], true);
        if (!is_array($shippingPriceList) || count($shippingPriceList) < 1) {
            $inputValidFlag = false;
        }
    }
    $actionType = get_secure_string($_REQUEST['type']);
    $paramProdID = get_secure_integer($_REQUEST['productID']);
    $listingFeeType = null;
    $data = [];
    $editableFlag = false;
    if ($actionType == 'relist') {
        $shopItemData = $shopProductIns->getProductById($paramProdID, true);
        if (!$shopItemData) {
            echo json_encode(['success' => 0, 'msg' => 'You could not relist this item.']);
            exit;
        }
        $listingFeeType = get_secure_integer($_REQUEST['listing_fee_type']);
        if ($listingFeeType === null) {
            $inputValidFlag = false;
        } else {
            //check if you can relist them
            if ($shopProductIns->hasMoneyToListProduct($userID, $listingFeeType)) {
                //Ok you can relist the product
            } else {
                echo json_encode(['success' => 0, 'msg' => 'You could not relist this item. You have no credits or bitcoin.']);
                exit;
            }
        }
        //you can relist this item
        $data['createdDate'] = date('Y-m-d H:i:s');
        if ($shopItemData['userID'] == $userID) {
            $editableFlag = true;
        } else {
            $editableFlag = false;
        }
    } else {
        $shopItemData = $shopProductIns->getProductById($paramProdID, false);
        if ($shopItemData && $shopItemData['userID'] == $userID) {
            $editableFlag = true;
        }
    }
    if ($inputValidFlag) {
        if ($editableFlag) {
            $data['title'] = get_secure_string($_REQUEST['title']);
            $data['subtitle'] = get_secure_string($_REQUEST['subtitle']);
            $data['description'] = get_secure_string($_REQUEST['description']);
            $data['catID'] = get_secure_string($_REQUEST['category']);
            $data['images'] = get_secure_string($_REQUEST['images']);
            $data['locationID'] = get_secure_string($_REQUEST['location']);
            $data['returnPolicy'] = get_secure_string($_REQUEST['return_policy']);
            $data['price'] = get_secure_string($_REQUEST['price']);
            $data['listingDuration'] = get_secure_string($_REQUEST['listing_duration']);
            $data['expiryDate'] = $data['listingDuration'] == -1 ? '0000-00-00 00:00:00' : date('Y-m-d H:i:s', time() + 3600 * 24 * $data['listingDuration']);
            $data['images'] = moveShopTmpImages($data['images']);
            if ($data['images'] === false) {
                echo json_encode(['success' => 0, 'msg' => 'Something goes wrong, please contact administrator.']);
                exit;
            }
            if ($actionType == 'relist') {
                $flag = $shopProductIns->payListingFee($userID, $paramProdID, $listingFeeType);
                if (!$flag) {
                    echo json_encode(['success' => 0, 'msg' => 'You could not relist this item. You have no credits or bitcoin.']);
                    exit;
                }
            }
            if ($category['isDownloadable'] == 1 && !empty($_REQUEST['filename'])) {
                if (!$_REQUEST['filename'] || file_exists(DIR_FS_SHOP_IMG_TMP . $_REQUEST['filename'])) {
                    echo json_encode(['success' => 0, 'msg' => 'Please select a zip file.']);
                    exit;
                }
                $data['isDownloadable'] = 1;
                $filename = moveShopTmpProduct($_REQUEST['filename']);
                //Remove Old File
                @unlink(DIR_FS_SHOP_PRODUCTS . $shopItemData['fileName']);
                $data['fileName'] = $filename;
            }
            $shopProductIns->updateProduct($paramProdID, $data);
            $shopProductIns->updateShippingPrice($paramProdID, $shippingPriceList);
            echo json_encode(['success' => 1, 'msg' => 'An item has been updated successfully.']);
        } else {
            echo json_encode(['success' => 0, 'msg' => "You don't have permission."]);
        }
    } else {
        //error
        echo json_encode(['success' => 0, 'msg' => 'Please input required field(s).']);
    }
}