{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}
<link href="/res/other/css/other.css" rel="stylesheet" type="text/css">
<style>
    #dataTable_filter div {
        padding: 0px 10px;
    }

    .operate a {
        padding: 5px 10px;
    }
</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">新增企业库信息</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>
                                <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("qiyeku_info")
    var ttable = {}
    $(function () {
        ttable = $('#dataTable').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": true,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "serverSide": true,
            "ajax": {
                "url": "/admin/audit/query_qyk/list",
                "type": "post",
            },
            "language": {
                "url": "/res/dist/js/dataTables.chinese.lang"
            },
            "columns": [
                {
                    "data": "company_name", "width": "18%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return val
                    }
                },
                {
                    "data": "alias", "width": "11%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return val
                    }
                },
                {
                    "data": "province", "width": "4%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return val
                    }
                },
                {
                    "data": "city", "width": "4%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return val
                    }
                },
                {
                    "data": "district", "width": "4%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return val
                    }
                },
                {
                    "data": "capital", "width": "6%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return val
                    }
                },
                {
                    "data": "company_address", "width": "17%", render: function (val, a, row) {
                        if (val == null) {
                            return ""
                        }
                        return val
                    }
                },
                {
                    "data": "_id", "width": "24%", 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>"
                    }
                }
            ]
        });
        /*新增企业库*/
        ttable.on('init.dt', function () {
            $(".opraaa").click(function () {
                var n = $(this).attr("opraaa");
                var _tit = "", htmlObj = {}, obj, tag = [];
                switch (n) {
                    case "newqyk":
                        tag = [
                            {label: "企业名称：", s_label: "s_company_name", must: true},
                            {label: "别名：", s_label: "s_alias"},
                            {label: "省份：", s_label: "s_province"},
                            {label: "城市：", s_label: "s_city"},
                            {label: "区县：", s_label: "s_district"},
                            {label: "注册资本：", s_label: "s_capital", placeholder: "示例：45678.98元；456.78万元；1234.25；"},
                            {label: "企业地址：", s_label: "s_company_address"},
                        ];
                        //新增企业库按钮
                        htmlObj = {
                            title: "新增企业库信息",
                            tag: tag,
                            bts: [
                                {
                                    label: "Save", class: "btn-danger",
                                    fun: function () {
                                        var company_name = $("#s_company_name").val();
                                        var alias = $("#s_alias").val();
                                        var province = $("#s_province").val();
                                        var city = $("#s_city").val();
                                        var district = $("#s_district").val();
                                        var capital = $("#s_capital").val();
                                        var company_address = $("#s_company_address").val();
                                        if (company_name === "") {
                                            alert("红色标签的表单不能为空！");
                                            return
                                        }
                                        //新增企业库api
                                        $.post("/admin/audit/qiyeku_info/save", {
                                            "company_name": company_name,
                                            "alias": alias,
                                            "province": province,
                                            "city": city,
                                            "district": district,
                                            "capital": capital,
                                            "company_address": company_address,
                                        }, function (data) {
                                            if (data && data.rep) {
                                                $("#myModal").modal("hide");
                                                alert("保存成功");
                                                setTimeout(function () {
                                                    ttable.search(company_name).draw();
                                                    //    history.go(0) //刷新本页
                                                }, 1500) //停1.5秒

                                            } else {
                                                alert("内部错误");
                                            }
                                        }, 'json')
                                    }
                                }
                            ]
                        };
                        OpenDialog(htmlObj, obj);
                        break;
                }
            });
        })
    });

    //详细行业类型
    function showHylxs(_id) {
        $("#infos").html("");
        $("#info_title").html("详细行业类型");
        $.post("/admin/audit/query_qyk/ById", {
            "_id": _id,
            "q_field": "industry",
        }, function (data) {
            if (data && data.rep) {
                // console.log(data);
                var tmp = "<div class='row'>";
                for (var i in data.data.industry) {
                    tmp = tmp +
                        "<div class=\"col-lg-10\">\n" +
                        "<div class=\"input-group\">\n" +
                        "   <input type='text' class='form-control' name='industry'  placeholder=\"请填写行业类型\" value='" + data.data.industry[i] + "'>" +
                        "   <span class=\"input-group-btn\">" +
                        '       <button class="btn btn-info " type="button" data-toggle="tooltip" title="删除" id="delCenterIpGrp"><span class="glyphicon glyphicon-minus"></span></button>' +
                        "   </span> " +
                        "  </div>" +
                        "</div>";
                    // console.log(i, data.data.industry[i]);
                }
                tmp = tmp +
                    "<div class=\"col-lg-10\">\n" +
                    "<button class='btn btn-info col-md-3' type='button'  id='addCenterIpGrpBtn' onclick='addCenterIpGrp(this)' >新增行业类型</button> " +
                    "<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='updateIndustrys(\"" + _id + "\")'>保存</button>" +
                    "</div>" +
                    "</div>";
                $("#infos").html(tmp);


            } else {
                alert("内部错误");
            }
        }, 'json');
        //展示详细行业类型
        $("#info_data").modal("show");
    }

    //添加行业详情
    function addCenterIpGrp(obj) {
        html = '<div class="input-group centerIp">' +
            '<input type="text" name="industry" class="form-control" id="ipInput" placeholder="请填写行业类型">' +
            '<span class="input-group-btn">' +
            '<button class="btn btn-info" type="button" data-toggle="tooltip" title="删除" id="delCenterIpGrp"><span class="glyphicon glyphicon-minus"></span></button>' +
            '</span>' +
            '</div>'
        obj.insertAdjacentHTML('beforebegin', html);
    }

    //更新行业详情
    function updateIndustrys(_id) {
        if (_id === "") {
            alert("参数id不存在");
            return
        }
        var params = $("#update-dataform").serialize();
        // console.log(_id, params);
        $.ajax({
            type: "POST",
            url: "/admin/audit/query_qyk/UpdateIndustrys",
            data: params + "&_id=" + _id,
            success: function (msg) {
                $("#info_data").modal("hide");
                if (msg.data) {
                    alert("success");
                } else {
                    alert("更新失败");
                }
            }
        });
    }

    $(document).on('click', '#delCenterIpGrp', function () {
        var el = this.parentNode.parentNode;
        if (confirm('您确定要删除选中的命令？')) {
            el.parentNode.removeChild(el);
        }
    });

    //详细联系方式
    function showTels(_id) {
        $("#infos").html("");
        $("#info_title").html("详细联系方式");
        $.post("/admin/audit/query_qyk/ById", {
            "_id": _id,
            "q_field": "contact",
        }, function (data) {
            if (data && data.rep) {
                // console.log(data);
                // var tmp = "<div class='row'>";
                var tmp = "";
                for (var i in data.data.contact) {
                    tmp += "<div class='row'>" +
                        "<div class=\"col-lg-10\">" +
                        '<h1><div class="input-group centerIp">' +
                        '<input type="text" name="contact_persons" class="form-control" id="ipInput" placeholder="请填写联系人" value=' + data.data.contact[i].contact_person + '>' +
                        '<select class="form-control" name="contact_types" value=' + data.data.contact[i].contact_type + '><option value ="法定代表人">法定代表人</option><option value ="项目联系人">项目联系人</option></select>' +
                        '<input type="text" name="phones" class="form-control" id="ipInput" placeholder="请填写联系电话" value=' + data.data.contact[i].phone + '>' +
                        '<select class="form-control" name="topscopeclasss" value=' + data.data.contact[i].topscopeclass + '><option value ="企业公示">企业公示</option><option value ="剑鱼标讯">剑鱼标讯</option></select>' +
                        '<span class="input-group-btn">' +
                        '<button class="btn btn-info" type="button" data-toggle="tooltip" title="删除" id="delCenterIpGrp"><span class="glyphicon glyphicon-minus"></span></button>' +
                        '</span>' +
                        '</div><h1>' +
                        '</div></div>';
                    // console.log(i, data.data.contact[i]);
                }
                tmp = tmp +
                    "<div class='row'>" +
                    "<div class=\"col-lg-10\">" +
                    "<button class='btn btn-info col-md-3' type='button'  id='addCenterIpGrpBtn' onclick='addCenterIpGrpTels(this)' >新增联系方式</button> " +
                    "<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='updateTels(\"" + _id + "\")'>保存</button>" +
                    "</div>" +
                    "</div>";
                $("#infos").html(tmp);
            } else {
                alert("内部错误");
            }
        }, 'json');
        $("#info_data").modal("show");
    }

    //添加联系方式
    function addCenterIpGrpTels(obj) {
        html = '<h1><div class="input-group centerIp">' +
            '<input type="text" name="contact_persons" class="form-control" id="ipInput" placeholder="请填写联系人">' +
            '<select class="form-control" name="contact_types"><option value ="法定代表人">法定代表人</option><option value ="项目联系人">项目联系人</option></select>' +
            '<input type="text" name="phones" class="form-control" id="ipInput" placeholder="请填写联系电话">' +
            '<select class="form-control" name="topscopeclasss"><option value ="企业公示">企业公示</option><option value ="剑鱼标讯">剑鱼标讯</option></select>' +
            '<span class="input-group-btn">' +
            '<button class="btn btn-info" type="button" data-toggle="tooltip" title="删除" id="delCenterIpGrp"><span class="glyphicon glyphicon-minus"></span></button>' +
            '</span>' +
            '</div><h1>'
        obj.insertAdjacentHTML('beforebegin', html);
    }

    //更新联系方式
    function updateTels(_id) {
        if (_id === "") {
            alert("参数id不存在");
            return
        }
        var params = $("#update-dataform").serialize();
        console.log(params)
        $.ajax({
            type: "POST",
            url: "/admin/audit/query_qyk/UpdateTels",
            data: params + "&_id=" + _id,
            // contentType: "application/json; charset=utf-8",
            dataType: "json",
            traditional: true,
            success: function (msg) {
                $("#info_data").modal("hide");
                if (msg.data) {
                    alert("success");
                } else {
                    alert("更新失败");
                }
            }
        });
    }

    //删除
    function deleteQyk(_id) {
        if (_id === "") {
            alert("参数id为空");
            return
        }
        if (confirm("您确认要提交此操作？")) {
            $.post("/admin/audit/qiyeku_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_company_name\" must=\"true\" value='" + _obj.company_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_alias\" value='" + _obj.alias + "'></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_capital\" value='" + _obj.capital + "'  placeholder=\"示例：45678.98元；456.78万元；1234.25；\"></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_company_address\" value='" + _obj.company_address + "'></div>" +
            "</div></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='updatedate(\"" + _obj._id + "\")'>更新</button></div>";
        $("#infos").html(com);
        $("#info_data").modal("show");
    }

    function updatedate(_id) {
        if (_id === "") {
            alert("id不存在！");
            return
        }
        var company_name = $("#s_company_name").val();
        var alias = $("#s_alias").val();
        var province = $("#s_province").val();
        var city = $("#s_city").val();
        var district = $("#s_district").val();
        var capital = $("#s_capital").val();
        var company_address = $("#s_company_address").val();
        if (company_name === "") {
            alert("红色标签的表单不能为空！");
            return
        }
        //新增企业库api
        $.post("/admin/audit/qiyeku_info/save", {
            "_id": _id,
            "company_name": company_name,
            "alias": alias,
            "province": province,
            "city": city,
            "district": district,
            "capital": capital,
            "company_address": company_address,
        }, function (data) {
            if (data && data.rep) {
                alert("更新成功");
                $("#info_data").modal("hide");
                setTimeout(function () {
                    ttable.search(company_name).draw();
                    //    history.go(0) //刷新本页
                }, 800) //停1.5秒
            } else {
                alert("内部错误");
            }
        }, 'json')
    }
</script>
