{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}
<style>
    #dataTable_filter div {
        padding: 0px 10px;
    }

    .operate a {
        padding: 5px 10px;
    }
    #infos{
        height: 666px;
        overflow-y: scroll;
    }
</style>
<div class="content-wrapper">
    <section class="content-header" style="padding: 49px 15px 0 15px;">
        <h1>
            <small><a class="btn btn-primary opraaa" opraaa="newqyk" onclick='AddAgencyHTML()'>新增代理机构信息</a></small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 代理机构增删改查</a></li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-body">
                        <table id="dataTable" class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th>代理机构名称</th>
                                <th>统一信用代码</th>
                                <th>单位类型</th>
                                <th>行政级别</th>
                                <th>省份</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
        </div>
    </section>

</div>
<div class="modal fade" id="info_data">
    <div class="modal-dialog">
        <form id="update-dataform" class="form-horizontal" role="form">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="info_title"></h4>
                </div>
                <div class="modal-body" id="infos">
                </div>
            </div>
            <!-- /.modal-content -->
        </form>
    </div>
    <!-- /.modal-dialog -->
</div>
{{template "dialog"}}
{{template "footer"}}
<script>
    menuActive("agency_info")
    var ttable = {};
    var editor_add = null;
    var editor_udapte = null;


    $(function () {
        ttable = $('#dataTable').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": true,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "serverSide": true,
            "ajax": {
                "url": "/admin/audit/query_agency/list",
                "type": "post",
            },
            "language": {
                "url": "/res/dist/js/dataTables.chinese.lang"
            },
            "columns": [
                {
                    "data": "agency_name", "width": "15%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return "<text  title="+val+">"+val+"</text >"
                    }
                },
                {
                    "data": "credit_no", "width": "10%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return "<text  title="+val+">"+val+"</text >"
                    }
                },
                {
                    "data": "type", "width": "10%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return "<text  title="+val+">"+val+"</text >"
                    }
                },
                {
                    "data": "ranks", "width": "10%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return "<text  title="+val+">"+val+"</text >"
                    }
                },
                {
                    "data": "province", "width": "10%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return "<text  title="+val+">"+val+"</text >"
                    }
                },
                {
                    "data": "_id", "width": "10%", render: function (val, a, row) {
                        var valueStr = JSON.stringify(row);
                        return "<div class='operate'>" +
                            "<a class='btn btn-primary' onclick='editdata(" + valueStr + ")'>编辑</a>&nbsp;" +
                            // "<a class='btn btn-default' onclick='showHylxs(\"" + val + "\")'>详细行业类型</a>&nbsp;" +
                            // "<a class='btn btn-sm btn-warning' onclick='showTels(\"" + val + "\")'>详细联系方式</a>&nbsp;" +
                            "<a class='btn btn-sm btn-danger' onclick='deleteQyk(\"" + val + "\")'>删除</a>&nbsp;" +
                            "</div>"
                    }
                }
            ]
        });
    });

    var vastrjson = [{
        "infoid": "招标信息id",
        "contact_person": "联系人",
        "contact_type": "联系人类型",
        "phone": "联系人电话",
        "topscopeclass": "项目类型"
    }];


    /*新增采购单位html*/
    function AddAgencyHTML() {
        $("#infos").html("");
        $("#info_title").html("新增代理机构信息");
        com ="<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\" style=\"color: red;\">机构名称：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_agency_name\" must=\"true\"></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">历史名称：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_history_name\" placeholder=\"多个;隔开\"></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">行政区划：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_area_code\" ></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">单位类型：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_type\"></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">行政级别：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_ranks\" ></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">省份：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_province\"></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">城市：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_city\"></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">区县：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_district\"></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">机构地址：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_address\"></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">公众号：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_wechat_accounts\" placeholder=\"多个;隔开\"></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">网址：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_website\"></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">联系方式：</label>" +
            "<div class=\"col-sm-10\" id='s_contact' style=\"height:250px\"></div>" +
            "<div class=\"row form-group\">" +
            "<button type='button' class='btn btn-sm btn-default col-md-offset-5 col-md-2'  data-dismiss='modal' aria-label='Close'>取消</button>" +
            "<button type='button' class='btn btn-sm btn-danger col-md-2' onclick='Add()'>新增代理机构</button></div>";


        $("#infos").html(com);
        $("#s_contact").html('');
        var container = document.getElementById('s_contact');
        showJson("add", vastrjson, container);
        $("#info_data").modal("show");
    }
    /*保存企业库*/
    function Add() {
        var agency_name = $("#s_agency_name").val();          //代理机构名称
        var history_name = $("#s_history_name").val();          //历史名称
        var area_code = $("#s_area_code").val();                //行政区划代码
        var type = $("#s_type").val();                          //单位类型
        var ranks = $("#s_ranks").val();                        //行政级别
        var province = $("#s_province").val();                  //省份
        var city = $("#s_city").val();                          //城市
        var district = $("#s_district").val();                  //区县
        var address = $("#s_address").val();                    //代理机构地址
        var wechat_accounts = $("#s_wechat_accounts").val();    //微信公众号
        var website = $("#s_website").val();                    //网址
        var contact = editor_add.getText();                     //联系人
        if (typeof contact == 'string') {
            try {
                var obj=JSON.parse(contact);
                if(typeof obj == 'object' && obj ){
                    // alert("格式正确");
                }else{
                    alert("联系人的表单json格式不正确");
                    return
                }
            } catch(e) {
                alert("联系人的表单json格式不正确");
                return
            }
        }
        if (agency_name === "") {
            alert("红色标签的表单不能为空！");
            return
        } else if (contact === JSON.stringify(vastrjson, null, 2)) {
            alert("联系人的表单未填写！");
            return
        }

        //新增代理机构api
        $.post("/admin/audit/agency_info/save", {
            "agency_name": agency_name,
            "history_name": history_name,
            "area_code": area_code,
            "type": type,
            "ranks": ranks,
            "province": province,
            "city": city,
            "district": district,
            "address": address,
            "wechat_accounts": wechat_accounts,
            "website": website,
            "contact":contact
        }, function (data) {
            if (data && data.rep) {
                alert("保存成功");
                $("#info_data").modal("hide");
                setTimeout(function () {
                    ttable.search(agency_name).draw();
                    //    history.go(0) //刷新本页
                }, 1500) //停1.5秒

            } else {
                alert("内部错误");
            }
        }, 'json')
    }


    //删除
    function deleteQyk(_id) {
        if (_id === "") {
            alert("参数id为空");
            return
        }
        if (confirm("您确认要提交此操作？")) {
            $.post("/admin/audit/agency_info/deleteQyk", {
                "_id": _id,
            }, function (data) {
                if (data && data.rep) {
                    setTimeout(function () {
                        alert("删除成功");
                        ttable.search("").draw();
                        //    history.go(0) //刷新本页
                    }, 800) //停1.5秒

                } else {
                    alert("内部错误");
                }
            }, 'json');
            ttable.draw();
        }
    }

    //编辑采购单位
    function editdata(_obj) {
        $("#infos").html("");
        $("#info_title").html("编辑采购单位信息");
        com ="<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\" style=\"color: red;\">机构名称：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_agency_name\" must=\"true\" readonly=\"true\" value='" + _obj.agency_name + "'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">历史名称：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_history_name\" value='" + _obj.history_name + "'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">行政区划：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_area_code\"  value='" + _obj.area_code + "'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">单位类型：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_type\" value='" + _obj.type + "'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">行政级别：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_ranks\" value='" + _obj.ranks + "' ></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">省份：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_province\" value='" + _obj.province + "'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">城市：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_city\" value='" + _obj.city + "'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">区县：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_district\" value='" + _obj.district + "'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">机构地址：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_address\" value='" + _obj.address + "'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">公众号：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_wechat_accounts\" placeholder=\"多个;隔开\" value='"+_obj.wechat_accounts.join(";")+"'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">网址：</label>" +
            "<div class=\"col-sm-10\"><input type=\"text\" class=\"form-control\" id=\"s_website\" value='" + _obj.website + "'></div></div>" +
            "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-2 control-label\">联系方式：</label>" +
            "<div class=\"col-sm-10\" id='s_contact' style=\"height:250px\"></div>" +
            "<div class=\"row form-group\">" +

            "<button type='button' class='btn btn-sm btn-default col-md-offset-2 col-md-2'  data-dismiss='modal' aria-label='Close'>取消</button>" +
            "<button type='button' class='btn btn-sm btn-danger col-md-2' onclick='updatedate(\"" + _obj._id + "\")'>更新</button></div>";

        $("#infos").html(com);
        var container = document.getElementById('s_contact');
        showJson("update", _obj.contact, container);
        $("#info_data").modal("show");
    }

    function updatedate(_id) {
        if (_id === "") {
            alert("id不存在！");
            return
        }
        var agency_name = $("#s_agency_name").val();          //采购单位名称
        var history_name = $("#s_history_name").val();          //历史名称
        var area_code = $("#s_area_code").val();                //行政区划代码
        var type = $("#s_type").val();                          //单位类型
        var ranks = $("#s_ranks").val();                        //行政级别
        var province = $("#s_province").val();                  //省份
        var city = $("#s_city").val();                          //城市
        var district = $("#s_district").val();                  //区县
        var address = $("#s_address").val();                    //代理机构地址
        var wechat_accounts = $("#s_wechat_accounts").val();    //微信公众号
        var website = $("#s_website").val();                    //网址
        var contact = editor_update.getText();                  //联系人
        if (typeof contact == 'string') {
            try {
                var obj=JSON.parse(contact);
                if(typeof obj == 'object' && obj ){
                    // alert("格式正确");
                }else{
                    alert("联系人的表单json格式不正确");
                    return
                }
            } catch(e) {
                alert("联系人的表单json格式不正确");
                return
            }
        }
        if (agency_name === "") {
            alert("红色标签的表单不能为空！");
            return
        }


        //新增企业库api
        $.post("/admin/audit/agency_info/save", {
            "_id": _id,
            "agency_name": agency_name,
            "history_name": history_name,
            "area_code": area_code,
            "type": type,
            "ranks": ranks,
            "province": province,
            "city": city,
            "district": district,
            "address": address,
            "wechat_accounts": wechat_accounts,
            "website": website,
            "contact":contact,

        }, function (data) {
            if (data && data.rep) {
                $("#info_data").modal("hide");
                alert("更新成功");
                setTimeout(function () {
                    ttable.search(agency_name).draw();
                    //    history.go(0) //刷新本页
                }, 800) //停1.5秒
            } else {
                alert("内部错误");
            }
        }, 'json')
    }
    function showJson(ptype, content, container) {
        var options = {
            mode: 'code',
            modes: ['code', 'tree'], // allowed modes
            onError: function (err) {
                alert(err.toString());
            }
        };
        if (ptype == "add") {
            editor_add = new JSONEditor(container, options, content);
        } else if (ptype == "update") {
            editor_update = new JSONEditor(container, options, content);
        }
    }



</script>

