<?php
//function getAddress($latitude, $longitude)
//{
//    if (!empty($latitude) && !empty($longitude)) {
//        //Send request and receive json data by address
//        $geocodeFromLatLong = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?latlng=' . trim($latitude) . ',' . trim($longitude) . '&sensor=false&key=AIzaSyC-pbf184_K3WfDf_rpZx4jSsvOZw-ie6o&language=en');
//        var_dump ($geocodeFromLatLong);
//        $output = json_decode($geocodeFromLatLong);
//        $status = $output->status;
//        //Get address from json data
//        $address = ($status == "OK") ? $output->results[1]->formatted_address : '';
//        //Return address of the given latitude and longitude
//        if (!empty($address)) {
//            return $address;
//        } else {
//            return false;
//        }
//    } else {
//        return false;
//    }
//}
//
//
//$latitude = '38.897952';
//$longitude = '-77.036562';
//$address = getAddress($latitude, $longitude);
//$address = $address ? $address : 'Not found';
//echo $address;

/*
require_once 'ResourcesHolder/core/DAL/DAL.php';
//require_once 'ResourcesHolder/core/DAL/sms/SendMessage.php';
//require_once 'ResourcesHolder/core/WebServiceSystem.php';
//require_once 'ResourcesHolder/core/DAL/public-function.php';

$DAL = new DAL('constant.php');
//$WS = new WebServiceSystem();
//$SMS = new SendMessage();

//$SMS->SendOneMessage("09133612196", "test");

//$SMS->SendOneMessage("09367153842", "test");
//$SMS->SendOneMessage("09367153842", "test");
$counter = 10;
$limit = 250;
$select = $DAL->Select(
    "SELECT * FROM `app_acceptor_list` WHERE
    `AppAccessMode`='sadra' LIMIT $limit OFFSET " . (($counter - 1) * $limit)
);

function getImageId($cat)
{

//    SELECT * FROM `wp_posts` WHERE post_type='product'
//AND post_date BETWEEN '2019-08-25 16:01:01' AND '2019-08-25 16:50:59'
//SELECT * FROM `wp_posts` WHERE post_type='product'
//AND post_date BETWEEN ('2019-08-25 16:40:59','2019-08-25 16:30:01')

    $nonIcon = array(
        array('wordpress' => 141, 'id' => 203, 'key' => "ابزار و يراق", 'value' => array(1573, 1574, 1575, 1576, 1577, 1578, 1579, 1580)),
        array('wordpress' => 140, 'id' => 189, 'key' => 'آتلیه', 'value' => array(1581, 1582, 1583, 1584, 1585, 1586, 1587, 1588, 1589, 1590, 1591)),
        array('wordpress' => 139, 'id' => 191, 'key' => 'آرایشگاه', 'value' => array(1591, 1592, 1593, 1594, 1595, 1596, 1597, 1598, 1599, 1600, 1601)),
        array('wordpress' => 14, 'id' => 168, 'key' => 'آرایشی بهداشتی', 'value' => array(1602, 1603, 1604, 1605, 1606, 1607, 1608, 1609, 1610, 1611, 1612, 1613)),
        array('wordpress' => 138, 'id' => 202, 'key' => 'الکتریکی', 'value' => array(1614, 1615, 1616, 1617, 1618, 1619, 1620, 1621)),
        array('wordpress' => 12, 'id' => 201, 'key' => 'آموزشگاه', 'value' => array(1622, 1623, 1624, 1625, 1626, 1627, 1628, 1629, 1630, 1631)),
        array('wordpress' => 137, 'id' => 204, 'key' => 'پست', 'value' => array(1632, 1633, 1634, 1635, 1636)),
        array('wordpress' => 114, 'id' => 181, 'key' => 'پوشاک', 'value' => array(1637, 1638, 1639, 1640, 1641, 1642, 1643, 1644, 1645, 1646)),
        array('wordpress' => 136, 'id' => 200, 'key' => 'تابلو و مهر سازی', 'value' => array(1647, 1648, 1649, 1650, 1651, 1652, 1653, 1654)),
        array('wordpress' => 135, 'id' => 188, 'key' => 'جواهرات', 'value' => array(1655, 1656, 1657, 1658, 1659, 1660, 1661, 1662, 1663, 1664)),
        array('wordpress' => 134, 'id' => 193, 'key' => 'چاپ و تکثیر', 'value' => array(1665, 1666, 1667, 1668, 1669, 1670, 1671, 1672, 1673, 1674)),
        array('wordpress' => 133, 'id' => 192, 'key' => 'خانه بازی', 'value' => array(1675, 1676, 1677, 1678, 1679, 1680, 1681, 1682, 1683, 1684)),
        array('wordpress' => 117, 'id' => 178, 'key' => 'خدمات تلفن همراه', 'value' => array(1685, 1686, 1687, 1688, 1689, 1690, 1691, 1692, 1693, 1694)),
        array('wordpress' => 112, 'id' => 180, 'key' => 'خدمات خودرو', 'value' => array(1695, 1696, 1697, 1698, 1699, 1700, 1701, 1702, 1703, 1704)),
        array('wordpress' => 131, 'id' => 176, 'key' => 'خدمات رفاهی', 'value' => array(1705, 1706, 1707, 1708, 1709, 1710, 1711, 1712, 1713, 1714)),
        array('wordpress' => 116, 'id' => 177, 'key' => 'خدمات کامپیوتری', 'value' => array(1715, 1716, 1717, 1718, 1719, 1720, 1721, 1722, 1723, 1724)),
        array('wordpress' => 130, 'id' => 199, 'key' => 'خشکشويي', 'value' => array(1725, 1726, 1727, 1728, 1729, 1730, 1731, 1732, 1733, 1734)),
        array('wordpress' => 129, 'id' => 196, 'key' => 'داروخانه', 'value' => array(1735, 1736, 1737, 1738, 1739, 1740, 1741, 1742, 1743, 1744)),
        array('wordpress' => 13, 'id' => 171, 'key' => 'درمان و سلامت', 'value' => array(1745, 1746, 1747, 1748, 1749, 1750, 1751, 1752, 1753, 1754)),
        array('wordpress' => 128, 'id' => 170, 'key' => 'دکوراسیون منزل', 'value' => array(1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762, 1763, 1764)),
        array('wordpress' => 8, 'id' => 174, 'key' => 'رستوران وفست فود', 'value' => array(1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774)),
        array('wordpress' => 127, 'id' => 197, 'key' => 'ساعت فروشی', 'value' => array(1775, 1776, 1777, 1778, 1779, 1780, 1781, 1782, 1783, 1784)),
        array('wordpress' => 119, 'id' => 186, 'key' => 'شیرینی سرا', 'value' => array(1785, 1786, 1787, 1788, 1789, 1790, 1791, 1792, 1793, 1794)),
        array('wordpress' => 126, 'id' => 198, 'key' => 'عطر فروشی', 'value' => array(1795, 1796, 1797, 1798, 1799, 1800, 1801, 1802, 1803, 1804)),
        array('wordpress' => 125, 'id' => 194, 'key' => 'کاشی و سرامیک', 'value' => array(1805, 1806, 1807, 1808, 1809, 1810, 1811, 1812, 1813, 1814)),
        array('wordpress' => 118, 'id' => 184, 'key' => 'کافی شاپ وبستنی', 'value' => array(1815, 1816, 1817, 1818, 1819, 1820, 1821, 1822, 1823, 1824)),
        array('wordpress' => 113, 'id' => 183, 'key' => 'کیف و کفش', 'value' => array(1825, 1826, 1827, 1828, 1829, 1830, 1831, 1832, 1833, 1834)),
        array('wordpress' => 121, 'id' => 175, 'key' => 'گل فروشی', 'value' => array(1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1843, 1844)),
        array('wordpress' => 122, 'id' => 179, 'key' => 'لوازم التحریر', 'value' => array(1845, 1846, 1847, 1848, 1849, 1850, 1851, 1852, 1853, 1854)),
        array('wordpress' => 123, 'id' => 182, 'key' => 'لوازم بهداشتی ساختمان', 'value' => array(1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864)),
        array('wordpress' => 120, 'id' => 169, 'key' => 'لوازم خانگی', 'value' => array(1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874)),
        array('wordpress' => 115, 'id' => 187, 'key' => 'مواد غذایی', 'value' => array(1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884))
    );
    $image = "";
    $cc = -1;

    $salt = 3776;


    for ($i = 0; $i < count($nonIcon); $i += 1) {
        $array = $nonIcon[$i];
        if ((int)$array['id'] == (int)$cat) {
            $key = $array['key'];
            $temp = $array['value'];
            $image = $salt + (int)$temp[array_rand($temp, 1)];
            $cc = $array['wordpress'];

        }
    }

    return array($image, $cc);
}


//var_dump($select);
$content = "";
$cou = 0;
foreach ($select as $acceptor) {
    $idd = getImageId($acceptor['sGuild']);

    $content .= "\$post_id =wp_insert_post(array(";
    $content .= "'post_title' =>'" . $acceptor['sName'] . "',";
    $content .= "'post_content' =>'<strong> " . $acceptor['sName'] . "</strong>',";
    $content .= "'post_status' => 'publish',";
    $content .= "'post_type' => 'product'";
    $content .= "));";
    $content .= "<br><br><br>";
    $content .= "<br>";
    $content .= "\$wpdb->get_results(\"INSERT into wp_term_relationships (object_id,term_taxonomy_id,term_order)VALUES(\$post_id,2,0),(\$post_id,124,0)";
    if ((int)$idd[1] > 0) {
        $content .= ",(\$post_id,$idd[1],0);\");";
    }
    // Add Featured Image to Post
//    $content .= "\$image_url = '" . $acceptor['sPhoto'] . "';"; // Define the image URL here
//    $img = explode('/', $acceptor['sPhoto']);
//    $content .= "<br>";
//    $content .= "\$image_name = '" . $img[count($img) - 1] . "';";
//    $content .= "<br>";
//    $content .= "\$upload_dir = wp_upload_dir();"; // Set upload folder
//    $content .= "<br>";
//    $content .= "\$image_data = file_get_contents(\$image_url);"; // Get image data
//    $content .= "<br>";
//    $content .= "\$unique_file_name = wp_unique_filename(\$upload_dir['path'], \$image_name);"; // Generate unique name
//    $content .= "<br>";
//    $content .= "\$filename = basename(\$unique_file_name);"; // Create image file name
//
//    // Check folder permission and define file location
//    $content .= "<br>";
//    $content .= "if (wp_mkdir_p(\$upload_dir['path'])) {";
//    $content .= "<br>";
//    $content .= "    \$file = \$upload_dir['path'] . '/' . \$filename;";
//    $content .= "<br>";
//    $content .= "} else {";
//    $content .= "<br>";
//    $content .= "    \$file = \$upload_dir['basedir'] . '/' . \$filename;";
//    $content .= "<br>";
//    $content .= "}";

    // Create the image  file on the server
//    $content .= "<br>";
//    $content .= "file_put_contents(\$file, \$image_data);";

    // Check image file type
//    $content .= "<br>";
//    $content .= "\$wp_filetype = wp_check_filetype(\$filename, null);";

    // Set attachment data
//    $content .= "<br>";
//    $content .= "\$attachment = array(";
//    $content .= "<br>";
//    $content .= "    'post_mime_type' => \$wp_filetype['type'],";
//    $content .= "<br>";
//    $content .= "    'post_title' => sanitize_file_name(\$filename),";
//    $content .= "<br>";
//    $content .= "    'post_content' => '',";
//    $content .= "<br>";
//    $content .= "    'post_status' => 'inherit'";
//    $content .= "<br>";
//    $content .= " );";
//
    // Create the attachment
//    $content .= "<br>";
//    $content .= "\$attach_id = wp_insert_attachment(\$attachment, \$file, \$post_id);";
//
//     Include image.php
//    $content .= "<br>";
//    $content .= "require_once(ABSPATH . 'wp-admin/includes/image.php');";
//
    // Define attachment metadata
//    $content .= "<br>";
//    $content .= "\$attach_data = wp_generate_attachment_metadata(\$attach_id, \$file);";

    // Assign metadata to attachment
//    $content .= "<br>";
//    $content .= "wp_update_attachment_metadata(\$attach_id, \$attach_data);";
//
    // And finally assign featured image to post


    $content .= "<br>";
    $content .= "set_post_thumbnail(\$post_id,$idd[0]);";
//    $content .= "set_post_thumbnail(\$post_id, \$attach_id);";


    $content .= "<br>";
    $content .= "wp_set_object_terms( \$post_id, 'simple', 'product_type' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_visibility', 'visible' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_stock_status', 'instock');";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, 'total_sales', '0' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_downloadable', 'no' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_virtual', 'yes' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_regular_price', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_sale_price', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_purchase_note', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_featured', 'no' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_weight', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_length', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_width', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_height', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_product_attributes', array() );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_sale_price_dates_from', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_sale_price_dates_to', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_price', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_sold_individually', '' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_manage_stock', 'no' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_backorders', 'no' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_stock', '' );";


    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, 'product_sale_lat', '" . $acceptor['Lat'] . "' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, 'product_sale_long', '" . $acceptor['Lng'] . "' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, 'seller_product_map_location', '" . str_replace("(", "", str_replace(")", "", $acceptor['sLocation'])) . "' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, 'number_percentage_manual', '" . $acceptor['sDiscount'] . "' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, 'percentage', '" . $acceptor['sDiscount'] . "' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, 'seller_address', '" . $acceptor['sAddress'] . "' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, 'seller_phone', '" . $acceptor['sNumber'] . "' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, 'service_hours', 'تمام وقت' );";
    $content .= "<br>";
    $content .= "update_post_meta( \$post_id, '_subtitle', 'فقط از طریق عضویت در کاشان کارت' );";
    $cou += 1;
    $content .= "echo $cou;";
}
echo $content;


//function buildTree(array $elements, $parentId = 0, $content = '')
//{
//
//    foreach ($elements as $element) {
//        if (is_object($element)) {
//            $content .= buildTree($element, $parentId + 1, $content);
//        } else {
//            $content = '';
//            for ($i = 0; $i < $parentId; $i += 1) {
//                $content .= "\t";
//            }
//            $content .= $element;
//        }
//        $content .= '<br>';
//    }
//    return $content;
//}
//
//echo buildTree();

//var_dump($_SERVER);
/*

$DLA = new DAL('constant.php');
$nc = ((int)$nc);
$query = "SELECT CONCAT(`client_fn`, ' ',`client_ln`) as `name` FROM `client_list_card` WHERE `card_number`='{$pan}' AND `client_nc`='{$nc}'";
$select = $DLA->Select($query);
if ($select && count($select) > 0) {
    try {
        $fullname = $select[0]['name'];
        var_dump($bot->CreateHTTPRequest('sendMessage', [
            'chat_id' => $bot->GetBotChatID(),
            'text' => TextTable("شمار کارت : $pan"),
            'reply_markup' => DefaultKeyBord(),
        ]));
        Send($bot, TextTable('منتظر بمانید...'), null);
        $array = array('Pan' => $pan);
        $array = array_merge($array, LoginDataWebServices);
        $data = $WS->BusApi()->json('CustBonServiceNafis', $array);
        $array = json_decode($data);
//            Send($bot, TextTable(json_encode($array)), null);
        if ($array[0]->error === 'no error') {
            if ($array[0]->response->error->ResponseCode === '0000') {
                $result = $array[0]->response->result;
                foreach ($result as $item) {
                    $text = "";
                    $text .= "\n" . $fullname;
                    $text .= "\n" . $item->BonTitle;
                    $text .= "\n" . "مبلغ بن (ريال)";
                    $text .= "\n" . $item->CrAmnt;
                    $text .= "\n" . "مانده (ريال)";
                    $text .= "\n" . $item->RemAmnt;
                    Send($bot, TextTable($text), null);
                }
            } else {
                Send($bot, TextTable($array[0]->response->error->ResponseMessage), null);
            }
        } else {
            Send($bot, TextTable("به شما بن تعلق نگرفته به امور شهرداری مراجعه نمایید."), null);
        }
    } catch (Exception $exception) {
        var_dump($bot->CreateHTTPRequest('sendMessage', [
            'chat_id' => $bot->GetBotChatID(),
            'parse_mode' => 'markdown',
            'text' => TextTable($exception->getMessage()),
            'reply_markup' => DefaultKeyBord(),
        ]));
    }
} else {
    var_dump($bot->CreateHTTPRequest('sendMessage', [
        'chat_id' => $bot->GetBotChatID(),
        'parse_mode' => 'markdown',
        'text' => TextTable("خطا"),
        'reply_markup' => DefaultKeyBord(),
    ]));
}
*/
