<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>修改收款终端</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
    <style>
        .inputTxt {
            margin-left: 10px;
        }
    </style>
</head>

<body>
    <form id="edit-channel" class="layui-form layui-form-pane" action="" lay-filter="edit-channel">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">平台</label>
                    <div class="layui-input-block">
                        <div class="layui-form-mid inputTxt">
                            <?php echo $platform ?>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <div class="layui-form-mid inputTxt">
                            <?php echo $account ?>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">终端编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="channel" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">收款样式</label>
                    <div class="layui-input-block">
                        <select name="type" lay-filter="type">
                            <option value="">请选择</option>
                            <option value="0">付款链接</option>
                            <option value="1">图片地址</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">收款码</label>
                    <div class="layui-input-block">
                        <input type="text" name="qrcode" autocomplete="off" lay-affix="upload-drag"
                            lay-filter="scanning" class="layui-input" data-type="<?php echo match ((int)$type) {0 => 'link',1 => 'img',default => ''}; ?>">
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">最后使用</label>
                    <div class="layui-input-block">
                        <div class="layui-form-mid inputTxt">
                            <?php echo $last_time ?>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">启用</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="state" class="layui-input" lay-skin="switch" lay-text="启用|禁用">
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>

    <script src="/component/layui/layui.js"></script>
    <script src="/component/pear/pear.js"></script>
    <script>
        layui.use(['form'], function () {
            let form = layui.form;

            form.val('edit-channel', {
                "channel": "<?php echo $channel ?>",
                "qrcode": "<?php echo $qrcode ?>",
                "state": '<?php echo $state ?>' == 1 ? true : false,
                "type": '<?php echo $type ?>'
            });

            form.on('input-affix(scanning)', function (obj) {
                const type = obj.elem.getAttribute('data-type');
                if (type === '') {
                    layer.msg('请先选择二维码类型！', { tips: 2, time: 1200 });
                    return false;
                }
                if (type === 'img') {
                    uploadCodeImg(obj.elem);
                    return false;
                }
                if (type === 'link') {
                    window.open('https://cli.im/deqr', '_blank');
                    return false;
                }
            });
            form.on('select(type)', function (data) {
                const form = data.elem.form;
                const value = data.value;
                const inuptelem = form.querySelector('input[name="qrcode"]');
                if (value === '0') {
                    inuptelem.setAttribute('data-type', 'link');
                    return false;
                }
                if (value === '1') {
                    inuptelem.setAttribute('data-type', 'img');
                    return false;
                }
            });

            form.on('submit(save)', function (obj) {
                let field = obj.field;
                field.state = 'state' in field ? 1 : 0;
                field.id = '<?php echo $cid ?>';
                (async () => {
                    const url = '/api/PayManage/editChannel';
                    const info = field;
                    const res = await fetch(url, { method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(info) });
                    if (res.status !== 200) {
                        layer.msg('请求失败，请重试！', { tips: 2, time: 1200 });
                        return false;
                    }
                    const rec_info = await res.json();
                    if (rec_info.code === 0) {
                        layer.msg(rec_info.msg, { icon: 1, time: 1200 }, () => {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.window.location.reload();
                        });
                    } else {
                        layer.msg(rec_info.msg, { icon: 2, time: 1200 });
                    }
                })()
                return false;
            });
            // 上传二维码图片
            async function uploadCodeImg(elem) {
                const input = document.createElement('input');
                input.type = 'file';
                input.accept = 'image/*';
                input.onchange = async (event) => {
                    const file = event.target.files[0];
                    if (file) {
                        const formData = new FormData();
                        formData.append('codeimg', file);
                        try {
                            const response = await fetch('/api/PayManage/uploadQrcode', {
                                method: 'POST',
                                body: formData,
                            });
                            if (response.ok) {
                                const result = await response.json();
                                if (result.code === 0) {
                                    layer.msg(result.msg, { icon: 1, time: 1200 });
                                    elem.value = result.data.imgpath;
                                    return result.data.imgpath;
                                } else {
                                    layer.msg('上传失败：' + result.message, { tips: 2, time: 1200 });
                                }
                            } else {
                                layer.msg('上传失败：服务器错误', { tips: 2, time: 1200 });
                            }
                        } catch (error) {
                            layer.msg('上传失败：' + error.message, { tips: 2, time: 1200 });
                        }
                    }
                };
                input.click();
            }
        })
    </script>
    <script>
    </script>
</body>

</html>