///<reference path="/Scripts/ExtPB_Net.js" />
///<reference path="/Scripts/MyExtPB.js" />
///<reference path="/Scripts/ExtPB_Dll_Pb_Http.js" />
///<reference path="/Scripts/MyExtPb_Input.js" />

function W_YiBanCuXiaoSheZhi() {

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var dw_list = new DataWindow(requestor);
    var tb_1 = new ToolStrip(requestor);
    var dw_mkfhjb = new DataWindow(requestor);

    if (typeof (this.bind) == "function") var baseBind = this.bind;
    this.bind = function (win) {
        self.Instance = win;
        if (typeof (baseBind) == "function") baseBind(win);

        if (requestor == null) requestor = new PBWindow();
        requestor.Attach(win);
        if (dw_list == null) dw_list = new DataWindow(requestor);
        dw_list.Attach(win.Control("dw_list"));
        if (tb_1 == null) tb_1 = new ToolStrip(requestor);
        tb_1.Attach(win.Control("tb_1"));
        if (dw_mkfhjb == null) dw_mkfhjb = new DataWindow(requestor);
        dw_mkfhjb.Attach(win.Control("dw_mkfhjb"));

        if (typeof (self.PostOpen) == "function")
            win.AttachEvent("window", "PostOpen", self.PostOpen);

        if (typeof (self.DoubleClicked) == "function")
            win.AttachEvent("dw_list", "DoubleClicked", self.DoubleClicked);

        if (typeof (self.List_DropDown) == "function")
            win.AttachEvent("dw_list", "RowFocusChanged", self.List_DropDown);

        if (typeof (self.Add) == "function")
            win.AttachEvent("btn_Add", "Clicked", self.Add);

        if (typeof (self.Edit) == "function")
            win.AttachEvent("btn_Edit", "Clicked", self.Edit);

        if (typeof (self.Delete) == "function")
            win.AttachEvent("btn_Delete", "Clicked", self.Delete);

        if (typeof (self.Audit) == "function")
            win.AttachEvent("btn_Audit", "Clicked", self.Audit);

        if (typeof (self.UnAudit) == "function")
            win.AttachEvent("btn_UnAudit", "Clicked", self.UnAudit);

        if (typeof (self.OpenOrClose) == "function")
            win.AttachEvent("btn_zz", "Clicked", self.OpenOrClose);

        if (typeof (self.print) == "function")
            win.AttachEvent("btn_Print", "Clicked", self.print);

        if (typeof (self.Export) == "function")
            win.AttachEvent("btn_Export", "Clicked", self.Export);

        if (typeof (self.Exit) == "function")
            win.AttachEvent("btn_Exit", "Clicked", self.Exit);

    }

    if (typeof (this.unbind) == "function") var baseUnBind = this.unbind;
    this.unbind = function () {
        self.Instance = null;
        if (typeof (this.Release) == "function") this.Release();
        if (typeof (baseUnBind) == "function") baseUnBind();

        dw_list.Detach(); dw_list = null;
        tb_1.Detach(); tb_1 = null;
        dw_mkfhjb.Detach(); dw_mkfhjb = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

    //以上代码由设计器自动生成,请不要手工修改
    //ExtPB.Net JavaScript End  设计器查找标志,请不要删除或修改
    //    var webReq = new WebRequestor();
    //    webReq.SetWebServiceUrl("/Service/ServiceOtherDangAn.ashx");

    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCuXiaoFangAn.ashx");
    var curW_YiBanCuXiaoSheZhi_Edit = new W_YiBanCuXiaoSheZhi_Edit();
   
     //#region 新增
    this.Add = function () {
         curW_YiBanCuXiaoSheZhi_Edit.SetDataWindow(dw_list,"") // 这个用来给子窗口Set当前数据窗口
         requestor.Open("/Service/LoadWindow.ashx?WinName=A8ERP.CuXiao.YiBanCuXiaoSheZhi.W_YiBanCuXiaoSheZhi_Edit&curParm1=ADD", curW_YiBanCuXiaoSheZhi_Edit, null);

     } 
     //#endregion
    
     //#region 修改
     this.Edit = function () {
         var curPzh = dw_list.GetItemString(dw_list.GetRow(), "pzh");
         if (curPzh.length < 1) {
             requestor.MessageBox("提示", "请先选择方案后再修改。", ICON.StopSign);
             return;
         }
         var CURFHJB = dw_list.GetItemString(dw_list.GetRow(), "FHJB");
         curW_YiBanCuXiaoSheZhi_Edit.SetDataWindow(dw_list, curPzh) // 这个用来给子窗口Set当前数据窗口
         requestor.Open("/Service/LoadWindow.ashx?WinName=A8ERP.CuXiao.YiBanCuXiaoSheZhi.W_YiBanCuXiaoSheZhi_Edit&curParm1=EDIT" + "&CURFHJB=" + CURFHJB + "&CURGNQXSTR=" + requestor.GetParm("CURGNQXSTR"), curW_YiBanCuXiaoSheZhi_Edit, null);

     }

     //#endregion

     this.DoubleClicked = function (x, y, row, dwcName) {
         self.Edit();
     } 

     //#region 删除
     this.Delete = function () {
         var curPzh = dw_list.GetItemString(dw_list.GetRow(), "pzh");
         var curStatus = "";
         if (curPzh.length < 1) {
             requestor.MessageBox("提示", "请选择一笔数据", ICON.Exclamation);
             return;
         }

         if (!confirm("您确定要删除吗？")) {
             return;
         }
         webReq.Invoke("W_YiBanCuXiaoSheZhi_Delete", "curPzh=" + curPzh);
         if (webReq.StatusCode() == 200) {
             requestor.MessageBox("提示", "删除成功!", ICON.Information);
             var rowId = dw_list.GetRow();
     
             dw_list.RowsDiscard(rowId, rowId, DWBUFFER.Primary);
         }
         else {
             requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
             return;
         }
     }

     //#endregion

     //#region 启用
     this.OpenOrClose = function () {
         var curPzh = dw_list.GetItemString(dw_list.GetRow(), "pzh");
         var curStatus = "";
         if (curPzh.length < 1) {
             requestor.MessageBox("提示", "请选择一笔数据", ICON.Exclamation);
             return;
         }
         var status = dw_list.GetItemString(dw_list.GetRow(), "qyzt");
         if (status == "1") {
             curStatus = "0";
         } else {
             curStatus = "1";
         }
         webReq.Invoke("W_YiBanCuXiaoSheZhi_Edit_EnableOrDisable", "curPzh=" + curPzh + "&curStatus=" + curStatus);
         if (webReq.StatusCode() == 200) {
             requestor.MessageBox("提示", "处理成功!", ICON.Information);
             var row=dw_list.GetRow()
             dw_list.Refresh();
             dw_list.ScrollToRow(row);
             return;
         }
         else {
             requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
             return;
         }
     }
     //#endregion   

     //#region this.DropDown
     this.List_DropDown = function () {
         var status = dw_list.GetItemString(dw_list.GetRow(), "qyzt"); 
         if (status == "1") { 
             //启用着就要终止
             tb_1.SetImage("btn_zz", "Custom024!")
             tb_1.SetText("btn_zz", "终止");
         } else { 
             //终止着就要启用
             tb_1.SetImage("btn_zz", "Run!")
             tb_1.SetText("btn_zz", "启用");
         } 

         var fhStatus = dw_list.GetItemString(dw_list.GetRow(), "fhjb");
         if (fhStatus == "F") {
             tb_1.SetEnabled("btn_Delete", true);
             tb_1.SetEnabled("btn_Audit", true);
             tb_1.SetEnabled("btn_UnAudit", false);
             tb_1.SetEnabled("btn_Edit", true);

         } else if (fhStatus == "T") {
             tb_1.SetEnabled("btn_Delete", false);
             tb_1.SetEnabled("btn_Audit", false);
             tb_1.SetEnabled("btn_Edit", false);
             tb_1.SetEnabled("btn_UnAudit", true);

         } else {
             tb_1.SetEnabled("btn_Delete", true);
             tb_1.SetEnabled("btn_Audit", true);
             tb_1.SetEnabled("btn_UnAudit", true);
             tb_1.SetEnabled("btn_Edit", true);
         }
         
         //var flag = requestor.GetParm["flag"]
         tb_1.SetEnabled("btn_PriorRow", true);
         tb_1.SetEnabled("btn_NextRow", true);
         if (dw_list.GetRow() <= 1) {
             tb_1.SetEnabled("btn_PriorRow", false);
         } else if (dw_list.GetRow() >= dw_list.RowCount()) {
             tb_1.SetEnabled("btn_NextRow", false);
         }

         var curFhjbs = dw_list.GetItemString(dw_list.GetRow(), "fhjb"); // 当前
 
         if (curFhjbs == "T") {
             var curRowNo = dw_mkfhjb.Find("fhjb='T'", 1, dw_mkfhjb.RowCount());  // 当前行
             tb_1.SetText("btn_Audit", dw_mkfhjb.GetItemString(curRowNo, "dygn"));
            } else {
             var curRowNo = dw_mkfhjb.Find("fhjb='" + curFhjbs + "'", 1, dw_mkfhjb.RowCount());  // 当前行
             var curFhjbStrs = self.GetAuditStatus(curFhjbs);
             var curToFhjbs = curFhjbStrs.split("#")[0];
             tb_1.SetText("btn_Audit", dw_mkfhjb.GetItemString(curRowNo, "dygn")); 

         }



     }
     //#endregion

     this.Exit = function () {
         setTimeout("window.close();", 100);
     }


     this.PostOpen = function () {

         var curFormPzh = requestor.GetParm("curFormPzh");
         if (curFormPzh.length > 0) {
             var temp = "pzh='" + curFormPzh + "'";
             var row = dw_list.Find("pzh='" + curFormPzh + "'", 1, dw_list.RowCount());
             if (row > 0) {
                 dw_list.ScrollToRow(row);
                 self.Edit();
             }
         }
     }

     //#region 审核
     this.Audit = function () {
         var curShjb = "F";
         if (dw_list.GetRow < 1) {
             alert("请先选择一笔数据进行审核");
             return;
         }
         var pzh = dw_list.GetItemString(dw_list.GetRow(), "pzh");
         if (pzh.length < 1) {
             alert("凭证号异常");
             return;
         }
         if (!confirm("您确定要审核凭证号为[" + pzh + "]的方案吗？")) {
             return;
         }
         var curFhjb = dw_list.GetItemString(dw_list.GetRow(), "fhjb"); // 当前
         var curFhjbStr = self.GetAuditStatus(curFhjb);
         var curToFhjb = curFhjbStr.split("#")[0];
         var curToShowName = dw_mkfhjb.GetItemString(curFhjbStr.split("#")[1], "fhmc");
         webReq.Invoke("W_YiBanCuXiaoSheZhi_Audit", "curPzh=" + pzh + "&curShjb=" + curToFhjb);
         if (webReq.StatusCode() == 200) {
             //#region 刷新汇总表
             dw_list.SetRedraw(false);
             dw_list.SetProcessing(true);

             //刷新
             // dw_list.Refresh();
             dw_list.Retrieve();
             //找到相应的记录
             var findRow = dw_list.Find("pzh='" + pzh + "' ", 1, dw_list.RowCount());
             if (findRow > 0) {
                 dw_list.ScrollToRow(findRow);
             }
             dw_list.SetProcessing(false);
             dw_list.SetRedraw(true);
             //#endregion

             // 下一笔将要显示在按钮上的功能
             var curNextFhjbStr = self.GetAuditStatus(curToFhjb);
             var curToButtonShowName = dw_mkfhjb.GetItemString(curFhjbStr.split("#")[1], "dygn");
             //   dw_mx_top.Modify("ysh_q_t.visible='1' ysh_t.visible='1' ysh_t.Text='" + curToShowName + "'");
             tb_1.SetText("btn_Audit", curToButtonShowName);
         }
     }
     //#endregion

     //#region 退审
     this.UnAudit = function () {
         if (dw_list.GetRow < 1) {
             alert("请先选择一笔数据进行退审");
             return;
         }
         var pzh = dw_list.GetItemString(dw_list.GetRow(), "pzh");
         if (pzh.length < 1) {
             alert("凭证号异常");
             return;
         }
         if (!confirm("您确定要退审凭证号为[" + pzh + "]的方案吗？")) {
             return;
         }
         webReq.Invoke("W_YiBanCuXiaoSheZhi_UnAudit", "curPzh=" + pzh);
         if (webReq.StatusCode() == 200) {

             //#region 刷新汇总表
             dw_list.SetRedraw(false);
             dw_list.SetProcessing(true);

             //刷新
             //             dw_list.Refresh();
             dw_list.Retrieve();
             //找到相应的记录
             var findRow = dw_list.Find("pzh='" + pzh + "' ", 1, dw_list.RowCount());
             if (findRow > 0) {
                 dw_list.ScrollToRow(findRow);
             }
             dw_list.SetProcessing(false);
             dw_list.SetRedraw(true);
             //#endregion             

          //   self.Hz_RowFocusChanged(dw_list.GetRow());
         }
     }
     //#endregion

     //#region 审核级别

     this.GetAuditStatus = function (curFhjb) {
        
         if (curFhjb == "T") {
             tb_1.SetEnabled("btn_Audit", false);
             tb_1.SetEnabled("btn_UnAudit", true); 
             return;
         }
         var fhjbRow = dw_mkfhjb.Find("fhjb='" + curFhjb + "'", 1, dw_mkfhjb.RowCount());  // 当前行

         var curShjbRow = 0;
         if (dw_mkfhjb.RowCount() - 2 == 0) {  //没有级别，只有2条数据
             curShjb = "T";
             curShjbRow = dw_mkfhjb.Find("fhjb='T'", 1, dw_mkfhjb.RowCount());  // 当前行
         } else {
             for (var i = 1; i <= dw_mkfhjb.RowCount() - 2; i++) {
                 if (parseInt(curFhjb == "F" ? "0" : curFhjb) < parseInt(dw_mkfhjb.GetItemString(i, "fhjb"))) {
                     curShjb = dw_mkfhjb.GetItemString(i, "fhjb");
                     curShjbRow = i;
                     break;
                 } else if (i == dw_mkfhjb.RowCount() - 2) {
                     curShjb = "T";
                     curShjbRow = dw_mkfhjb.Find("fhjb='T'", 1, dw_mkfhjb.RowCount());  // 当前行
                 }

             }
         }
         return curShjb + "#" + curShjbRow
     }
     //#endregion
}