<?php
/**
 * 添加货源
 */
if (!empty($_GET['id'])) {
    $title = '编辑货源-' . $_GET['id'];
} else {
    $title = '添加对接货源';
}
include "header.php";
if (!empty($_QET['id'])) {
    $_QET['id'] = (int)$_QET['id'];
    $SQ = $DB->get_row("SELECT * FROM `sky_shequ` WHERE `id` = '{$_QET['id']}' ");
    if (!$SQ) show_msg('货源->' . $_QET['id'] . '不存在', '货源->' . $_QET['id'] . '不存在请检查是否访问有误?', 3);
    if (!strstr($SQ['url'], 'http://') && !strstr($SQ['url'], 'https://')) { #未包含http://修正
        $SQ['url'] = 'http://' . $SQ['url'] . '/';
        @$DB->query("UPDATE `sky_shequ` SET `url`='{$SQ['url']}' WHERE (`id`='{$_QET['id']}')");
    }
}
?>

<div class="row">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-12">
                        <h3 class="mb-3 header-title text-success"><?= $title ?></h3>
                        <p class="text-muted">
                            根据下面<code>提示</code>输入对应内容！,目前仅支持亿乐玖伍社区,其他对接后续添加
                        </p>
                        <form class="form-horizontal layui-form">
                            <div class="form-group mb-3">
                                <label for="example-input-normal" style="font-weight: 500">请选择对接货源类型</label>
                                <select class="custom-select mt-3" name="type" lay-filter="shequ">
                                    <option></option>
                                    <option value="1" <?= $SQ['type'] == 1 ? 'selected' : null ?>>玖伍社区</option>
                                    <option value="2" <?= $SQ['type'] == 2 ? 'selected' : null ?>>亿乐社区</option>
                                    <option value="3" <?= $SQ['type'] == 3 ? 'selected' : null ?>>小储系统</option>
                                    <option value="4" <?= $SQ['type'] == 4 ? 'selected' : null ?>>卡易信</option>
                                </select>
                            </div>

                            <div class="form-group mb-3" id="pattern" style="display: none">
                                <label for="example-input-normal" style="font-weight: 500">数据获取/货源对接</label>
                                <select class="custom-select mt-3" name="pattern">
                                    <option value="1" <?= $SQ['pattern'] == '1' ? 'selected' : null ?>>本地对接</option>
                                    <option value="2" <?= $SQ['pattern'] == '2' ? 'selected' : null ?>>云端代理</option>
                                </select>
                            </div>

                            <div class="form-group mb-3" id="url" style="display: none">
                                <label for="example-input-normal" style="font-weight: 500"></label>
                                <input type="text" name="url" lay-verify="required"
                                       class="form-control" value="<?= $SQ['url'] ?>"
                                       placeholder="">
                            </div>

                            <div class="form-group mb-3" id="user" style="display: none">
                                <label for="example-input-normal" style="font-weight: 500"></label>
                                <input type="text" name="user" lay-verify="required"
                                       class="form-control" value="<?= $SQ['username'] ?>"
                                       placeholder="">
                            </div>

                            <div class="form-group mb-3" id="pass" style="display: none">
                                <label for="example-input-normal" style="font-weight: 500"></label>
                                <input type="text" name="pass" lay-verify="required"
                                       class="form-control" value="<?= $SQ['password'] ?>"
                                       placeholder="">
                            </div>

                            <div class="form-group mb-3" id="secrer" style="display: none">
                                <label for="example-input-normal" style="font-weight: 500"></label>
                                <input type="text" name="secret"
                                       class="form-control" value="<?= $SQ['secret'] ?>"
                                       placeholder="">
                            </div>

                            <button type="submit" lay-submit lay-filter="hy"
                                    class="btn btn-block btn-xs btn-success"><?= isset($_QET['id']) ? '保存货源数据' : '添加一个新的货源' ?>
                            </button>

                        </form>

                        <div class="mt-3" style="display: none;" id="ip">当前服务器IP为：<span
                                    class="badge badge-danger-lighten" id="ips">载入中</span> (可能 IP地址 有所误差,请以实际为准！)
                        </div>
                        <div class="mt-3" style="display: none" id="tonxit">推荐同系统对接货源地址：<span
                                    class="badge badge-success-lighten" onclick="window.open('http://9090ds.cn/')">小储官方货源站 9090ds.cn </span>
                            (官方货源站，质量保证！)
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<?php include "bottom.php"; ?>

<script>
    $(function (e) {
        console.log(e)
        layui.use('form', function () {
            var form = layui.form;
            form.on('submit(hy)', function (data) {
                layer.alert('是否要执行当前操作？', {
                    icon: 3, btn: ['确定', '取消'], btn1: function (layero, index) {
                        var index = layer.msg('数据保存中,请稍后...', {icon: 16, time: 999999});
                        if (dis != '') {
                            data.field['dis'] = dis;
                        }
                        $.post('ajax.php?act=source_add', data.field, function (res) {
                            if (res.code == 1) {
                                layer.close(index);
                                layer.alert(res.msg, {
                                    btn: ['继续', '返回列表'],
                                    btn1: function (layero, index) {
                                        location.reload();
                                    },
                                    btn2: function (layero, index) {
                                        location.href = 'admin.source.list.php'
                                    }
                                });
                            } else {
                                layer.close(index);
                                layer.alert(res.msg, {
                                    btn1: function (layero, index) {
                                        location.reload();
                                    }
                                });
                            }
                        });
                    }
                });
                return false;
            })

            form.on('select(shequ)', function (data) {
                a.b(data.value)
            });
        })
        var a = {
            b: function (a) {
                switch (a) {
                    case '1': //玖伍
                        $("#url,#user,#pass,#pattern").show();
                        $("#secrer,#ip,#tonxit").hide();
                        $("#url label").text('社区域名');
                        $("#url input").attr('placeholder', '请将对应玖伍社区的域名填写完整！')
                        $("#user label").text('登陆账号');
                        $("#user input").attr('placeholder', '请填写好此社区的登陆账号！');
                        $("#pass label").text('登陆密码');
                        $("#pass input").attr('placeholder', '请填写好此社区的登陆密码！');
                        break;
                    case '2': //亿乐
                        $("#url,#user,#pass,#ip").show();
                        $("#secrer,#tonxit,#pattern").hide();
                        $("#url label").text('社区域名');
                        $("#url input").attr('placeholder', '请将对应亿乐社区的域名填写完整！')
                        $("#user label").text('TokenID');
                        $("#user input").attr('placeholder', '请填写好此社区的TokenID！');
                        $("#pass label").text('密匙');
                        $("#pass input").attr('placeholder', '请填写好此社区的对接密匙！');
                        break;
                    case '3': //小储
                        $("#url,#user,#tonxit,#pass,#ip").show();
                        $("#secrer,#pattern").hide();
                        $("#url label").text('货源域名');
                        $("#url input").attr('placeholder', '请将对应小储系统的域名填写完整！')
                        $("#user label").text('对接ID (用户后台右上角获取)');
                        $("#user input").attr('placeholder', '请填写好此您在用户后台获取的ID！');
                        $("#pass label").text('对接KEY(用户后台右上角获取)');
                        $("#pass input").attr('placeholder', '请填写好您在用户后台获取的KEY！');
                        break;
                    case '4': //卡易信
                        $("#url,#user,#secrer,#pass,#pattern").show();
                        $("#tonxit,#ip").hide();
                        $("#url label").text('卡易信域名');
                        $("#url input").attr('placeholder', '请将对应卡易信域名填写完整！')
                        $("#user label").text('登陆账号');
                        $("#user input").attr('placeholder', '请填写好此对接货源的登陆账号！');
                        $("#pass label").text('登陆密码');
                        $("#pass input").attr('placeholder', '请填写此对接货源的登陆密码！');
                        $("#secrer label").text('交易密码');
                        $("#secrer input").attr('placeholder', '请填写好交易密码,若无可留空！');
                        break;
                    default:
                        $("#secrer,#url,#user,#pass,#ip,#tonxit,#pattern").hide();
                        break;
                }
            }
        }
        <?php
        if (!empty($_QET['id'])) {
            echo '
            var dis = ' . $_QET['id'] . ';
            a.b(\'' . $SQ['type'] . '\');
            ';
        } else {
            echo 'var dis = "";';
        }
        ?>
    })
    $.post('ajax.php?act=admin_ip', function (res) {
        $("#ips").text(res.msg);
    })
</script>


