
layui.use(['form', 'layer', 'jquery', 'laydate'], function () {
  var form = layui.form,
    layer = parent.layer === undefined ? layui.layer : parent.layer,
    $ = layui.jquery,
    laydate = layui.laydate;
  form.render('select');
  //日期
  laydate.render({
    elem: '#date'
  });
  //日期
  laydate.render({
      elem: '#date2'
  });
  //监听提交
  form.on('submit(SubmitData)', function (data) {
    //所有表单数据
    var dataArr = new Array();
    dataArr = data.field;

    var zbbm = $("#F_DEPTID").find("option:selected").val();
    var zbry = $("#F_CREATEBY").find("option:selected").val();
    if (zbbm == null || zbbm == "") {

      layer.open({
        title: '提示'
        , content: '转办部门不能为空!'
      });

      return false;
    }
    if (zbry == null || zbry == "") {


      layer.open({
        title: '提示'
        , content: '转办人员不能为空!'
      });
      return false;
    }

    //转办部门
    dataArr["F_DEPTCODE"] = $("#F_DEPTID").find("option:selected").text();
    //转办人员
    dataArr["F_USERNAME"] = $("#F_CREATEBY").find("option:selected").text();



    $.ajax({
      url: "/WorkOrder/workMyOrderAddData",
      type: "post",
      async: false,//同步请求
      contentType: "application/json",
      dataType: "text",
      cache: false,
      data: JSON.stringify(dataArr),
      // data: dataArr,
      success: function (thisResult, result) {

        if (thisResult == "True") {


          layer.open({
            content: '保存成功'
            ,offset: '40%'
            , btn: ['确定']
            , yes: function (index, layero) {
              layer.close(index);
              ////刷新父窗体

              var thisindex = parent.layer.getFrameIndex(window.name); //获取窗口索引
              parent.layer.close(thisindex);
            }
            , cancel: function () {
              var thisindex = parent.layer.getFrameIndex(window.name); //获取窗口索引
              parent.layer.close(thisindex);
            }
          });

          return false;
        } else {

          layer.open({
            title: '提示'
            ,offset: '40%'
            , content: '保存失败!'
          });
          return false;
        }

      },
      error: function (error) {
        return false;
      }
    });
    return false;
  });
  //监听关闭工单
  form.on('submit(CloseData)', function (data) {
    //所有表单数据
    var dataArr = new Array();
    dataArr = data.field;

    //var zbbm = $("#F_DEPTID").find("option:selected").val();
    //var zbry = $("#F_CREATEBY").find("option:selected").val();
    //if (zbbm == null || zbbm == "") {
    //    layui.layer.alert("转办部门不能为空!", {
    //        icon: 2
    //    });
    //    return false;
    //}
    //if (zbry == null || zbry == "") {
    //    layui.layer.alert("转办人员不能为空!", {
    //        icon: 2
    //    });
    //    return false;
    //}

    //转办部门
    dataArr["F_DEPTCODE"] = $("#F_DEPTID").find("option:selected").text();
    //转办人员
    dataArr["F_USERNAME"] = $("#F_CREATEBY").find("option:selected").text();

    $.ajax({
      url: "/WorkOrder/workMyOrderCloseData",
      type: "post",
      async: false,//同步请求
      contentType: "application/json",
      dataType: "text",
      cache: false,
      data: JSON.stringify(dataArr),
      // data: dataArr,
      success: function (thisResult, result) {

        if (thisResult == "True") {


          layer.open({
            content: '保存成功'
            , btn: ['确定']
            , yes: function (index, layero) {
              layer.close(index);
              ////刷新父窗体

              var thisindex = parent.layer.getFrameIndex(window.name); //获取窗口索引
              parent.layer.close(thisindex);
            }
            , cancel: function () {
              var thisindex = parent.layer.getFrameIndex(window.name); //获取窗口索引
              parent.layer.close(thisindex);
            }
          });

          return false;
        } else {
          layui.layer.alert("保存失败!", {
            icon: 2
          });

          return false;
        }

      },
      error: function (error) {
        return false;
      }
    });
    return false;
  });

  
  //.监听当前 select，动态改变子项
  form.on('select(firstfcoptype)', function (d) {

    $.ajax({
      url: "/Class/GetListData",
      type: "get",
      contentType: "application/json",
      dataType: "text",
      async: false,
      data:
      {
        Parentid: $("#OrderFirstDZ").val(),
        page: 1,
        limit: 100,
      },
      success: function (thisResult) {

        if (eval('(' + thisResult + ')').msg == "成功") {
          $("#OrderSecondDZ").empty();
          $("#OrderSecondDZ").append("<option value='' ></option>");
          var list = eval('(' + thisResult + ')').data;
          if (list.length > 0) {
            for (var i = 0; i < list.length; i++) {
              $("#OrderSecondDZ").append("<option  value='" + list[i].Classid + "'>" + list[i].Classname + "</option>");
            }
          }
          form.render('select');
        }
        else {
          layui.layer.alert("获取该部门下用户信息失败!", {
            icon: 2
          });
        }

      }
    });
  });

  //.监听当前 select，动态改变子项
  form.on('select(secondfcoptype)', function (d) {

    $.ajax({
      url: "/Class/GetListData",
      type: "get",
      contentType: "application/json",
      dataType: "text",
      async: false,
      data:
      {
        Parentid: $("#OrderSecondDZ").val(),
        page: 1,
        limit: 100,
      },
      success: function (thisResult) {

        if (eval('(' + thisResult + ')').msg == "成功") {
          $("#OrderThirdDZ").empty();
          $("#OrderThirdDZ").append("<option value='' ></option>");
          var list = eval('(' + thisResult + ')').data;
          if (list.length > 0) {
            for (var i = 0; i < list.length; i++) {
              $("#OrderThirdDZ").append("<option  value='" + list[i].Classid + "'>" + list[i].Classname + "</option>");
            }
          }
          form.render('select');
        }
        else {
          layui.layer.alert("获取该部门下用户信息失败!", {
            icon: 2
          });
        }

      }
    });
  });

})


//关闭当前页
$('.closeThisPage').on('click', function () {
  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
  parent.layer.close(index);
});
