<!DOCTYPE html>
<!--[if IE 8]>
<html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]>
<html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html>
<!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
    <base href="#springUrl('/assets/adminlte/')"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>工作台</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="plugins/ionicons/css/ionicons.min.css">

    <!-- DataTables -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">

    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="plugins/iCheck/flat/blue.css">
    <!-- Morris chart -->
    <link rel="stylesheet" href="plugins/morris/morris.css">
    <!-- jvectormap -->
    <link rel="stylesheet" href="plugins/jvectormap/jquery-jvectormap-1.2.2.css">
    <!-- Date Picker -->
    <link rel="stylesheet" href="plugins/datepicker/datepicker3.css">
    <!-- Daterange picker -->
    <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker-bs3.css">
    <link rel="stylesheet"
          href="plugins\bootstrap-fileinput\fileinput.min.css">
    <!-- bootstrap wysihtml5 - text editor -->
    <link rel="stylesheet" href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>
<body class="hold-transition skin-blue sidebar-mini">


<div class="wrapper">
    <div class="content-wrapper" style="margin-left:0;">
        <div id="importExcelDiv"></div>

        <section class="content-header">
            <h1>
                收货地址管理
                <small>收货地址管理</small>
            </h1>
            <ol class="breadcrumb">
                <li><a><i class="fa fa-dashboard"></i>首页</a></li>
                <li><a class="active">收货地址管理管理列表</a></li>
            </ol>
        </section>


        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <form id="seachTableForm" action="#springUrl('/a/address/list')" method="get">
                                    <div class="col-xs-2">
                                        <input  type="text" class="form-control required"
                                               id="addressName" name="addressName" placeholder="患者姓名">
                                    </div>
                                    <div class="col-xs-2" >
                                        <select  id="province" name="provinceId" onchange="getCityList(this.value)" class="form-control required">
                                            <option value="">请选择省份</option>
                                        </select>
                                    </div>
                                     <div class="col-xs-2" >
                                        <select id="city" name="cityId" onchange="getReginList(this.value);" class="form-control required">
                                            <option value="">请选择城市</option>
                                        </select>
                                     </div>
                                    <div class="col-xs-2" >
                                        <select id="county" name="areaId" class="form-control required">
                                            <option value="">请选择区/县</option>
                                        </select>
                                    </div>
                                    <div class="col-xs-2">
                                        #if($shiro.hasPermission("qy:address:view"))
                                        <a href="javascript:void(0)" class="btn btn-primary search">搜索</a>
                                        #end
                                    </div>

                                    <!--                                                                        #if($shiro.hasPermission("qy:address:edit"))-->
                                    <!--                                    <a href="#springUrl('/a/address/form')" class="btn btn-primary">新增</a>-->
                                    <!--                                                                            <a href="#springUrl('/a/address/exportExcel')" class="btn btn-primary">导出</a>-->
                                    <!--                                        <a onclick="importExcel();" class="btn btn-primary">导入</a>-->
                                    <!--                                                                        #end-->
                            </form>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                            <table id="table" class="table table-bordered table-striped">
                                <thead>
                                <tr>
                                    <td hidden="true">Id
                                    </td>
                                    <th>openId</th>
                                    <th>患者姓名</th>
                                    <th>性别</th>
                                    <th>联系方式</th>
                                    <th>省份</th>
                                    <th>城市</th>
                                    <th>区/县</th>
                                    <th>详细地址</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="tablebody">
                                </tbody>

                            </table>
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->


    <!-- Add the sidebar's background. This div must be placed
         immediately after the control sidebar -->
    <div class="control-sidebar-bg"></div>
</div><!-- ./wrapper -->
<!-- jQuery 2.1.4 -->
<script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
<!-- Bootstrap 3.3.5 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- DataTables -->
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables/extensions/i18n/lanauage_ch.js"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<script src="plugins/datepicker/bootstrap-datepicker.js"></script>
<script src="plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.min.js"></script>
<!--fileinput js-->
<script src="plugins\bootstrap-fileinput\fileinput.js"></script>
<script src="plugins/bootstrap-fileinput/zh.js"></script>
<script src="plugins/bootstrap-fileinput/zh2.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<script type="text/javascript"
        src="plugins/jquery-validation/js/jquery.validate.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="common/js/cfapp.js"></script>
<script>

    var csrfheader = {name:'_csrf_header',value:'${_csrf.headerName}'};
    var csrftoken  = {name:'_csrf',value:'${_csrf.token}'};
    let areaList = null
    let listProvince = null
    let listCity = null
    let listRegin = null


    function formatDates(now) {
        var now = new Date(now);
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        return year + "-" + month + "-" + date + "   " + hour + ":"
            + minute + ":" + second;
    }


    function seachTable() {
        var sSource = "#springUrl('/a/address/listData')";
        var aoData = {
            iDisplayStart: 1,
            iDosplayLength: 10
        }
        var retrieveData = function (sSource, aoData, fnCallback) {

            $("#seachTableForm input,select").each(function () {
                var params = {
                    name: $(this).attr("name"),
                    value: $(this).val()
                };
                aoData.push(params);
            })

            $.ajax({
                "type": "GET",
                "url": sSource,
                "dataType": "json",
                "data": aoData, //以json格式传递
                "success": fnCallback
            });
        };

        $('#table').DataTable({
            "lengthChange": false,
            "searching": false,
            "ordering": false,
            "bFiltered": false,
            "bStateSave": true, // save datatable state(pagination, sort, etc) in cookie.
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": sSource,
            "fnServerData": retrieveData,
            "pagingType": "full_numbers",
            "aoColumns": [
                {
                    "mData": "id"
                },
                {
                    "mData": "openId"
                },
                {
                    "mData": "addressName"
                },
                {
                    "mData": "sex"
                },
                {
                    "mData": "phone"
                },
                {
                    "mData": "provinceName"
                },
                {
                    "mData": "cityName"
                },
                {
                    "mData": "countyName"
                },
                {
                    "mData": "address"
                }
                ,
                {
                    "mData": "id"
                }],
            "aoColumnDefs": [
                { // set default column settings
                    'visible': false,
                    'targets': [0]
                },
                {
                    "aTargets": [1],
                    "mData": "openId",
                    "mRender": function (a, b, c, d) {
                        return a;
                    }
                },
                {
                    "aTargets": [2],
                    "mData": "addressName",
                    "mRender": function (a, b, c, d) {
                        return c.addressName.slice(0, 1) + '*' + c.addressName.slice(2, 3);
                    }
                },
                {
                    "aTargets": [3],
                    "mData": "sex",
                    "mRender": function (a, b, c, d) {
                        if (a == '0') {
                            return "女";
                        } else {
                            return "男";
                        }
                    }
                },
                {
                    "aTargets": [4],
                    "mData": "phone",
                    "mRender": function (a, b, c, d) {
                        return c.phone.slice(0, 1) + '*' + c.phone.slice(2, 3);
                    }
                },
                {
                    "aTargets": [5],
                    "mData": "provinceName",
                    "mRender": function (a, b, c, d) {
                        return a;
                    }
                },
                {
                    "aTargets": [6],
                    "mData": "cityName",
                    "mRender": function (a, b, c, d) {
                        return a;
                    }
                },
                {
                    "aTargets": [7],
                    "mData": "countyName",
                    "mRender": function (a, b, c, d) {
                        return a;
                    }
                },
                {
                    "aTargets": [8],
                    "mData": "address",
                    "mRender": function (a, b, c, d) {
                        return a;
                    }
                },
                {
                    "aTargets": [9],
                    "mData": "id",
                    "mRender": function (a, b, c, d) {
                        var html = '#if($shiro.hasPermission("qy:wxQrcode:edit"))';
                        html += '<div class="btn-group">\n' +
                            '<button type="button" class="btn btn-success btn-flat">操作</button>\n' +
                            '<button type="button" class="btn btn-success btn-flat dropdown-toggle" data-toggle="dropdown">\n' +
                            '  <span class="caret"></span>\n' +
                            '  <span class="sr-only">Toggle Dropdown</span>\n' +
                            '</button>\n' +
                            '<ul class="dropdown-menu" role="menu">\n';
                        html += '<li><a href="#springUrl("/a/address/form?id=' + a + '")">查看</a></li>';
                        html += '<li><a href="javascript:removeData(' + a + ')">删除</a></li>';
                        html += '</ul>';
                        html += '#end';
                        return html;
                    }
                }


            ]
        });
    }

    // return '<a href="#springUrl("/a/address/form?id=' + c.id + '")" data-id="' + c.id + '" data-action="view">' + formatDates(a, "yyyy-MM-dd HH:mm:ss");
    // +'</a>';
    jQuery(document).ready(function () {
        var url = "#springUrl('/a/address/listArea')"
        $.ajax({
            url: url,
            type: "GET",
            success: function (data) {
                console.log(data)
                areaList = data
                listProvince = data.filter(x => x.cityType == 1)
                var province = document.querySelector('#province') //querySelector获取id="province"的元素
                for (index in listProvince) {
                    var op = new Option(listProvince[index].areaName, listProvince[index].areaId, false, false)
                    //将循环出来的元素追加到province下拉框里
                    province.options[province.length] = op
                }


            }
        });



        seachTable();
        $('.datepicker').datepicker({
            show: true,
            format: 'yyyy-mm-dd',
            autoclose: true,
            language: 'zh-CN',
            todayBtn: 'linked',
            clearBtn: 'linked'

        });
        $('.search').click(function () {
            $("#table").dataTable().fnClearTable();
        });
    });

    Cfapp.init();

    function getCityList(areaId) {
        listCity = areaList.filter(x => x.affiliationareaId == areaId && x.cityType == 2)
        $("#city").empty();
        $("#county").empty();
        var city = document.querySelector('#city')
        for (index in listCity) {
            var op = new Option(listCity[index].areaName, listCity[index].areaId, false, false)
            //将循环出来的元素追加到province下拉框里
            city.options[city.length] = op
        }
        if (listCity.length==1){
            getReginList(listCity[0].areaId);
        }
        getReginList($("#city").val())
        var province = $("#province option:selected").val()
        if (province==""){
            $("#city").append(`<option value = "">请选择城市</option>`);
            $("#county").append(`<option value = "">请选择区/县</option>`);

        }
    }

    function getReginList(areaId) {
        listRegin = areaList.filter(x => x.affiliationareaId == areaId && x.cityType == 3)
        $("#county").empty();
        var county = document.querySelector('#county')
        for (index in listRegin) {
            var op = new Option(listRegin[index].areaName, listRegin[index].areaId, false, false)
            //将循环出来的元素追加到province下拉框里，第一次训话length=1，第二次循环length=2
            county.options[county.length] = op
        }
    }

    function removeData(data) {
        Cfapp.confirm({
            message: "确定要删除吗",
            btnoktext: "确定",
            btncanceltext: "取消",
            success: function () {
                $.ajax({
                    type: "POST",
                    url: "#springUrl('/a/address/delete')",
                    data: {id: data,"_csrf_header":csrfheader.value,"_csrf":csrftoken.value},
                    dataType: "json",
                    success: function (data) {
                        if (data.errorNo == 0) {
                            Cfapp.alert({
                                message: "删除成功",
                                btntext: "确定",
                                success: function () {
                                    location.href = "#springUrl('/a/address/list')";
                                }
                            });
                        }
                    },
                    error: function () {
                    }
                })
            },
            cancel: function () {
                $(".modal-backdrop").fadeOut();
            }
        });
    }

    // function importExcel() {
    //     var templateExcelUrl = "#springUrl('/a/address/templateExcel')";
    //     var importExcelUrl = "#springUrl('/a/address/importExcel')";
    //     Cfapp.importExcel({
    //         title: '收货地址管理导入',
    //         importurl: importExcelUrl,
    //         templateurl: templateExcelUrl,
    //         cancel: function () {
    //
    //         },
    //         success: function () {
    //
    //         }
    //     });
    // }


</script>

</body>
</html>
