<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>全部工单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="decorator" content="base" />
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/static/css/reset.css" />
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/lib/font/iconfont.css" />
</head>
<body>

<div class="layui-fluid">
    <div class="layui-form layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="SIT_GDGL_QBGD_QBGD_T" html='<a href="${ctx}/service/order/getFormOrder"><li class="layui-this">全部工单</li></a>'/>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset"  onclick="reset()" >
                    <i class="layui-icon fs-12">&#xe9aa;</i>
                    重置
                </button>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10" id="searchBtn" lay-submit lay-filter="LAY-user-back-search">
                    <i class="layui-icon fs-12">&#xe615;</i>
                    查询
                </button>
            </ul>
        </div>
        <div class="layui-card-body layuiadmin-card-header-auto" id="searchForm">
            <div class="layui-form-item">
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">工单编号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="number" class="layui-input" >
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">服务工程师：</label>
                    <div class="layui-input-block" >
                        <select name="employe_id" lay-search>
                            <option value="">请选择</option>
                            <c:forEach var="emp" items="${empList}">
                                <option value="${emp.columns.id}">${emp.columns.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">工单状态：</label>
                    <div class="layui-input-block" >
                        <select name="siteStatus" lay-filter="aihao">
                            <option value="">请选择</option>
                            <option value="1" >待接收</option>
                            <option value="3" >待派工</option>
                            <option value="4" >服务中</option>
                            <option value="7" >已完工</option>
                            <option value="8" >已完成</option>
                            <option value="9" >无效工单</option>
                        </select>
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">电站名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="power_station_name" class="layui-input" >
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">服务类型：</label>
                    <div class="layui-input-block" >
                        <select name="service_type" lay-filter="aihao">
                            <option value="">请选择</option>
                            <option value="安装" >安装</option>
                            <option value="维修" >维修</option>
                            <option value="咨询" >咨询</option>
                        </select>
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">设备类型：</label>
                    <div class="layui-input-block" >
                        <select name="appliance_category" lay-filter="aihao">
                            <option value="">请选择</option>
                            <option value="光伏组件">光伏组件</option>
                            <option value="逆变器">逆变器</option>
                            <option value="通讯模块">通讯模块</option>
                            <option value="并网箱">并网箱</option>
                            <option value="线缆">线缆</option>
                            <option value="配件">配件</option>
                        </select>
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">服务措施：</label>
                    <div class="layui-input-block">
                        <select name="service_measures" lay-search>
                            <option value="">请选择</option>
                            <c:forEach var="measures" items="${measures}">
                                <option value="${measures.columns.service_measures}">${measures.columns.service_measures}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">设备型号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="appliance_model" class="layui-input" >
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">用户姓名：</label>
                    <div class="layui-input-block">
                        <input type="text" name="customer_name" class="layui-input" >
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">用户电话：</label>
                    <div class="layui-input-block">
                        <input type="text" name="customer_mobile" class="layui-input" >
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">用户地址：</label>
                    <div class="layui-input-block">
                        <input type="text" name="customer_address" class="layui-input" >
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">设备序列号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="appliance_machine_code" class="layui-input" >
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm5">
                    <label class="layui-form-label">保修类型：</label>
                    <div class="layui-input-block" >
                        <select name="warranty_type" lay-filter="aihao">
                            <option value="">请选择</option>
                            <option value="1">保内</option>
                            <option value="2">保外</option>
                        </select>
                    </div>
                </div>


                <div class="layui-col-lg4 layui-col-sm6">
                    <label class="layui-form-label">报单时间：</label>
                    <div class="layui-input-block">
                        <div class="layui-col-xs5">
                            <input type="text" name="repairTimeSta" class="layui-input" id="repairTime" placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs1 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="repairTimeEnd" class="layui-input" id="endTime" placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-col-lg4 layui-col-sm6">
                    <label class="layui-form-label">派工时间：</label>
                    <div class="layui-input-block">
                        <div class="layui-col-xs5">
                            <input type="text" name="site_dispatch_timeMin" class="layui-input" id="disStartTime" placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs1 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="site_dispatch_timeMax" class="layui-input" id="disEndTime" placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-col-lg4 layui-col-sm6">
                    <label class="layui-form-label">预约时间：</label>
                    <div class="layui-input-block">
                        <div class="layui-col-xs5">
                            <input type="text" name="promiseStratTime" class="layui-input" id="promiseStratTime" placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs1 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="promiseEndTime" class="layui-input" id="promiseEndTime" placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-col-lg4 layui-col-sm6">
                    <label class="layui-form-label">完工时间：</label>
                    <div class="layui-input-block">
                        <div class="layui-col-xs5">
                            <input type="text" name="endTimeSta" class="layui-input" id="endStartTime" placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs1 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="endTimeEnd" class="layui-input" id="endEndTime" placeholder="结束日期" >
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <sfTags:pagePermission authFlag="SIT_GDGL_QBGD_JD_B" html='<button class="layui-btn layui-btn-sm" lay-event="receive"><i class="layui-icon layui-icon-export"></i>接单</button>'/>
                    <sfTags:pagePermission authFlag="SIT_GDGL_QBGD_JVD_B" html='<button class="layui-btn layui-btn-sm" lay-event="refuseOrder"><i class="layui-icon layui-icon-export"></i>拒单</button>'/>
                  <sfTags:pagePermission authFlag="SIT_GDGL_QBGD_PG_B" html='<button class="layui-btn layui-btn-sm" lay-event="disOrder"><i class="layui-icon layui-icon-export"></i>派工</button>'/>
                    <sfTags:pagePermission authFlag="SIT_GDGL_QBGD_ZP_B" html='<button class="layui-btn layui-btn-sm" lay-event="turnDispatch"><i class="layui-icon layui-icon-export"></i>转派</button>'/>
                   <sfTags:pagePermission authFlag="SIT_GDGL_QBGD_WX_B" html='<button class="layui-btn layui-btn-sm" lay-event="invalid"><i class="layui-icon layui-icon-export"></i>无效 </button>'/>
                    <sfTags:pagePermission authFlag="SIT_GDGL_QBGD_DC_B" html='<button class="layui-btn layui-btn-sm" lay-event="export" id="exportBtn"><i class="layui-icon layui-icon-export"></i>导出</button>'/>
                </div>
            </script>
        </div>
    </div>
</div>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer','laypage','laydate','form'], function(){
        $ = layui.$;
        tableExt = layui.tableExt;
        layer = layui.layer;
        laypage = layui.laypage;
        laydate = layui.laydate;
        form = layui.form;
        table = tableExt.getLayTable();

        //开始日期
        var insStart = laydate.render({
            elem: '#repairTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新结束日期的最小日期
                insEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                insEnd.config.elem[0].focus();
            }
        });
        //结束日期
        var insEnd = laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新开始日期的最大日期
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });
        //开始日期
        var disStartTime = laydate.render({
            elem: '#disStartTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新结束日期的最小日期
                disEndTime.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                disEndTime.config.elem[0].focus();
            }
        });
        //结束日期
        var disEndTime = laydate.render({
            elem: '#disEndTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新开始日期的最大日期
                disStartTime.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        //开始日期
        var endStartTime = laydate.render({
            elem: '#endStartTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新结束日期的最小日期
                endEndTime.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                endEndTime.config.elem[0].focus();
            }
        });
        //结束日期
        var endEndTime = laydate.render({
            elem: '#endEndTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新开始日期的最大日期
                endStartTime.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        //开始日期
        var promiseStratTime = laydate.render({
            elem: '#promiseStratTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新结束日期的最小日期
                promiseEndTime.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                promiseEndTime.config.elem[0].focus();
            }
        });
        //结束日期
        var promiseEndTime = laydate.render({
            elem: '#promiseEndTime'
            ,type: 'datetime'
            ,done: function(value, date){
                //更新开始日期的最大日期
                promiseStratTime.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        tableExt.render({
            elem: '#test'
            , height: 'full-240'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            , url: '${ctx}/service/order/getServiceFormList'
            , cols: [[
                {type: 'numbers',fixed:'left',title:'序号'}
                , {field: 'id', type: 'checkbox',fixed: 'left'}
                , {field: 'number', title: '工单编号', minWidth:'200',sort: false, align: 'center',fixed: true,exportable:true,templet:function(d){
                       var flag = d.flag;
                        var html;
                        if(isBlank(flag)){//
                            html ="<i class=\"icon iconfont\" style=\"color: #8D8D8D\" onclick=\"orderFlag('"+d.id+"')\">&#xe6b0;</i>";
                        }else{
                            html ="<i class=\"icon iconfont \" style=\"color: "+d.flag_type+";\" onclick=\"orderFlag('"+d.id+"')\">&#xe6b0;</i>";
                        }
                        return html + "&nbsp;&nbsp;<a href=\"#\" class=\"layui-table-link\" onclick=\"orderDetail('"+d.id+"')\">"+d.number+"</a>";
                    }}
                , {field: 'status', title: '工单状态', sort: false, align: 'center',width:150,exportable:true,templet:function(d){
                        var status = d.status;
                        var site_status=d.site_status;
                        var callback_result = d.callback_result;
                           if(site_status==9){
                              return "无效工单";
                           }else if(callback_result ==2){
                                return "<span data-pass = \"passed\">服务中</span>";
                            }else if(site_status==1){
                                return "待接收";
                            }else if(site_status==3 ||site_status==8){
                                return "待派工";
                            }else if(site_status==4 ||site_status==5 ||site_status==6 ){
                                return "服务中";
                            } else if(site_status==7) {
                               if (status == 5) {
                                   return "已完成";
                               }
                                return "已完工";
                            }

                    },expFormat:function (d) {
                        var status = d.status;
                        var site_status = d.site_status;
                        var callback_result = d.callback_result;
                        if (site_status==9) {
                            return "无效工单";
                        }else if (callback_result == 2) {
                            return "服务中";
                        } else if (site_status == 1) {
                            return "待接收";
                        } else if (site_status == 3 || site_status == 8) {
                            return "待派工";
                        } else if (site_status == 4 || site_status == 5 || site_status == 6) {
                            return "服务中";
                        } else if (site_status == 7) {
                            if (status == 5) {
                                return "已完成";
                            }
                            return "已完工";
                        }
                    }
                    }
                , {field: 'employe_name', title: '服务工程师', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'power_station_name', title: '电站名称', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'customer_name', title: '用户姓名', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'customer_mobile', title: '用户电话', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'customer_address', title: '用户地址',minWidth:'200', sort: false, align: 'center',exportable:true,templet:function(d){
                        return d.province+d.city+d.area+d.customer_address;
                    }
                    , expFormat: function (d) {
                        return d.province+d.city+d.area+d.customer_address;
                    }
                }
                , {field: 'promise_time', title: '预约时间',minWidth:'100', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'promise_limit', title: '时间要求',minWidth:'100', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'service_type', title: '服务类型', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'service_measures', title: '服务措施', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'appliance_capacity', title: '容量(KW)', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'warranty_type', title: '保修类型', sort: false, width:150,align: 'center',exportable:true,templet:function(d){
                        if(d.warranty_type == 1){
                            return "保内";
                        }else if(d.warranty_type == 2){
                            return "保外";
                        }else {
                            return "";
                        }               }
                    ,expFormat:function (d) {
                        if(d.warranty_type == 1){
                            return "保内";
                        }else if(d.warranty_type == 2){
                            return "保外";
                        }else{
                            return "";
                        }
                    }}
                , {field: 'appliance_model', title: '设备型号', minWidth:'150',sort: false, align: 'center',exportable:true}
                , {field: 'appliance_category', title: '设备类型', minWidth:'150',sort: false, align: 'center',exportable:true}
                , {field: 'appliance_machine_code', title: '设备序列号', minWidth:'150',sort: false, align: 'center',exportable:true}
                , {field: 'remarks', title: '工单描述', minWidth:'150',sort: false, align: 'center',exportable:true}
                , {
                    field: 'order_type',
                    title: '信息来源',
                    minWidth: '150',
                    sort: false,
                    align: 'center',
                    exportable: true,
                    templet: function (d) {
                        if (d.order_type == 1) {
                            return "用户在线报修";
                        } else if (d.order_type == 2) {
                            return "平台录入";
                        }else if (d.order_type == 4) {
                            return "电子商务";
                        }  else {
                            return "其他";
                        }
                    }
                    ,
                    expFormat: function (d) {
                        if (d.order_type == 1) {
                            return "用户在线报修";
                        } else if (d.order_type == 2) {
                            return "平台录入";
                        } else if (d.order_type == 4) {
                            return "电子商务";
                        } else {
                            return "其他";
                        }
                    }
                }
                , {field: 'appliance_buy_time', title: '投运时间',minWidth:'100', sort: false, align: 'center',exportable:true}
                , {field: 'createName', title: '报单人', minWidth:'150',sort: false, align: 'center',exportable:true}
                , {field: 'repair_time', title: '报单时间',minWidth:'100', sort: false, align: 'center',exportable:true}
                , {field: 'site_dispatch_time', title: '派工时间',minWidth:'100', sort: false, align: 'center',exportable:true}
                , {field: 'site_process_time', title: '接单时间',minWidth:'100', sort: false, align: 'center',exportable:true}
                , {field: 'drop_in_time', title: '上门时间',minWidth:'100', sort: false, align: 'center',exportable:true}
                , {field: 'end_time', title: '完工时间',minWidth:'100', sort: false, align: 'center',exportable:true}
                , {field: 'level', title: '重要程度', sort: false, align: 'center',width:150,exportable:true,templet:function(d){
                        if(d.level == 1){
                            return "紧急";
                        }else if(d.level == 2){
                            return "一般";
                        }else {
                            return "";
                        }             },expFormat:function (d) {
                        if(d.level == 1){
                            return "紧急";
                        }else if(d.level == 2){
                            return "一般";
                        }else {
                            return "";
                        }
                    }}
                , {field: 'please_refer_mall', title: '销售商', sort: false, align: 'center',width:150,exportable:true}
                , {field: 'invoice', title: '发票号码', sort: false, align: 'center',width:150,exportable:true}
            ]]
            ,done:function(res,cuss,count){
                $('span[data-pass="passed"]').parent().parent().parent().css('background-color','#ffeaf2');//:

            },exportOpt: {                           //导出选项配置
                ele: '#exportBtn',                   //导出的Dom元素
                headerTitle: '工单管理-全部工单',             //excel表格的标题
                pageData: false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当pageData为false生效
                fileName: '工单管理-全部工单',
                where: function () {
                    //当pageData为false时生效，过滤的条件
                    return _serializeJson($("searchForm"));
                }
            }
        });


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            switch(obj.event){
                case 'export':
                    break;
                case 'receive':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg("请至少选择一条数据",{icon:5,anim:6});
                        return;
                    }

                    var orderId="";
                    for(var i=0;i<data.length;i++){
                      if(data[i].site_status != 1){
                            layer.msg("您选择的工单中有不可操作接单的工单，请重新选择！",{icon:5,anim:6});
                            return;
                        }
                        orderId+=data[i].id+",";
                    }
                    console.log(orderId);
                    var indexla = layer.confirm('您确定要接收工单吗？', {
                        icon: 3,
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        $.ajax({
                            type: "POST",
                            url: "${ctx}/service/order/receiveOrder",
                            data: {
                                orderId: orderId
                            },
                            success: function (result) {
                                if (result.code == "200") {
                                    layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                                        window.location.reload();
                                    });
                                } else if (result.code == "500") {
                                    layer.msg('您选择的工单中有不可接收的工单，不能操作接单！', {icon: 2})
                                }else if('422' === data.code){
                                    layer.msg("工单已被转派！")
                                }
                                else{
                                    layer.msg('操作失败！', {icon: 2})
                                }
                            }
                        });

                    }, function () {
                        layer.close(indexla)
                    });
                    break;
                case 'disOrder':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg("请至少选择一条数据",{icon:5,anim:6});
                        return;
                    }
                    var orderId="";
                    for(var i=0;i<data.length;i++){
                        if(data[i].site_status != 3){
                            layer.msg("您选择的工单中有不可操作派工的工单，请重新选择！",{icon:5,anim:6});
                            return;
                        }
                        orderId+=data[i].id+",";
                    }
                    layer.open({
                        type: 2,
                        content: "${ctx}/service/order/dispatchOrderForm?orderId="+orderId/*+"&applianceCategory="+applianceCategory*/ //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                        , title: "派工"
                        , btn: ['确认派工']
                        , area: ["700px", "420px"]
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'sureSubmit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var obj=layero.find('iframe').contents().find("#zhijiepaidan tr").find("input[name='emplyid']:checked");
                                var empIds= '';
                                obj.each(function () {
                                    if (isBlank(empIds)) {
                                        empIds = $(this).val();
                                    } else {
                                        empIds = $(this).val() + ',' + empIds;
                                    }
                                });

                                var empName = layero.find('iframe').contents().find("#paigongzhi").html();
                                var empMobile = layero.find('iframe').contents().find("#mobiles").val();
                                if (isBlank(empIds)) {
                                    layer.msg('请选择工程师', {icon: 2});
                                    return false;
                                }
                                $.ajax({
                                    type: "POST",
                                    url: "${ctx}/service/order/dispatchForm",
                                    data: {
                                        orderId: orderId,
                                        empId:empIds,
                                        empName:empName,
                                        employeMobile:empMobile
                                    },
                                    success: function (result) {
                                        if (result.code == 200) {
                                            layer.closeAll()
                                            layer.msg('派工成功！', {icon: 1, time: 1000}, function () {
                                                window.location.reload();
                                            });
                                        } else if (result.code == "508") {
                                            layer.msg('该工单不可操作派工！', {icon: 2});
                                        } else {
                                            layer.msg('派工失败', {icon: 2, time: 1000});
                                        }
                                    }
                                });

                            });
                            submit.trigger('click');
                            return false;//开启该代码可禁止点击该按钮关闭
                        }
                        ,
                        success: function (layero) {
                            layero.find('.layui-layer-btn').css('text-align', 'center');
                            layero.find('.layui-layer-btn').find(".layui-layer-btn0").css("background-color", "#009688");
                        }
                    });




                    break;
                case 'refuseOrder':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg("请至少选择一条数据",{icon:5,anim:6});
                        return;
                    }
                    var orderId="";
                    for(var i=0;i<data.length;i++){
                        if(data[i].site_status != 1){
                            layer.msg("您选择的工单中有不可操作接单的工单，请重新选择！",{icon:5,anim:6});
                            return;
                        }
                        orderId+=data[i].id+",";
                    }
                    var indexla = layer.confirm('您确定要拒接工单吗？', {
                        icon: 3,
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        $.ajax({
                            type: "POST",
                            url: "${ctx}/service/order/refuseOrder",
                            data: {
                                orderId: orderId
                            },
                            success: function (result) {
                                if (result.code == "200") {
                                    layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                                        window.location.reload();
                                    });
                                } else if (result.code == "500") {
                                    layer.msg('您选择的工单中有不可退回的工单，不能操作拒单！', {icon: 2})
                                }else if('422' === data.code){
                                    layer.msg("工单已被转派！")
                                } else if('423' === data.code) {
                                    layer.msg("自建工单不可退回！");
                                }
                                else{
                                    layer.msg('操作失败！', {icon: 2})
                                }
                            }
                        });

                    }, function () {
                        layer.close(indexla)
                    });
                    break;
                case 'turnDispatch':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg("请至少选择一条数据",{icon:5,anim:6});
                        return;
                    }

                    var orderId="";
                    var employeName = "";
                    for(var i=0;i<data.length;i++){
                        if(data[i].site_status == '1' ||data[i].site_status == '3'||data[i].site_status == '7'){
                            layer.msg("您选择的工单中有不可操作转派的工单，请重新选择！",{icon:5,anim:6});
                            return;
                        }
                        orderId+=data[i].id+",";
                        employeName = data[i].employe_name;
                    }
                    layer.open({
                        type: 2,
                        content: "${ctx}/service/order/turnDisOrderForm?orderId="+ orderId+"&employeName="+employeName //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                        , title: "转派"
                        , btn: ['确认转派']
                        , area: ["700px", "430px"]
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'sureSubmit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var obj=layero.find('iframe').contents().find("#zhijiepaidan tr").find("input[name='emplyid']:checked");
                                var empIds= '';
                                obj.each(function () {
                                    if (isBlank(empIds)) {
                                        empIds = $(this).val();
                                    } else {
                                        empIds = $(this).val() + ',' + empIds;
                                    }
                                });
                                var empName = layero.find('iframe').contents().find("#paigongzhi").html();
                                var empMobile = layero.find('iframe').contents().find("#mobiles").val();
                                var textarea=layero.find('iframe').contents().find("#remarks").val();
                                if (isBlank(empIds)) {
                                    layer.msg('请选择工程师', {icon: 2});
                                    return false;
                                }

                                if (isBlank(textarea)) {
                                    layer.msg('请输入转派原因', {icon: 2});
                                    return false;
                                }
                                $.ajax({
                                    type: "POST",
                                    url: "${ctx}/service/order/tunDispatchForm",
                                    data: {
                                        orderIds: orderId,
                                        empIds:empIds,
                                        empName:empName,
                                        employeMobile:empMobile,
                                        transferReasons:textarea
                                    },
                                    success: function (result) {
                                        if (result.code == 200) {
                                            layer.closeAll()
                                            layer.msg('转派成功！', {icon: 1, time: 1000}, function () {
                                                window.location.reload();
                                            });
                                        } else if (result.code == "505") {
                                            layer.msg('转派失败!', {icon: 2, time: 1000}, function () {
                                                layer.closeAll()
                                            });
                                        } else if (result.code == "405") {
                                            layer.msg('该工单不可转派，请刷新页面!', {icon: 2, time: 1000});
                                        } else if (result.code == "402") {
                                            layer.msg('该工单有申请备件，不能转派！', {icon: 2, time: 1000});
                                        }else if (result.code == "403") {
                                            layer.msg('该工单有收款记录，不能转派！', {icon: 2, time: 1000});
                                        }else {
                                            layer.msg('转派失败,数据有误', {icon: 2, time: 1000});
                                        }
                                    }
                                });

                            });
                            submit.trigger('click');
                        },
                        success: function (layero) {
                            layero.find('.layui-layer-btn').css('text-align', 'center');
                            layero.find('.layui-layer-btn').find(".layui-layer-btn0").css("background-color", "#009688");
                        }
                    });
                    break;
                case 'invalid':                        //无效工单
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length != 1) {
                        layer.msg("请选择一条数据",{icon:5,anim:6});
                        return;
                    }
                    var orderId = data[0].id;
                    var status = data[0].site_status;
                    if(status ==2 || status==7 || status==8 || status==9 ){
                        layer.msg("当前状态不可操作无效",{icon:5,anim:6});
                        return;
                    }else{
                        var indexla = layer.confirm('您确定要操作无效工单吗？', {
                            icon:3,
                            btn: ['确定','取消'] //按钮
                        }, function(){
                            layer.close(indexla);
                            layer.open({
                                type: 2,
                                content: "${ctx}/order/siteInvalid?orderId="+orderId  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                                ,title: "无效工单"
                                ,area:["600px","220px"]
                                ,success : function(layero) {
                                    layero.find('.layui-layer-btn').css('text-align', 'center');
                                    layero.find('.layui-layer-btn').find(".layui-layer-btn0").css("background-color","#009688");
                                }
                            })

                        }, function(){
                            layer.close(indexla)
                        });
                    }
                    break;
                case 'appointment':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length != 1) {
                        layer.msg("请选择一条数据",{icon:5,anim:6});
                        return false;
                    }
                    var orderId = data[0].id;
                    var status = data[0].status;
                    if(data[0].site_status == '1' ||data[0].site_status == '3'||data[0].site_status == '7'){
                        layer.msg("选择的工单不满足预约条件！");
                        return
                    }
                    layer.open({
                        type: 2,
                        content: "${ctx}/service/order/appointment?orderId="+orderId  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                        ,title: "预约"
                        ,btn:['确认']
                        ,area:["400px","450px"]
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'sureSubmit'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var promiseTime=layero.find('iframe').contents().find("#promiseTime").val();
                                var promiseLimit=layero.find('iframe').contents().find("#promiseLimit").val();
                                if(isBlank(promiseTime)){
                                    layer.msg('请选择预约时间', {icon:2});
                                    return false;
                                }
                                if(isBlank(promiseLimit)){
                                    layer.msg('请选择时间要求', {icon:2});
                                    return false;
                                }
                                $.ajax({
                                    type:"POST",
                                    url:"${ctx}/service/order/saveAppointment",
                                    data:{
                                        orderId:orderId,
                                        promiseTime:promiseTime,
                                        promiseLimit:promiseLimit
                                    },
                                    success:function(result){
                                        if (result.code == "505") {
                                            layer.msg("编辑失败", {icon: 2});
                                        } else if(result.code == '200'){
                                            layer.msg("编辑成功", {icon: 1});
                                            window.location.reload();
                                        }else{
                                            layer.msg("编辑失败",{icon:2});
                                        }
                                    }, error: function () {
                                        layer.msg("编辑失败", {icon: 2});
                                    }
                                });

                            });
                            submit.trigger('click');
                        },
                        success : function(layero) {
                            layero.find('.layui-layer-btn').css('text-align', 'center');
                            layero.find('.layui-layer-btn').find(".layui-layer-btn0").css("background-color","#009688");
                        }
                    });



                    break;
            };
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('test', {
                where: field,
                page:{
                    curr:1
                }
            });
        });



    });

    function orderFlag(id){
        layer.open({
            type: 2,
            content: "${ctx}/order/getFlagOrder?orderId="+id //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            ,title: "标记工单"
            ,area:["300px","260px"]
            ,closeBtn: 0
            ,shadeClose:true

        });
    }


    /*工单详情*/
    function orderDetail(id) {
        layer.open({
            type: 2,
            title:" 工单信息",
            area: ['1150px', '81%'],
            resize:false,
            content: '${ctx}/service/order/serviceGetOrderDetail?orderId='+id+"&type=8",
            cancel:function(){
              /*  window.location.href="${ctx}/service/order/getFormOrder"*/
            }
        });
    }

    function reset(){
        layui.form.render();
        $("#searchForm").find("select").val("");
        $("#searchForm").find("input").val("");
    }

    //备件详情
    function fittingDetail(id){
        layer.open({
            type: 2
            , title: '备件信息'
            , content: '${ctx}/platform/fitting/toFittingDetailNo?id=' + id
            , area: ['1000px', '50%']
            , yes: function(index, layero){

            }
        });
    }

    function Refresh(){
        window.location.reload();
    }


    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }
</script>
</body>
</html>

