apiready = function() {
    var ctrl = {
        logId: null,
        type: null,
        itemId: null,
        employeeId: '',
        problemId: '',

        init: {},
        bind: {},
        getBug: {},
        msgFilter: {} //拦截接口返回数据，对体验模式下的数据进行过滤
    }

    ctrl.init = function() {
        // 适配安卓状态栏
        CommonModel.fitInStatusBar();

        ctrl.employeeId = $api.getStorage('employeeId');
        ctrl.problemId = api.pageParam.id;

        ctrl.bind();
        ctrl.getBug();
    }

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

        //点击查看抢修单
        // $('.content').on('touchend', '#toRepairDetail', function() {
        //     api.openWin({
        //         name: 'repairLogDetail',
        //         url: 'widget://html/feature/logQuery/repairLogDetail.html',
        //         pageParam: {
        //             id: ctrl.id
        //         }
        //     });

        // })

        // “生成工单” 按钮 点击
        $('#create-order-btn').on('touchend', function() {
            api.confirm({
                title: '',
                msg: '确定要生成工单吗',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                var index = ret.buttonIndex;
                if (index == 1) { // 确定
                    ctrl.createOrder();
                }
            });
        })
    }

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

    //对接口返回的数据进行过滤，针对体验模式修改部分数据
    ctrl.msgFilter = function(response) {
        if ($api.getStorage('cus') && response.body.prName) {
            response.body.prName = $api.guestPrName;
            response.body.problemDesc = response.body.problemDesc.replace($api.guestKeysPattern, $api.guestName);
        }
    };

    ctrl.getBug = function() {
        if(api.pageParam.id) {
          // 根据id获取设备缺陷
          var data = {
            problemId: api.pageParam.id
          }
          $api.get('/ems/rest/xj/problem', data,function(ret, err) {
            if (!err && ret && ret.code == "200") {
                // 未处理，显示 生成工单 按钮
                if (ret.body.status == 0) {
                    $('#create-order-btn').show();
                }
                ctrl.render(ret);
            } else {

            }
          })
        }else {
          // 根据条件查询(从巡检日志异常项跳转)一个设备缺陷
          ctrl.logId = api.pageParam.logId;
          ctrl.type = api.pageParam.type;
          ctrl.itemId = api.pageParam.itemId;
          var data = {
              "taskId": this.logId,
              "itemId": this.itemId,
              "source": this.type
          }

          $api.get('/ems/rest/xj/problem/one', data, function(ret, err) {
              if (!err && ret && ret.code == "200") {
                  ctrl.render(ret);
              } else {

              }
          });
        }


    }
    ctrl.render = function(ret) {

      //针对体验模式过滤某些数据
      ctrl.msgFilter(ret);
      var item = ret.body;
      ctrl.prefix = ret.prefix;
      if (item.qxOrderId) {
          ctrl.id = item.qxOrderId;
          $('#toRepairDetail').removeClass('hide');
      } else {
          $('#toRepairDetail').addClass('hide');
      }
      $("#pr-name").text(item.prName);
      $("#inspector").text(item.employeeName);
      var dateUtils = new DateUtils();
      dateUtils.setDate(item.commitTime)
      var xjTime = dateUtils.getFormattedDate('yyyy-MM-dd hh:mm:ss');
      $("#time").text(xjTime ? xjTime : '不详');
      $("#desc").text(item.problemDesc);


      var photoBrowser = api.require('photoBrowser');
      var imageList = [];
      var picContainer = document.getElementById("pic-con");
      //点击图片  使用图片查看器查看
      picContainer.addEventListener("touchend", function() {
          //初始化图片查看器
          photoBrowser.open({
              images: imageList,
              bgColor: '#000'
          }, function(ret, err) {
              if (ret && ret.eventType == "click") {
                  photoBrowser.close();
              }
          });
      })

      //图片
      var pics = item.picture ? item.picture.split(",") : [];
      var length = pics.length;

      for (var i = 0; i < length; i++) {
          (function() {
              var image = new Image();
              image.src = "../../../image/pre-pic.png";
              image.onload = function() {
                  picContainer.appendChild(image);
              }

              api.imageCache({
                  url: ctrl.prefix + pics[i],
                  policy: "cache_else_network",
                  thumbnail: false
              }, function(ret, err) {
                  if (ret.status) {


                      image.src = ret.url;
                      image.onload = function() {
                          picContainer.appendChild(image);
                          photoBrowser.appendImage({
                              images: [ret.url]
                          });

                          imageList.push(ret.url);
                      }
                  } else {
                      image.src = "../../../image/img-error.png";
                      image.onload = function() {
                          picContainer.appendChild(image);
                          photoBrowser.appendImage({
                              images: [image.src]
                          });

                          imageList.push(image.src);
                      }
                  }

              })
          })()


      }
    }

    // 生成工单
    ctrl.createOrder = function () {
        var url = '/test/gong-dan/xjProblemTransferToWorkOrder/'
            + ctrl.employeeId + '/'
            + ctrl.problemId;

        $api.post(url, function(ret, err) {
            if (!err && ret && ret.code == "200") {
                ctrl.toast('生成成功')

                api.closeWin();
            } else {
                ctrl.toast('生成失败,' + ret.msg)
            }
        })
    };
    ctrl.init();
}
