using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Helper;
using PTMedicalInsurance.Variables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PTMedicalInsurance.Business
{
    class OPSettlementCancelProcess : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            string newSettleID = "",outParam = "";
            dynamic rtnResult = (0,"",outParam);
            if (cBus.cancleSettlement(TradeEnum.OutpatientSettlementCancel, out outParam) != 0)
            {
                int i = outParam.IndexOf("获取到的就诊信息为无效状态，不能办理结算撤销业务");
                if (i != 0)
                {
                    if (DialogResult.Yes != MessageBox.Show("医保中心提示：" + outParam + "，是否强制撤销HIS结算信息？", "提示", MessageBoxButtons.YesNo))
                    {
                        return Error(-1, "中心取消结算失败，");
                    }

                    newSettleID = "YC";
                }
                else
                {
                    rtnResult = (-1, "取消中心结算", outParam);
                    //查看是医保平台是否已经退费，如果是，则询问是否继续退费
                    string SqlStr = " SELECT * FROM  BS_MedInsuSettlement WHERE  ValidFlag = 0 AND BillType = -1 ";
                    SqlStr = SqlStr + " AND Hospital_Dr=" + Global.inf.hospitalDr + " AND Interface_Dr=" + Global.inf.interfaceDr;
                    SqlStr = SqlStr + " AND SettlementID ='" + Global.pat.settlID + "'";

                    JObject joSqlStr = new JObject();
                    joSqlStr.Add("sqlStr", SqlStr);

                    JObject joCancleSettlInfo = mIS.QuerySettlementInfo(joSqlStr);

                    JArray jaCancleSettlInfo = JArray.Parse(JsonHelper.getDestValue(joCancleSettlInfo, "result.data"));
                    if (jaCancleSettlInfo.Count == 1)
                    {
                        //询问收款员是否继续进行HIS退费
                        if (DialogResult.Yes == MessageBox.Show("中心提示：" + outParam, " 该费用在医保中心已成功退费但在HIS未成功退费，是否强制退HIS费用？ ", MessageBoxButtons.YesNo))
                        {
                            rtnResult = (0, "该费用在医保中心已成功退费但在HIS未成功退费，收款员选择强制退HIS费用！", outParam);
                        }
                    }
                    else
                    {
                        rtnResult = (-1, "取消中心结算(云医保平台未退费或未查询到退费记录)", outParam);
                    }

                    return rtnResult;
                }
            }
            else
            {
                newSettleID = JsonHelper.getDestValue(JObject.Parse(outParam), "output.setlinfo.setl_id");
            }


            //退云医保平台结算
            if (mIS.cancleSettlement(newSettleID, out outParam) != 0)
            {
                return Error(-1, "取消平台结算信息");
            }

            return Success();
        }
    }
}
