@{
    ViewBag.Title = "用户配置设置";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}
<style type="text/css">
    table tr td:first-child
    {
        text-align: right;
        width: 30%;
    }
</style>
<div class="wrapper wrapper-content" style="width: inherit;">
    <div class="ibox float-e-margins" style="width: inherit;">
        <table class="table table-bordered table-striped table-hover">
            <tr>
                <td>默认仓库：</td>
                <td>
                    <input type="text" readonly="true" id="txtStore" style="width: 400px">
                    <input type="hidden" id="StoreCode" />
                </td>
                <td>
                    <button id="selStock" class="glyphicon glyphicon-plus btn btn-default"></button>
                </td>
            </tr>
            <tr>
                <td>启用经销商实时价格跟踪：</td>
                <td>
                    <select name="regtype" id="UsePriceTrack" style="width: 160px" onchange="dpriceWChange()">
                        <option value="是">是</option>
                        <option value="否">否</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>设置全局价格方式：</td>
                <td>
                    <select name="regtype" id="dpriceW" style="width: 160px" onchange="dpriceWChange()">
                        <option value="1">选择相关售价类型</option>
                        <option value="2">启用客户价格类型跟踪</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>设置全局价格：</td>
                <td>
                    <select name="regtype" id="txtprice" disabled="true" style="width: 160px"></select>
                </td>
            </tr>
            <tr>
                <td>是否允许手机端下载数据使用全新升级：</td>
                <td>
                    <select name="regtype" id="IfNewDataUpdate" style="width: 160px" onchange="dpriceWChange()">
                        <option value="是">是</option>
                        <option value="否">否</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>启用基本信息授权控制：</td>
                <td>
                    <div class="radio i-checks">
                        <input type="radio" value="是" name="UserPower" id="AccessControl" />是
                        <input type="radio" value="否" name="UserPower" id="AccessControl2" />否
                    </div>
                </td>
            </tr>
            <tr>
                <td>启用商品选择，库存量有货、无货显示：</td>
                <td>
                    <div class="radio i-checks">
                        <input type="radio" value="是" name="StockStandards" id="StockStandards1" />是
                        <input type="radio" value="否" name="StockStandards" id="StockStandards2" />否
                    </div>
                </td>
            </tr>
            <tr>
                <td>看图开单展示的价格是客户的预设售价：</td>
                <td>
                    <div class="radio i-checks">
                        <input type="radio" value="是" name="IsDefaultPrice" id="IsDefaultPrice1" />是
                        <input type="radio" value="否" name="IsDefaultPrice" id="IsDefaultPrice2" />否
                    </div>
                </td>
            </tr>
            <tr>
                <td>上传首页图片：</td>
                <td>
                    <form id="firstImgForm" name="formfirstImg" action="../DealerEdition/UplodaFirstImg" enctype="multipart/form-data" method="post" target="ajaxUploadUplodaFirstImg">
                        <div style="width: 140px; height: 28px; padding: 0px; float: left" class="btn btn-default">
                            <input onchange="FirstImg()" name="firstImg" type="file" id="FirstfileImg" style="position: relative; opacity: 0; width: inherit; height: inherit; z-index: 5;" multiple />
                            <div style="position: relative; width: inherit; height: inherit; margin-top: -40px; z-index: 2;">
                                <span class="glyphicon glyphicon-folder-open" style="line-height: 45px;"> 选择上传图片</span>
                            </div>
                        </div>
                        &nbsp;&nbsp; <span id="fileFirstimg" class="btn btn-default" style="float: left" onclick="UploadFirstImg()">开始上传</span>

                        <iframe name="ajaxUploadUplodaFirstImg" hidden="hidden"></iframe>
                    </form>
                </td>
            </tr>
            <tr>
                <td>上传图片前预览:</td>
                <td>
                    <form id="imgformdata">
                        <div style="position: relative;" id="ccc">
                            <img id="firstimg" onclick="ClearfirtsImg()" width="150px" />
                        </div>
                    </form>
                </td>
            </tr>
            <tr>
                <td>已上传首页图片:</td>
                <td>
                    <div style="position: relative;" id="imgdiv">
                    </div>
                </td>
            </tr>
            <tr>
                <td>上传LOGO图片：</td>
                <td>
                    <form id="logoImgForm" name="formLogpImg" action="../DealerEdition/UplodaLogoImg" enctype="multipart/form-data" method="post" target="ajaxUploadUplodaLogoImg">
                        <div style="width: 140px; height: 28px; padding: 0px; float: left" class="btn btn-default">
                            <input onchange="LogoImg()" type="file" id="logofileImg" name="logofileImg" style="position: relative; opacity: 0; width: inherit; height: inherit; z-index: 5;" />
                            <div style="position: relative; width: inherit; height: inherit; margin-top: -40px; z-index: 2;">
                                <span class="glyphicon glyphicon-folder-open" style="line-height: 45px;"> 选择上传图片</span>
                            </div>
                        </div>
                        &nbsp;&nbsp; <span id="fileLogoimg" onclick="fileLogoimg()" style="float: left" class="btn btn-default"> 开始上传</span>
                        <div style="position: relative; width: 150px;margin-left:8px">
                            <img id="logoimg" width="150px" />
                            <div id="delicon" style="position: absolute; bottom: 0px; right: 0px; z-index: 99; width: 30px; height: 30px; border-radius: 50%;" onclick="ClearlogoImg()">
                                <img src="~/icon/删除.png" title="删除" style="cursor: pointer;" />
                            </div>
                        </div>
                        <iframe name="ajaxUploadUplodaLogoImg" hidden="hidden"></iframe>
                    </form>
            </tr>
            <tr>
                <td></td>
                <td>
                    <button id="btnSave" onclick="btnSave()" class="btn btn-default"><i class="glyphicon glyphicon-saved"></i>保存</button>
                </td>
            </tr>
        </table>
    </div>
</div>
<script type="text/javascript">
    function dataBind() {
        var data = parent.$("#businessTable").bootstrapTable('getData');
        for (var i = 0; i < data.length; i++) {
            if (data[i]["TypeName"] == 'DefaultStock') {
                var defaultStock = data[i]["TypeValues"];
                $("#StoreCode").val(defaultStock.substring(0, defaultStock.lastIndexOf(',')));
                $("#txtStore").val(defaultStock.substring(defaultStock.lastIndexOf(',') + 1));
            }
            else if (data[i]["TypeName"] == 'UsePriceTrack') {
                var UsePriceTrack = data[i]["TypeValues"];
                $("#UsePriceTrack").val(UsePriceTrack);
            }
            else if (data[i]["TypeName"] == 'IfNewDataUpdate') {
                var IfNewDataUpdate = data[i]["TypeValues"];
                $("#IfNewDataUpdate").val(IfNewDataUpdate);
            }
            else if (data[i]["TypeName"] == 'UserPower') {
                $("input[name='UserPower'][value='" + data[i]["TypeValues"] + "']").attr("checked", true);
            }
            else if (data[i]["TypeName"] == 'StockStandards') {  //启用商品选择，库存量有货、无货显示
                $("input[name='StockStandards'][value='" + data[i]["TypeValues"] + "']").attr("checked", true);
            }
            else if (data[i]["TypeName"] == 'IsDefaultPrice') {  //看图开单展示的价格是客户的预设售价
                $("input[name='IsDefaultPrice'][value='" + data[i]["TypeValues"] + "']").attr("checked", true);
            }
            else if (data[i]["TypeName"] == 'DefaultPriceWay') {
                var dpriceW = data[i]["TypeValues"];
                if (dpriceW == "启用客户价格类型跟踪") {
                    $("#dpriceW").val("2");
                    $("#txtprice").attr("disabled", "true");
                } else {
                    $("#txtprice").removeAttr("disabled");
                    $("#dpriceW").val("1");
                }

            }
            else if (data[i]["TypeName"] == 'Defaultpriceset') {
                if (data[i]["TypeValues"]) {
                    $("#txtprice").find("option[text=" + data[i]["TypeValues"] + "]").attr("selected", true);
                }
                //$("#txtprice").val(data[i]["TypeValues"].substring(0, data[i]["TypeValues"].lastIndexOf(',')));
            }
            else if (data[i]["TypeName"] == 'UploadMainLog') {
                if (data[i]["TypeValues"]) {
                    $("#logoimg").attr("src", "../UserData/" + data[i]["TypeValues"] + "?" + new Date().getTime());
                    $("#logoimg").error(function () {
                        $("#delicon").hide();
                        $("#logoimg").removeAttr("src");
                        $("#logoimg").removeAttr("title");
                        $("#logoimg").removeAttr("width");
                        $("#logoimg").removeAttr("height");
                    })
                } else {
                    $("#delicon").hide();
                }
            } 
        }
    }
    //全局价格改变时
    function dpriceWChange() {
        var dpriceW = $("#dpriceW").val()
        if (dpriceW == "2") {
            $("#txtprice").attr("disabled", "true");
        } else {
            $("#txtprice").removeAttr("disabled");
        }
    }
    //保存
    function btnSave() {
        var store = $("#StoreCode").val() + "," + $("#txtStore").val();//默认仓库
        var usePriceTrack = $("#UsePriceTrack").val();//启用经销商实时价格跟踪
        var ifNewDataUpdate = $("#IfNewDataUpdate").val();//是否允许手机端下载数据使用全新升级
        var dpriceW = $("#dpriceW").val() + "," + $("#dpriceW").find("option:selected").text();//设置全局价格方式
        var price = $("#txtprice").val() + "," + $("#txtprice").find("option:selected").text();//设置全局价格
        var UserPower = $("input[name='UserPower']:checked").val() //启用基本信息授权控制
        var StockStandards = $("input[name='StockStandards']:checked").val() //启用商品选择，库存量有货、无货显示
        var IsDefaultPrice = $("input[name='IsDefaultPrice']:checked").val() //看图开单展示的价格是客户的预设售价
        $.ajax({
            url: "../DealerEdition/SaveUserConfiguration",
            data: {
                store: store,
                usePriceTrack: usePriceTrack,
                ifNewDataUpdate: ifNewDataUpdate,
                dpriceW: dpriceW,
                price: price,
                UserPower: UserPower,
                StockStandards: StockStandards,
                IsDefaultPrice: IsDefaultPrice
            },
            datatype: "text",
            type: "post",
            success: function (data) {
                if (data == "ok") {
                    parent.$('#businessTable').bootstrapTable('refresh');
                    parent.layer.msg("保存成功", { icon: 1, time: 2000 })
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }
            }, error: function () {
                layer.msg("连接超时", { icon: 2, time: 2000 })
            }
        })
    }
    //上传LOGO图片预览
    function LogoImg() {
        if ($.ImgYulan(document.getElementById("logofileImg").files[0], "logoimg")) {
            $("#delicon").show();
        }
    }
    //去掉LOGO图片预览
    function ClearlogoImg() {
        var file = $("#logofileImg")
        if (file.val()) {
            file.after(file.clone().val(""));
            file.remove();
            $("#logoimg").removeAttr("src");
            $("#logoimg").removeAttr("title");
            $("#logoimg").removeAttr("width");
            $("#logoimg").removeAttr("height");
            $("#delicon").hide();
        } else {
            layer.confirm('确定删除LOGO图片?', { icon: 3, title: '提示' }, function (index) {
                $.ajax({
                    url: "../DealerEdition/DelUplodaLogoImg",
                    data: {
                        path: $("#logoimg").attr("src")
                    },
                    datatype: "text",
                    type: "post",
                    async: false,
                    success: function (data) {
                        if (data == "err") {
                            layer.msg("删除失败", { time: 2000 });
                            return;
                        }
                    }, error: function () {
                        layer.msg("删除失败", { time: 2000 });
                        return;
                    }
                });
                $("#logoimg").removeAttr("src");
                $("#logoimg").removeAttr("title");
                $("#logoimg").removeAttr("width");
                $("#logoimg").removeAttr("height");
                $("#delicon").hide();
                layer.close(index);
            });
        }
    }
    //上传LOGO图片
    function fileLogoimg() {
        if (document.getElementById("logofileImg").files.length < 1) {
            layer.msg("请选择图片", { icon: 2, time: 2000 });
            return;
        }
        $("#logoImgForm").submit();
    }
    //上传首页图片预览
    function FirstImg() {
        $.DuoImgsYulan(document.getElementById("FirstfileImg").files, "ccc");
    }
    //去掉首页图片预览
    function ClearfirtsImg() {
        var file = $("#FirstfileImg")
        file.after(file.clone().val(""));
        file.remove();
    }
    //上传首页图片
    function UploadFirstImg() {
        var formdata = new FormData($("#imgformdata"));
        var img = document.getElementsByName("loadimgs");
        if (img.length < 1) { layer.msg("请选择上传图片", { time: 2000, icon: 2 }); return };
        for (var i = 0; i < img.length; i++) {
            formdata.append("img" + i, img[i].src)
        }
        $.ajax({
            type: 'post',
            url: '../DealerEdition/UplodaFirstImg',
            data: formdata,
            cache: false,
            processData: false, // 不处理发送的数据，因为data值是Formdata对象，不需要对数据做处理
            contentType: false, // 不设置Content-type请求头
            success: function (data) {
                if (data=="ok") {
                    layer.msg("上传成功", { time: 2000, icon: 1 });
                    document.getElementById("ccc").innerHTML = "";
                    GetFirstImgs();
                    ClearfirtsImg();
                } else {
                    layer.msg(data, { time: 2000, icon: 2 });
                }
            },
            error: function () { layer.msg("连接超时", { time: 2000, icon: 2 }); }
        });
    }
    //获取已经上传首页图片
    function GetFirstImgs() {
        document.getElementById("imgdiv").innerHTML = "";
        $.ajax({
            url: "../DealerEdition/GetFirstImgs",
            dataType: "Json",
            type: "post",
            success: function (data) {
                if (data) {
                    data = jQuery.parseJSON(data);
                    if (data[0]["Feld1"]) $.BDimgYuLan("imgdiv", "../DealerEdition/DelFirstImg", "../UserData/MainImage/" + data[0]["Feld1"] + "?" + new Date().getTime(), data[0]["Feld1"]);
                    if (data[0]["Feld2"]) $.BDimgYuLan("imgdiv", "../DealerEdition/DelFirstImg", "../UserData/MainImage/" + data[0]["Feld2"] + "?" + new Date().getTime(), data[0]["Feld2"]);
                    if (data[0]["Feld3"]) $.BDimgYuLan("imgdiv", "../DealerEdition/DelFirstImg", "../UserData/MainImage/" + data[0]["Feld3"] + "?" + new Date().getTime(), data[0]["Feld3"]);
                }
            }
        });
    }
    $(function () {
        $.ajax({
            url: "../WLTManage/GetDefPriceID",
            dataType: "html",
            type: "post",
            async: false,
            success: function (data) {
                $("#txtprice").append(data);
            }
        });
        dataBind();
        $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });
        GetFirstImgs();
        //上传首页图片
        $("#btnimgUpload").on("click", function () {
            layer.open({
                type: 2,
                title: '图片上传',
                shadeClose: false,
                shade: 0.5,
                area: ['80%', '90%'],
                content: '../DealerEdition/FimgUpload'
            });
        });
        //仓库信息查询
        $("#selStock").on("click", function () {
            layer.open({
                type: 2,
                title: '仓库信息',
                shadeClose: false,
                shade: 0.5,
                area: ['80%', '90%'],
                content: '../CommonSelector/StoreSelector'
            });
        })
    });
</script>
