<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>修改申请待审批</title>
  <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
  <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
</head>
<body>
<div class="layuimini-container">
  <div class="layuimini-main">
    <script type="text/html" id="toolbarDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="view">
          <i class="layui-icon layui-icon-file" style="font-size: 10px;"></i>查看
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="spOption">
          <i class="layui-icon layui-icon-edit" style="font-size: 10px;"></i>审核
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="spList">
          <i class="layui-icon layui-icon-list" style="font-size: 10px;"></i>审批列表
        </button>
        <!--                <button class="layui-btn layui-btn-sm" lay-event="exportList">-->
        <!--                    <i class="layui-icon layui-icon-export" style="font-size: 10px;"></i>导出列表-->
        <!--                </button>-->
      </div>
    </script>

    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

  </div>
</div>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script th:src="@{/lib/xm-select-v1.2.2/xm-select.js}" charset="utf-8"></script>
<script th:inline="javascript" type="text/javascript">
  AjaxUtil.ctx = /*[[@{/}]]*/'';
  var updateflag = 0;
  var xmApproval = {};
  layui.use(['form', 'table'], function () {
    var form = layui.form,
            table = layui.table;

    var currTable = table.render({
      elem: '#currentTableId',
      url: AjaxUtil.ctx + 'prjApproverecord/xgsqList',
      method: 'post',
      toolbar: '#toolbarDemo',
      defaultToolbar: ['filter', 'exports', 'print'],
      cols: [
        [
          {type: "checkbox", fixed: 'left'},
          {type: 'numbers', align: 'center',title: '序号'},
          {field: 'spzt', width: '12%', title: '审批状态', sort: true},
          {field: 'xmlxmc', width: '10%', title: '类型', sort: true},
          {field: 'xm', width: '35%', title: '项目/申请', sort: true},
          // {field: 'zt', width: '12%',title: '状态', sort: true},
          {field: 'tjrxm', width: '8%', title: '提交人', align: 'center'},
          {field: 'tjsj', width: '12%', title: '提交时间', align: 'center'}
          // {title: '操作', toolbar: '#currentTableBar', align: 'center', fixed: 'right'}
        ]
      ],
      sortName: 'xm',
      autoSort: false,
      limits: [10, 15, 20, 25, 50, 100],
      limit: 10,
      page: true,
      skin: 'grid',
      parseData: function (res) { //res 即为原始返回的数据
        return {
          "code": res.code, //解析接口状态
          "msg": res.message, //解析提示文本
          "count": res.data.total, //解析数据长度
          "data": res.data.rows //解析数据列表
        };
      },
      done: function (res, curr, count) { // done为数据渲染完的回调
        // 测试打印出后端返回的权限列表
        // let permissionSet = [[${permissionSet}]];
        // console.log(JSON.stringify(permissionSet));
      }
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
      // 执行搜索重载
      currTable.reload({
        page: {
          curr: 1 // 页码从1开始
        },
        where: form.val("search-form")
      });
      return false;
    });

    // 监听重置操作
    form.on('submit(data-reset-btn)', function (data) {
      form.val("search-form", {
        "xm": '',
        "erpbh": '',
        "xmlx": '',
        "spzt": '',
        "tablename":''
      });
      // 执行搜索重载
      currTable.reload({
        page: {
          curr: 1
        },
        where: form.val("search-form")
      });
      return false;
    });

    // 监听排序事件
    table.on('sort(currentTableFilter)', function (obj) { //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
      currTable.reload({
        initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
        where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
          sortName: toLine(obj.field), //排序字段
          sortOrder: toLine(obj.type) //排序方式
        }
      });
    });

    // 监听行工具事件
    table.on('toolbar(currentTableFilter)', function (obj) {
      if (obj.event == 'view') {
        var projects = table.cache["currentTableId"];
        if (projects.length == 0)
          return;

        var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
        if (data == null || data.length === 0) {
          Message.warning("请选择要查看的记录！", 1500);
        } else if (data.length > 1) {
          Message.warning("请选择一条记录进行查看！", 1500);
        } else {
          var height = 1090;
          var title = '查看' + data[0].xmlxmc;
          var content = AjaxUtil.ctx;
          if (data[0].tablename == 'prj_Project')
            content += 'prjProject/forView/' + data[0].xmid;
          else if (data[0].tablename == 'prj_AnnualBudgetRecord') {
            height = 1015;
            content += 'PrjAnnualBudgetRecord/forView/' + data[0].xmid
          }
          layer.open({
            title: title,
            type: 2,
            shade: 0.3,
            maxmin: true,
            shadeClose: true,
            area: [height + 'px', layerheight + 'px'],
            content: content,
            end: function (index) {
              layer.close(index);
            }
          });
        }
      } else if (obj.event === 'spOption') {
        var projects = table.cache["currentTableId"];
        if (projects.length == 0)
          return;

        var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
        if (data == null || data.length === 0) {
          Message.warning("请选择要审批的记录！", 1500);
        } else if (data.length > 1) {
          Message.warning("请选择一条记录进行审核！", 1500);
        } else {
          var content = oneSP(data)
          layer.open({
            title: '审批意见',
            type: 2,
            shade: 0.3,
            maxmin: true,
            shadeClose: true,
            area: ['1025px', layerheight + 'px'],
            content: content,
            end: function () {
              if (updateflag == 1)
                currTable.reload();
              updateflag = 0;
            }
          });
        }
      } else if (obj.event === 'spList') {
        var projects = table.cache["currentTableId"];
        if (projects.length == 0)
          return;

        var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
        if (data == null || data.length === 0) {
          Message.warning("请选择要查看的记录！", 1500);
        } else if (data.length > 1) {
          Message.warning("请选择一条记录进行查看！", 1500);
        } else {
          xmApproval = {
            "xmid": data[0].xmid,
            "xm": data[0].xm
          };

          var content = AjaxUtil.ctx;
          if (data[0].tablename == 'sta_FeasibilityReport')
            content += 'reportinfo';
          else if (data[0].tablename == 'prj_Project')
            content += 'prjProject';
          else if (data[0].tablename == 'prj_AnnualBudget')
            content += 'prjAnnualbudget';
          else if (data[0].tablename == 'sta_BudgetApproval')
            content += 'staBudgetapproval';
          else if (data[0].tablename == 'sta_Reimbursement')
            content += 'staReimbursement';
          else if (data[0].tablename == 'prj_AnnualBudgetRecord')
            content += 'PrjAnnualBudgetRecord';
          else if (data[0].tablename == 'sta_PayApproval')
            content += 'staPayapproval';
          content += '/forApproveList?mc=' + data[0].xm;

          var index = layer.open({
            title: '审批列表',
            type: 2,
            shade: 0.3,
            maxmin: true,
            shadeClose: true,
            area: ['1300px', '750px'],
            content: content,
            end: function () {
              layer.close(index);
            }
          });
        }
      }
    });

    //双击
    table.on('rowDouble(currentTableFilter)', function (obj) {
      var projects = table.cache["currentTableId"];
      if (projects.length == 0)
        return;

      var checkStatus = table.checkStatus('currentTableId')
              , data = checkStatus.data;
      if (data == null || data.length === 0) {
        Message.warning("请选择要审批的记录！", 1500);
      } else if (data.length > 1) {
        for (var index in data) {
          bathSP(data[index])
          if (index == (data.length - 1)) {
            var tc = layer.alert('操作成功', function (index) {
              // 执行搜索重载
              currTable.reload({
                page: {
                  curr: 1
                },
                where: form.val("search-form")
              });
              layer.close(tc);
            });
          }
        }

      } else {
        var content = oneSP(data)
        layer.open({
          title: '审批意见',
          type: 2,
          shade: 0.3,
          maxmin: true,
          shadeClose: true,
          area: ['1025px', layerheight + 'px'],
          content: content,
          end: function () {
            if (updateflag == 1)
              currTable.reload();
            updateflag = 0;
          }
        });
      }
    });
  });

  function oneSP(data) {
    var content = AjaxUtil.ctx;
    if (data[0].tablename == 'sta_FeasibilityReport') {
      xmApproval = {
        "xmid": data[0].xmid,
        "xm": data[0].xm,
        "ysje": data[0].kysje
      };
      content += 'reportinfo/forApproveUpdate/' + data[0].xmid + '/' + data[0].kysje + '?spzt=' + data[0].spzt;
    } else if (data[0].tablename == 'prj_Project') {
      xmApproval = {
        "xmid": data[0].xmid,
        "xm": data[0].xm,
        "ysje": data[0].rysje
      };
      content += 'prjProject/forApproveUpdate/' + data[0].xmid + '/' + data[0].rysje + '?spzt=' + data[0].spzt;
    } else if (data[0].tablename == 'prj_AnnualBudget') {
      xmApproval = {
        "xmid": data[0].xmid,
        "xm": data[0].xm,
        "xmzys": data[0].xmzys
      };
      content += 'prjAnnualbudget/forApproveUpdate/' + data[0].xmid + '?spzt=' + data[0].spzt;
    } else if (data[0].tablename == 'prj_AnnualBudgetRecord') {
      xmApproval = {
        "xmid": data[0].xmid,
        "xm": data[0].xm,
        "xmzys": data[0].xmzys
      };
      content += 'PrjAnnualBudgetRecord/forApproveUpdate/' + data[0].xmid + '?spzt=' + data[0].spzt;
    } else if (data[0].tablename == 'sta_BudgetApproval') {
      xmApproval = {
        "xmid": data[0].xmid,
        "xm": data[0].xm,
        "xmzys": data[0].xmzys
      };
      content += 'staBudgetapproval/forApproveUpdate/' + data[0].xmid + '/' + data[0].sqje + '?spzt=' + data[0].spzt;
    } else if (data[0].tablename == 'sta_Reimbursement') {
      xmApproval = {
        "xmid": data[0].xmid,
        "xm": data[0].xm,
        "zt": data[0].spzt,
        "bzje": data[0].bzje
      };
      content += 'staReimbursement/forApproveUpdate/' + data[0].xmid + '/' + data[0].bzje + '?spzt=' + data[0].spzt;
    } else if (data[0].tablename == 'sta_PayApproval') {
      xmApproval = {
        "xmid": data[0].xmid,
        "xm": data[0].xm,
        "zt": data[0].spzt,
        "htje": data[0].htje
      };
      content += 'staPayapproval/forApproveUpdate/' + data[0].xmid + '/' + data[0].htje + '?spzt=' + data[0].spzt;
    }
    return content;
  }

  function bathSP(data) {
    if (data.tablename == 'sta_FeasibilityReport') {
      var xmApprovalBath = {
        "xmid": data.xmid,
        "xm": data.xm,
        "ysje": data.kysje
      };
      AjaxUtil.get({
        url: AjaxUtil.ctx + 'reportinfo/forApproveUpdate/' + data.xmid + '/' + data.kysje + '?spzt=' + data.spzt,
        data: data.field,
        success: function (res) {
          console.log(res);
          AjaxUtil.post({
            url: AjaxUtil.ctx + "prjApproverecord/doUpdateApprove",
            data: res.data.field,
            success: function (res) {
              //更新审批状态
              var project = {
                "id": xmApprovalBath.xmid,
                "spzt": '审批结束'
              }
              AjaxUtil.post({
                url: AjaxUtil.ctx + "reportinfo/updateSpzt",
                contentType: "application/json",
                data: JSON.stringify(project),
                success: function (res) {
                  return true;
                },
                error: function (error) {
                }
              });
            },
            error: function (error) {
            }
          });
        },
        error: function (error) {
        }
      });


    } else if (data.tablename == 'prj_Project') {
      var xmApprovalBath = {
        "xmid": data.xmid,
        "xm": data.xm,
        "ysje": data.rysje
      };
      AjaxUtil.get({
        url: AjaxUtil.ctx + 'prjProject/bathUpdate/' + data.xmid + '/' + data.rysje + '?spzt=' + data.spzt,
        data: data.field,
        success: function (res) {
          console.log(res);
          AjaxUtil.post({
            url: AjaxUtil.ctx + "prjApproverecord/doUpdateApprove",
            data: res.approveInfo,
            success: function (res) {
              //更新审批状态
              var project = {
                "id": xmApprovalBath.xmid,
                "spzt": '审批结束'
              }
              AjaxUtil.post({
                url: AjaxUtil.ctx + "prjProject/updateSpzt",
                contentType: "application/json",
                data: JSON.stringify(project),
                success: function (res) {
                  return true;
                },
                error: function (error) {
                }
              });
            },
            error: function (error) {
            }
          });
        },
        error: function (error) {
        }
      });
    } else if (data.tablename == 'prj_AnnualBudget') {
      var xmApprovalBath = {
        "xmid": data.xmid,
        "xm": data.xm,
        "xmzys": data.xmzys
      };
      AjaxUtil.get({
        url: AjaxUtil.ctx + 'prjAnnualbudget/bathUpdate/' + data.xmid + '?spzt=' + data.spzt,
        data: data.field,
        success: function (res) {
          console.log(res);
          AjaxUtil.post({
            url: AjaxUtil.ctx + "prjApproverecord/doUpdateApprove",
            data: res.approveInfo,
            success: function (res) {
              //更新审批状态
              var project = {
                "id": xmApprovalBath.xmid,
                "spzt": '审批结束'
              }
              AjaxUtil.post({
                url: AjaxUtil.ctx + "prjAnnualbudget/updateSpzt",
                contentType: "application/json",
                data: JSON.stringify(project),
                success: function (res) {
                  return true;
                },
                error: function (error) {
                }
              });
            },
            error: function (error) {
            }
          });
        },
        error: function (error) {
        }
      });
    } else if (data.tablename == 'prj_AnnualBudgetRecord') {
      var xmApprovalBath = {
        "xmid": data.xmid,
        "xm": data.xm,
        "xmzys": data.xmzys
      };
          //修改审批记录表
          AjaxUtil.post({
            url: AjaxUtil.ctx + "prjApproverecord/doUpdateApprove",
            data: res.approveInfo,
            success: function (res) {

              var project = {
                "id": xmApprovalBath.xmid,
                "spzt": '审批结束'
              }
              //修改年度预算明细表
            },
            error: function (error) {
            }
          });
    } else if (data.tablename == 'sta_BudgetApproval') {
      var xmApprovalBath = {
        "xmid": data.xmid,
        "xm": data.xm,
        "zt": data.spzt,
        "sqje": data.sqje
      };
      AjaxUtil.get({
        url: AjaxUtil.ctx + 'staBudgetapproval/bathUpdate/' + data.xmid + '/' + data.rysje + '?spzt=' + data.spzt,
        data: data.field,
        success: function (bathUpdateRes) {
          console.log(bathUpdateRes.approveInfo);
          AjaxUtil.post({
            url: AjaxUtil.ctx + "prjApproverecord/doUpdateApproveFysq",
            data: bathUpdateRes.approveInfo,
            success: function (res) {
              console.log(res);
              //更新审批状态
              var project = {
                "id": xmApprovalBath.xmid,
                "spzt": '',
                "sqje": xmApprovalBath.sqje,
                "operations":"bathsp"
              }
              if (bathUpdateRes.approveInfo.spcz == '返回修改') {
                project.spzt = '未提交(返回)';
              } else if (bathUpdateRes.approveInfo.zt == '部门主任审批') {
                project.spzt = '科技项目管理专责审批';
              } else if (bathUpdateRes.approveInfo.zt == '科技项目管理专责审批') {
                project.spzt = '科技部部门主任审批';
              } else if (bathUpdateRes.approveInfo.zt == '科技部部门主任审批') {
                project.spzt = '主管院领导审批';
              } else if (bathUpdateRes.approveInfo.zt == '主管院领导审批') {
                if (xmApprovalBath.sqje < 1000000) {
                  project.spzt = '审批结束';
                } else {
                  project.spzt = '院长审批';
                }
              } else if (bathUpdateRes.approveInfo.zt === '院长审批') {
                project.spzt = '审批结束';
              }
              AjaxUtil.post({
                url: AjaxUtil.ctx + "staBudgetapproval/updateSpzt",
                contentType: "application/json",
                data: JSON.stringify(project),
                success: function (res) {

                },
                error: function (error) {
                }
              });
            },
            error: function (error) {
            }
          });
        },
        error: function (error) {
        }
      });
    } else if (data.tablename == 'sta_Reimbursement') {
      var xmApprovalBath = {
        "xmid": data.xmid,
        "xm": data.xm,
        "zt": data.spzt,
        "bzje": data.bzje
      };
      AjaxUtil.get({
        url: AjaxUtil.ctx + 'staReimbursement/bathUpdate/' + data.xmid + '/' + data.bzje + '?spzt=' + data.spzt,
        data: data.field,
        success: function (bathUpdateRes) {
          console.log(bathUpdateRes.approveInfo);
          AjaxUtil.post({
            url: AjaxUtil.ctx + "prjApproverecord/doUpdateApprove",
            data: bathUpdateRes.approveInfo,
            success: function (res) {
              //更新审批状态
              var project = {
                "id": xmApprovalBath.xmid,
                "spzt": '审批结束'
              }
              AjaxUtil.post({
                url: AjaxUtil.ctx + "staReimbursement/updateSpzt",
                contentType: "application/json",
                data: JSON.stringify(project),
                success: function (res) {
                  return true;
                },
                error: function (error) {
                }
              });
            },
            error: function (error) {
            }
          });
        },
        error: function (error) {
        }
      });
    } else if (data.tablename == 'sta_PayApproval') {
      var xmApprovalBath = {
        "xmid": data.xmid,
        "xm": data.xm,
        "zt": data.spzt,
        "htje": data.htje
      };
      AjaxUtil.get({
        url: AjaxUtil.ctx + 'staPayapproval/bathUpdate/' + data.xmid + '/' + data.htje + '?spzt=' + data.spzt,
        data: data.field,
        success: function (bathUpdateRes) {
          console.log(bathUpdateRes.approveInfo);
          AjaxUtil.post({
            url: AjaxUtil.ctx + "prjApproverecord/doUpdateApproveFksq",
            data: bathUpdateRes.approveInfo,
            success: function (res) {
              if (res.code == 0) {
                //更新审批状态
                var project = {
                  "id": xmApprovalBath.xmid,
                  "spzt": ''
                }
                if (bathUpdateRes.approveInfo.spcz == '返回修改') {
                  project.spzt = '未提交(返回)';
                } else if (bathUpdateRes.approveInfo.zt == '科技项目管理专责2审批') {
                  project.spzt = '科技部部门主任审批';
                } else if (bathUpdateRes.approveInfo.zt === '科技部部门主任审批') {
                  project.spzt = '审批结束';
                }

                AjaxUtil.post({
                  url: AjaxUtil.ctx + "staPayapproval/updateSpzt",
                  contentType: "application/json",
                  data: JSON.stringify(project),
                  success: function (res) {
                    return true;
                  },
                  error: function (error) {
                  }
                });
              }
            },
            error: function (error) {
            }
          });
        },
        error: function (error) {
        }
      });
    }
  }

  function setUpdateFlag() {
    updateflag = 1;
  }

  function getXmApproval() {
    return xmApproval;
  }
</script>
</body>
</html>