<!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-repair-start" th:object="${amsRepairOrder}">
            <input name="repairId" th:field="*{repairId}" type="hidden">

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">维修方类型：</label>
                <div class="col-sm-8">
                    <select id="repairerType" name="repairerType" class="form-control" th:with="type=${@dict.getType('ams_repairer_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>

            <!-- 内修展示 -->
            <div class="form-group" id="div-internal-repairer">
                <label class="col-sm-3 control-label is-required">维修人：</label>
                <div class="col-sm-8">
                    <select id="repairUserId" name="repairUserId" class="form-control">
                        <option value="">-- 请选择系统用户 --</option>
                        <option th:each="user : ${userList}" th:text="${user.userName}" th:value="${user.userId}" th:data-name="${user.userName}" th:data-phone="${user.phonenumber}"></option>
                    </select>
                    <input id="manualRepairUserName" class="form-control" type="text" placeholder="或手工填写内部维修人姓名" style="margin-top: 8px;">
                    <input type="hidden" id="repairUserName" name="repairUserName">
                </div>
            </div>

            <!-- 外修展示 -->
            <div class="form-group" id="div-external-org" style="display: none;">
                <label class="col-sm-3 control-label is-required">外部维修单位：</label>
                <div class="col-sm-8">
                    <input id="repairOrgName" name="repairOrgName" class="form-control" type="text" placeholder="请输入外部维修公司名称">
                </div>
            </div>

            <div class="form-group" id="div-external-repairer" style="display: none;">
                <label class="col-sm-3 control-label">维修联系人：</label>
                <div class="col-sm-8">
                    <input id="externalRepairUserName" name="externalRepairUserName" class="form-control" type="text" placeholder="请输入维修人员姓名">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">联系电话：</label>
                <div class="col-sm-8">
                    <input id="repairContactPhone" name="repairContactPhone" class="form-control" type="text" placeholder="请输入联系电话">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "asset/repair";

        $("#form-repair-start").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var repairerType = $("#repairerType").val();
                if (repairerType === 'INTERNAL') {
                    var userId = $("#repairUserId").val();
                    var userName = userId ? $("#repairUserId").find("option:selected").data("name") : $("#manualRepairUserName").val();
                    if (!userName) {
                        $.modal.alertWarning("请选择系统用户或填写内部维修人姓名");
                        return;
                    }
                    $("#repairUserName").val(userName);
                } else if (repairerType === 'EXTERNAL') {
                    var orgName = $("#repairOrgName").val();
                    if (!orgName) {
                        $.modal.alertWarning("请输入外部维修单位");
                        return;
                    }
                    var extUserName = $("#externalRepairUserName").val();
                    $("#repairUserName").val(extUserName);
                }

                $.operate.save(prefix + "/start", $('#form-repair-start').serialize());
            }
        }

        $(function() {
            $("#repairerType").change(function() {
                var type = $(this).val();
                if (type === 'INTERNAL') {
                    $("#div-internal-repairer").show();
                    $("#div-external-org").hide();
                    $("#div-external-repairer").hide();

                    // 清空外修输入，并清空联系电话（切换时重置）
                    $("#repairOrgName").val("");
                    $("#externalRepairUserName").val("");
                    $("#repairContactPhone").val("");
                } else {
                    $("#div-internal-repairer").hide();
                    $("#div-external-org").show();
                    $("#div-external-repairer").show();

                    // 清空内修输入，并清空联系电话（切换时重置）
                    $("#repairUserId").val("");
                    $("#manualRepairUserName").val("");
                    $("#repairUserName").val("");
                    $("#repairContactPhone").val("");
                }
            });

            // 监听内部维修人选择，自动带出表 sys_user 中该用户的手机号码
            $("#repairUserId").change(function() {
                var phone = $(this).find("option:selected").data("phone");
                if (phone) {
                    $("#repairContactPhone").val(phone);
                } else {
                    $("#repairContactPhone").val("");
                }
            });

            // 初始化触发
            $("#repairerType").trigger("change");
            $("#repairUserId").trigger("change");
        });
    </script>
</body>
</html>
