<?php

/**
 * @id 28
 * @name 联系方式设置
 * @version 1.0.0
 * @description 提供网站在线客服功能，支持多种联系方式，包括微信或QQ等，方便访客快速联系
 * @price 0
 * @type 工具
 * @website 
 * @width 780
 * @height 510
 * @thumbnail /img/id28.png
 */

$plugin_config = <<<EOT
[config_params]
公司名称 = 温州启肯网络有限公司
官方网站 = http://www.qiken.com
在线客服链接 = http://www.qiken.com/chat.html
服务时间 = 13:00 - 24:00
手机号 = 13738373177
微信二维码 = /assets/images/weixin1.png
QQ号1 = 328493711
QQ号1二维码 = /assets/images/qq1.png
QQ号2 = 25479460
QQ号2二维码 = 
QQ群二维码 = /assets/images/qqqun1.png
邮箱 = 328493711@qq.com
[/config_params]
EOT;

$config = array();
if (preg_match('/\[config_params\](.*?)\[\/config_params\]/s', $plugin_config, $matches)) {
    $lines = explode("\n", $matches[1]);
    foreach ($lines as $line) {
        $line = trim($line);
        if (empty($line)) continue;
        if (strpos($line, ' = ') !== false) {
            list($key, $value) = explode(' = ', $line, 2);
            $config[trim($key)] = trim($value);
        }
    }
}

if (isset($_GET['plugin']) && $_GET['plugin'] == '28') {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://' . $_SERVER['HTTP_HOST'] . '/admin/Plugin.php');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'action=load_plugin_settings&plugin=28_contactset');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    $response = curl_exec($ch);
    if ($response) {
        $data = json_decode($response, true);
        if (isset($data['success']) && $data['success'] && isset($data['params'])) {
            foreach ($data['params'] as $param) {
                if (isset($param['name']) && isset($param['value'])) {
                    $config[$param['name']] = $param['value'];
                }
            }
        }
    }
    curl_close($ch);
}

// 显示配置表单
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">

    <style>
        .config-form {
            background: #fff;
            border-radius: 8px;
            padding: 30px;
            max-width: 700px;
            margin: 0 auto;
            border: 1px solid #e8e8e8;
            position: relative;
            z-index: 1;
            overflow: visible;
            margin-right: 10px;
        }

        .form-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #e8e8e8;
        }

        .form-row {
            display: flex;
            margin-bottom: 15px;
        }

        .form-group {
            flex: 1;
            margin-bottom: 15px;
        }

        .form-row .form-group {
            margin-bottom: 0;
        }

        .form-row .form-group:not(:last-child) {
            margin-right: 20px;
        }

        select {
            appearance: none;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23666' d='M6 9l-4-4h8l-4 4z'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 12px center;
            padding-right: 32px;
        }

        label {
            display: block;
            font-size: 14px;
            color: #555;
            margin-bottom: 8px;
            font-weight: 500;
        }

        input[type="text"],
        select {
            width: 100%;
            height: 36px;
            padding: 0 12px;
            border: 1px solid #d9d9d9;
            border-radius: 4px;
            font-size: 14px;
            color: #333;
            transition: all 0.3s;
            box-sizing: border-box;
        }

        input[type="text"]:focus,
        select:focus {
            border-color: #40a9ff;
            outline: none;
            box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
        }

        .btn-save {
            background: #1890ff;
            color: #fff;
            border: none;
            padding: 0 32px;
            height: 36px;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            box-shadow: 0 2px 8px rgba(24, 144, 255, 0.3);
        }

        .btn-save:hover {
            background: #40a9ff;
        }

        .btn-save:active {
            background: #096dd9;
        }

        .btn-cancel {
            padding: 0 32px;
            height: 36px;
            font-size: 16px;
            border: 1px solid #6b7280;
            border-radius: 4px;
            color: #fff;
            background-color: #6b7280;
            cursor: pointer;
            transition: all 0.3s;
        }

        .btn-cancel:hover {
            background-color: #4b5563;
            border-color: #4b5563;
        }

        ::-webkit-scrollbar {
            width: 8px;
            height: 8px;
        }

        ::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 4px;
        }

        ::-webkit-scrollbar-thumb {
            background: #c1c1c1;
            border-radius: 4px;
        }

        ::-webkit-scrollbar-thumb:hover {
            background: #a8a8a8;
        }
    </style>
</head>

<body>
    <div class="config-form">

        <!-- 前台模板调用示例 -->
        <div style="margin-bottom: 20px; padding: 15px; background: #f0f8ff; border-radius: 6px; border: 1px solid #d1e7fd;">
            <div style="display: flex; align-items: center; margin-bottom: 0px;">
                <span style="font-size: 14px; color: #1890ff; font-weight: 600;">前台模板页面，可调用本工具任何参数 示例：</span>
                <span style="margin-left: 10px; font-family: 'Courier New', monospace; font-size: 13px; color: #333;">{qk_Plugin:ID28_'官方网站'}</span>
            </div>

        </div>

        <form id="kefuConfigForm">
            <!-- 第一行：公司名称和官方网站 -->
            <div class="form-row">
                <div class="form-group">
                    <label>公司名称</label>
                    <input type="text" name="公司名称" value="<?php echo htmlspecialchars($config['公司名称'] ?? '温州启肯网络有限公司'); ?>">
                </div>
                <div class="form-group">
                    <label>官方网站</label>
                    <input type="text" name="官方网站" value="<?php echo htmlspecialchars($config['官方网站'] ?? ''); ?>" placeholder="请输入官网地址">
                </div>
            </div>

            <!-- 第二行：在线客服链接和服务时间 -->
            <div class="form-row">
                <div class="form-group">
                    <label>在线客服链接</label>
                    <input type="text" name="在线客服链接" value="<?php echo htmlspecialchars($config['在线客服链接'] ?? 'http://www.qiken.com/chat.html'); ?>">
                </div>
                <div class="form-group">
                    <label>服务时间</label>
                    <input type="text" name="服务时间" value="<?php echo htmlspecialchars($config['服务时间'] ?? '13:00 - 24:00'); ?>" placeholder="例如：13:00 - 24:00">
                </div>
            </div>

            <!-- 第三行：手机号和微信二维码 -->
            <div class="form-row">
                <div class="form-group">
                    <label>手机号</label>
                    <input type="text" name="手机号" value="<?php echo htmlspecialchars($config['手机号'] ?? '13738373177'); ?>">
                </div>
                <div class="form-group">
                    <label>微信二维码</label>
                    <input type="text" name="微信二维码" value="<?php echo htmlspecialchars($config['微信二维码'] ?? ''); ?>" placeholder="图片地址">
                </div>
            </div>

            <!-- 第四行：QQ号1和QQ号1二维码 -->
            <div class="form-row">
                <div class="form-group">
                    <label>QQ号1</label>
                    <input type="text" name="QQ号1" value="<?php echo htmlspecialchars($config['QQ号1'] ?? '328493711'); ?>">
                </div>
                <div class="form-group">
                    <label>QQ号1二维码</label>
                    <input type="text" name="QQ号1二维码" value="<?php echo htmlspecialchars($config['QQ号1二维码'] ?? ''); ?>" placeholder="图片地址">
                </div>
            </div>

            <!-- 第五行：QQ号2和QQ号2二维码 -->
            <div class="form-row">
                <div class="form-group">
                    <label>QQ号2</label>
                    <input type="text" name="QQ号2" value="<?php echo htmlspecialchars($config['QQ号2'] ?? '25479460'); ?>">
                </div>
                <div class="form-group">
                    <label>QQ号2二维码</label>
                    <input type="text" name="QQ号2二维码" value="<?php echo htmlspecialchars($config['QQ号2二维码'] ?? ''); ?>" placeholder="图片地址">
                </div>
            </div>

            <!-- 第六行：QQ群和邮箱 -->
            <div class="form-row">
                <div class="form-group">
                    <label>QQ群二维码</label>
                    <input type="text" name="QQ群" value="<?php echo htmlspecialchars($config['QQ群'] ?? ''); ?>" placeholder="图片地址">
                </div>
                <div class="form-group">
                    <label>邮箱</label>
                    <input type="text" name="邮箱" value="<?php echo htmlspecialchars($config['邮箱'] ?? '328493711@qq.com'); ?>">
                </div>
            </div>



            <div style="display: flex; justify-content: flex-end; gap: 12px; margin-top: 30px;">
                <button type="button" class="btn-cancel">取消</button>
                <button type="button" class="btn-save">保存设置</button>
            </div>
        </form>

    </div>

    <script>
        window.callParentToast = function(message, options = {}) {
            window.parent.postMessage({
                type: 'toast',
                func: 'showToast',
                message: message,
                options: options
            }, window.parent.location.origin);
        }

        const form = document.getElementById('kefuConfigForm');
        const saveBtn = document.querySelector('.btn-save');
        const cancelBtn = document.querySelector('.btn-cancel');

        saveBtn.addEventListener('click', function() {
            const formData = new FormData(form);
            const settings = {};

            for (const [name, value] of formData.entries()) {
                settings[name] = value;
            }

            fetch('/admin/Plugin.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: 'action=save_plugin_settings&plugin=28_contactset&settings=' + encodeURIComponent(JSON.stringify(settings))
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        callParentToast('配置保存成功', {
                            type: 'success'
                        });
                    } else {
                        callParentToast('配置保存失败: ' + data.message, {
                            type: 'error'
                        });
                    }
                })
                .catch(error => {
                    callParentToast('保存失败: 网络错误', {
                        type: 'error'
                    });
                });
        });

        cancelBtn.addEventListener('click', function() {
            if (window.parent) {
                try {
                    const closeButton = window.parent.document.querySelector('.modal-close');
                    if (closeButton) {
                        closeButton.click();
                        return;
                    }

                    const cancelButton = window.parent.document.querySelector('.close-modal');
                    if (cancelButton) {
                        cancelButton.click();
                        return;
                    }

                    const modal = window.parent.document.querySelector('.modal');
                    if (modal) {
                        modal.style.display = 'none';
                    }
                } catch (error) {
                    console.error('关闭模态框失败:', error);
                }
            }
        });
    </script>
</body>

</html>
<?php
