

apiready = function() {
    var ctrl = {
        init: {},
        bind: {},
        prefix: 'https://cdyfile.saas.dianwutong.com/',
        orderData: {
            pics: [],
            serverPicUrls: [],
            uploadPicNum: 0,
            desc: '',
            prId: null,
        },
    };

    ctrl.init = function() {
        CommonModel.fitInStatusBar();

        ctrl.orderId = api.pageParam.id;
        ctrl.orderType = api.pageParam.type;

        // "未接单" 工单 显示删除工单按钮
        if (ctrl.orderType == 0) {
            $('#delete-btn').show()
        }

        ctrl.bind();

        ctrl.initOrderDetail();
    }
    ctrl.bind = function() {
        //点击返回按钮
        $api.addEvt($api.dom("#back"), "touchend", function() {
            api.closeWin();
        });

        // 点击图片预览
        $("#content").on("touchend", ".problem-pic-item", function (e) {
            e.stopPropagation();
            var src = $(this).attr('src');
            
            $('#pic-review img').attr('src', src);
            $('#pic-review').css('display', 'block');
        });

        // 点击预览图片 关闭预览
        $('#pic-review').on("touchend", function () {
            $(this).css('display', 'none');
        });
        // 点击 删除工单
        $('#delete-btn').on("touchend", function () {
            api.confirm({
                title: '',
                msg: '确定要删除工单吗',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                var index = ret.buttonIndex;
                if (index == 1) { // 确定
                    ctrl.deleteSystemOrder();
                }
            });
        });
    };

    /**
     * 弹出提示框
     */
     ctrl.toast = function (msg) {
        api.toast({
            msg: msg,
            duration: 3000,
            locaiton: 'top'
        });
    };

    // 初始化 工单详情
    ctrl.initOrderDetail = function () {
        ctrl.getOrderDetail();
    };

    // 获取工单详情
    ctrl.getOrderDetail = function () {
        var url = '/test/gong-dan/queryById/' + ctrl.orderId;
        
        api.showProgress({
            title: '载入中...',
            text: '请稍后',
            modal: false
        });
        $api.get(url, function(res, err) {
            api.hideProgress();
            if (err) {
                ctrl.toast("网络请求失败");
            } else {
                if (!res.code || res.code != "200" || !res.data) {
                    ctrl.toast("服务器响应错误" + (res.code ? ("，错误码：" + res.code) : ""));
                } else {
                    ctrl.renderOrderDetail(res.data);
                }
            }
        });
    };

    // 渲染工单详情
    ctrl.renderOrderDetail = function (data) {
        $('#pr-name').text(data.prName);
        $('#person').text(data.tiJiaoRenName);
        $('#create-time').text(data.tiJiaoShiJian);
        $('#desc').text(data.miaoShu);
        $('#result-desc').text(data.chuLiMiaoShu || '无');
        $('#result-person').text(data.chuLiRenName || '无');
        $('#handle-time').text(data.chuLiShiJian || '无');
        $('#complete-time').text(data.wanChengShiJian || '无');
        
        // 渲染问题照片
        var picContentDom = $('#pic-content');
        if (data.zhaoPian) {
            ctrl.renderPics(picContentDom, data.zhaoPian);
        } else {
            picContentDom.html('无');
        };

        // 渲染处理照片
        var resultPicContentDom = $('#result-pic-content');
        if (data.chuLiZhaoPian) {
            ctrl.renderPics(resultPicContentDom, data.chuLiZhaoPian);
        } else {
            resultPicContentDom.html('无');
        };
    };

    // 渲染 图片
    ctrl.renderPics = function(dom, pics) {
        dom.empty();

        var picsArr = pics.split(',');
        var length = picsArr.length;

        for (var i = 0; i < length; i++) {
            var picUrl = ctrl.prefix + picsArr[i];
            var picDom = '<span class="pic-item"><img class="problem-pic-item" src="'+ picUrl +'" alt=""></span>';

            dom.append(picDom);
        }
    };

    // 删除工单
    ctrl.deleteSystemOrder = function () {
        api.showProgress({
            title: '载入中...',
            text: '请稍后',
            modal: false
        });
        var url = '/test/gong-dan/deleteById';
        var data = {
            id: ctrl.orderId
        };

        $api.post(url, data, function(res, err) {
            api.hideProgress();
            if (err) {
                ctrl.toast("网络请求失败");
            } else {
                if (!res.code || res.code != "200") {
                    ctrl.toast(res.msg);
                } else {
                    // 返回工单列表页
                    api.sendEvent({
                        name: 'orderListChanged'
                    });

                    ctrl.toast("删除成功");
                    api.closeWin();
                }
            }
        });
    };

    ctrl.init();
}
