<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增客户信息')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-info-add" th:object="${ccCustInfo}">
        <input name="id" th:field="*{id}" type="hidden">
        <input name="id" th:field="*{phoneNum}" type="hidden">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label" th:text="#{custinfo.form.custName}"></label>
                <div class="col-sm-8">
                    <input name="custName" th:field="*{custName}" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label" th:text="#{custinfo.form.province}"></label>
                <div class="col-sm-8">
                    <select name="provinceCode" class="form-control" id="provinceCode" th:field="*{provinceCode}">
                        <option value="" th:text="#{custinfo.form.province.empty}"></option>
                        <!-- 省份数据 -->
                        <option th:each="province : ${provinces}" th:value="${province.extId}" th:text="${province.extName}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label" th:text="#{custinfo.form.city}"></label>
                <div class="col-sm-8">
                    <select name="cityCode" class="form-control" id="cityCode" th:field="*{cityCode}">
                        <option value="" th:text="#{custinfo.form.city.empty}"></option>
                        <!-- 城市数据 -->
                        <!-- 城市数据会根据省份的选择动态加载 -->
                        <option th:each="city : ${citys}" th:value="${city.extId}" th:text="${city.extName}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label" th:text="#{custinfo.form.county}"></label>
                <div class="col-sm-8">
                    <select name="countyCode" class="form-control" id="countyCode" th:field="*{countyCode}">
                        <option value="" th:text="#{custinfo.form.county.empty}"></option>
                        <!-- 区/县数据 -->
                        <!-- 区/县数据会根据城市的选择动态加载 -->
                        <option th:each="county : ${countys}" th:value="${county.extId}" th:text="${county.extName}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label" th:text="#{custinfo.form.address}"></label>
                <div class="col-sm-8">
                    <input name="address" th:field="*{address}" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label" th:text="#{custinfo.form.gender}"></label>
                <div class="col-sm-8">
                    <div class="input-group" style="width: 100%">
                        <select name="gender" class="form-control m-b" th:with="type=${@dict.getType('sys_user_sex')}">
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{gender}"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label" th:text="#{custinfo.form.callnotes}"></label>
                <div class="col-sm-8">
                    <textarea name="notes" id="notes" class="form-control" type="text" ></textarea>
                </div>
            </div>
        </div>

        <!-- 沟通记录显示区域 -->
        <hr> <!-- 在每条记录之间添加分隔线 -->
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label" th:text="#{custinfo.form.hisrecords}"></label>
                <div class="col-sm-8">
                    <div th:each="callRecord : ${ccCustInfo.callRecordList}" class="record">
                        <div class="row">
                            <div class="col-sm-12">
                                <strong th:text="#{custinfo.form.callTime}"></strong>
                                <span th:text="${#dates.format(callRecord.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12">
                                <strong th:text="#{custinfo.form.notes}"></strong>
                                <span th:text="${callRecord.notes}"></span>
                            </div>
                        </div>
                        <hr> <!-- 在每条记录之间添加分隔线 -->
                    </div>
                </div>
            </div>
        </div>
        <!-- 保存按钮 -->
        <div class="col-xs-12">
            <div class="form-group">
                <div class="col-sm-8 col-sm-offset-3">
                    <button type="button" class="btn btn-primary"
                            onclick="submitHandler()">
                        <i class="fa fa-save"></i> 保存
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">

    var _callType = [[${callType}]];
    var _uuid = [[${uuid}]];
    var prefix = ctx + "cc/custcallrecord"
    $("#form-info-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        var formData = $('#form-info-add').serializeArray();
        // 添加额外的数据
        var callRecord = {
            notes: $("#notes").val(),
            callType: _callType,
            uuid: _uuid
        };
        formData.push({"name": "callRecord", "value": JSON.stringify(callRecord)})
        var province = $('#provinceCode option:selected').text(); // 获取选中项的text值
        var city = $('#cityCode option:selected').text(); // 获取选中项的text值
        var county = $('#countyCode option:selected').text(); // 获取选中项的text值
        formData.push({"name": "province", "value": province})
        formData.push({"name": "city", "value": city})
        formData.push({"name": "county", "value": county})
        if ($.validate.form()) {
            var config = {
                url: prefix + "/add",
                type: "post",
                dataType: "json",
                data: formData,
                beforeSend: function () {
                    $.modal.loading(i18n("common.tip.loading"));
                    $.modal.disable();
                },
                success: function(result) {
                    $.modal.closeLoading();
                    $.modal.enable();
                }
            };
            $.ajax(config)
        }
    }


    // 省份下拉框改变时，加载对应的城市数据
    $('#provinceCode').change(function() {
        var provinceCode = $(this).val();
        var citySelect = $('#cityCode');
        var countySelect = $('#countyCode');

        // 清空城市和区/县下拉框
        citySelect.html('<option value="">请选择城市</option>');
        countySelect.html('<option value="">请选择区/县</option>');

        // 发送请求获取对应的城市数据
        if (provinceCode) {
            $.ajax({
                url: ctx + "cc/divisiondata/getCitysByProvinceCode",
                type: 'GET',
                data: {provinceCode: provinceCode},
                success: function(response) {
                    $.each(response.data, function(index, city) {
                        citySelect.append($('<option>', {
                            value: city.extId,
                            text: city.extName
                        }));
                    });
                }
            });
        }
    });

    // 城市下拉框改变时，加载对应的区/县数据
    $('#cityCode').change(function() {
        var cityCode = $(this).val();
        var countySelect = $('#countyCode');

        // 清空区/县下拉框
        countySelect.html('<option value="">请选择区/县</option>');

        // 发送请求获取对应的区/县数据
        if (cityCode) {
            $.ajax({
                url: ctx + "cc/divisiondata/getCountysByCityCode",
                type: 'GET',
                data: {cityCode: cityCode},
                success: function(response) {
                    $.each(response.data, function(index, county) {
                        countySelect.append($('<option>', {
                            value: county.extId,
                            text: county.extName
                        }));
                    });
                }
            });
        }
    });
</script>
</body>
</html>