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

namespace WinFrom1.ToFile
{
    public partial class TableButton : Form
    {
        IrisDataHelper irisData = new IrisDataHelper();
        InvokeHelper invoker = new InvokeHelper();
        JObject userMsg = (JObject)JsonConvert.DeserializeObject("");
        public string fromType = "";
        public TableButton(string type = "")
        {
            fromType = type;
            InitializeComponent();
            //GetTableData(this,EventArgs.Empty, type);
            this.Text = type;
            this.Load += new EventHandler(GetTableData);
            //switch (type)
            //{
            //    case "转院备案撤销":
            //        this.Load += new EventHandler(GetTableData);
            //        break;
            //    case "慢特病备案撤销":
            //        this.Load += new EventHandler(GetTableData);
            //        break;
            //    case "定点备案撤销":
            //        this.Load += new EventHandler(GetTableData);
            //        break;
            //    default:
            //        break;
            //}
        }


        #region 示例Json
        string rtn = "{\"body\":{\"output\":{\"medinsinfo\":[{\"fixmedins_code\":\"1200001001569\",\"fixmedins_name\":\"天津河西区普瑞眼科医院有限公司\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"05\"},{\"fixmedins_code\":\"5101001002094\",\"fixmedins_name\":\"成都普瑞眼科医院有限责任公司\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"11\"},{\"fixmedins_code\":\"5199001000058\",\"fixmedins_name\":\"成都普瑞眼科医院\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"10\"},{\"fixmedins_code\":\"5299001102440\",\"fixmedins_name\":\"贵州普瑞眼科医院\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"02\"},{\"fixmedins_code\":\"5399001000144\",\"fixmedins_name\":\"昆明普瑞眼科医院\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"04\"},{\"fixmedins_code\":\"6101001000198\",\"fixmedins_name\":\"西安普瑞眼科医院有限责任公司\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"03\"},{\"fixmedins_code\":\"6201001100030\",\"fixmedins_name\":\"兰州普瑞眼科医院\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"06\"},{\"fixmedins_code\":\"6299001100030\",\"fixmedins_name\":\"兰州普瑞眼科医院\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"06\"},{\"fixmedins_code\":\"6599001102925\",\"fixmedins_name\":\"乌鲁木齐普瑞眼科医院\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"06\"},{\"fixmedins_code\":\"6600001000519\",\"fixmedins_name\":\"乌鲁木齐普瑞眼科医院（有限公司）\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"05\"},{\"fixmedins_code\":\"H34010202798\",\"uscc\":\"91340102MA2WCNTB23\",\"fixmedins_name\":\"合肥瑶海区普瑞眼科医院\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"07\"},{\"fixmedins_code\":\"H34010400768\",\"uscc\":\"91340100MA2MYPJ64A\",\"fixmedins_name\":\"合肥普瑞眼科医院\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"04\"},{\"fixmedins_code\":\"H34012400958\",\"uscc\":\"91340124MA2RG8M04F\",\"fixmedins_name\":\"庐江普瑞眼科门诊部\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"11\"},{\"fixmedins_code\":\"H34017200925\",\"uscc\":\"91340111MA2PGYQ5X2\",\"fixmedins_name\":\"合肥经济开发区普瑞眼科门诊部\",\"fixmedins_type\":\"1\",\"hosp_lv\":\"11\"}]},\"infcode\":0,\"inf_refmsgid\":\"340121202209022050100314008840\"},\"code\":200,\"csbCode\":200,\"message\":\"SUCCESS\",\"requestId\":\"ac11000116621230090334471d0121\",\"state\":{}}";
        #endregion

        /// <summary>
        /// 动态的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
            if (dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() != "撤销") return;
            //JObject insuJson = new JObject();
            dynamic joData = new JObject();
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            joData.psn_no = row.Cells[0].Value.ToString();    //人员代码 2501
            joData.trt_dcla_detl_sn = row.Cells[3].Value.ToString();   //流水号
            joData.memo = "撤销备案";   //撤销原因
            //insuJson.Add(new JProperty("data", joData));
            //insuJson.Add(new JProperty("infno", "2506"));
            JObject joRtn = new JObject();

            switch (fromType)
            {
                case "转院备案撤销":
                    joRtn = invoker.invokeInsuService(JsonHelper.setCenterInpar("2502", joData.ToString()), "转院备案撤销");
                    break;
                case "慢特病备案撤销":
                    joRtn = invoker.invokeInsuService(JsonHelper.setCenterInpar("2504", joData.ToString()), "慢特病备案撤销");
                    break;
                case "定点备案撤销":
                    joRtn = invoker.invokeInsuService(JsonHelper.setCenterInpar("2506", joData.ToString()), "定点备案撤销");
                    break;
                default:
                    break;
            }

            if (joRtn.ToString().IndexOf("errorCode") != -1)
            {
                string errorCode = joRtn["errorCode"].ToString();
                if (errorCode == "-1")
                {
                    MessageBox.Show(joRtn["errorMessage"].ToString());
                    return;
                }
            }
            string code = joRtn["code"].ToString();
            string csbCode = joRtn["csbCode"].ToString();
            string message = joRtn["message"].ToString();

            if (code == "200" && message == "SUCCESS")      //调用IRIS
            {
                string infcode = joRtn["body"]["infcode"].ToString();
                if (infcode == "-1")
                {
                    MessageBox.Show("备案撤销失败（医保）！");
                    return;
                }

                JObject setJson = new JObject();
                string parNum = "";
                setJson.Add(new JProperty("params", SetBackDataJson(fromType, out parNum)));
                setJson.Add(new JProperty("code", parNum));
                //setJson.Add(new JProperty("session", irisData.AddUserJson()));
                setJson.Add(new JProperty("session", userMsg));
                JObject irsRtn = invoker.invokeIrisService(setJson.ToString().Replace("\r\n", ""), fromType);

                string errorCode = irsRtn["errorCode"].ToString().Trim();
                if (errorCode == "0")
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = new DataGridViewTextBoxCell();
                    this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "已撤销";
                }
                else {
                    MessageBox.Show("写入IRIS失败！");
                }
            }
        }

        #region 人员备案查询  
        /// <summary>
        /// 动态获取数据并填充
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void GetTableData(object sender, EventArgs e)
        {
            try
            {
                JObject setJson = new JObject();
                string parNum = "";
                switch (fromType)
                {
                    case "转院备案撤销":       //查询转院备案列表
                        {
                            setJson.Add(new JProperty("params",  SetGetDataJson("查询转院备案列表", "2502", out parNum)));
                            break;
                        }
                    case "慢特病备案撤销":        //查询慢特病备案列表
                        {
                            setJson.Add(new JProperty("params", SetGetDataJson("查询慢特病备案列表", "2504", out parNum)));
                            break;
                        }
                    case "定点备案撤销":       //查询定点备案列表
                        {
                            setJson.Add(new JProperty("params", SetGetDataJson("查询定点备案列表", "2506", out parNum)));
                            break;
                        }
                    default:
                        break;
                }
                setJson.Add(new JProperty("code", parNum));
                //setJson.Add(new JProperty("session", irisData.AddUserJson()));
                setJson.Add(new JProperty("session", userMsg));
                //return setJson;


                string inParms = setJson.ToString().Replace("\r\n", "");
                //JObject obRtn = invoker.invokeIrisService(inParms, fromType);

                #region 示例obRtn
                string ddd = "{\"errorCode\":\"0\",\"errorMessage\":\"\",\"result\":{\"data\":[{\"TransCode\":\"\",\"TransDesc\":\"\",\"CardID\":\"51277119900812897X\",\"Name\":\"高果果\",\"PsnNo\":\"501128\",\"InsuType\":\"\",\"Tel\":\"\",\"DiseCodg\":\"\",\"DiseName\":\"\",\"DiagCode\":\"\",\"DiagName\":\"\",\"FixmedinsCode\":\"\",\"FixmedinsName\":\"广州番禺普瑞眼科医院\",\"ReflinMedinsNo\":\"\",\"ReflinMedinsName\":\"\",\"BegnDate\":\"\",\"EndDate\":\"\",\"CreateDate\":\"2022-09-12\",\"CreateTime\":\"\",\"CreateUserDr\":\"\",\"InsuSerNo\":\"99000022122\",\"BizTypeFlag\":\"1\",\"FixSrtNo\":\"\",}]}}";
                JObject obRtn = (JObject)JsonConvert.DeserializeObject(ddd);

                #endregion
                if (obRtn.ToString().IndexOf("errorCode") != -1)
                {
                    string errorCode = obRtn["errorCode"].ToString();
                    if (errorCode == "-1")
                    {
                        MessageBox.Show(obRtn["errorMessage"].ToString());
                        return;
                    }
                }
                //string errorCode = obRtn["errorCode"].ToString().Trim();
                if (obRtn["errorCode"].ToString() == "0")
                {
                    //string data = obRtn["result"].ToString().Trim();
                    JArray newData = new JArray();
                    JArray data = (JArray)JsonConvert.DeserializeObject(obRtn["result"]["data"].ToString());
                    foreach (var item in data)
                    {
                        JObject tabRow = new JObject();
                        tabRow["人员编号"] = item["PsnNo"].ToString().Trim();
                        tabRow["姓名"] = item["Name"].ToString().Trim();
                        tabRow["身份证号"] = item["CardID"].ToString().Trim();
                        tabRow["申报流水号"] = item["InsuSerNo"].ToString().Trim();
                        tabRow["申报时间"] = item["CreateDate"].ToString().Trim();
                        //tabRow["业务类型"] = item["BizTypeFlag"].ToString().Trim();
                        tabRow["定点医疗机构"] = item["FixmedinsName"].ToString().Trim();
                        newData.Add(tabRow);
                    }

                    dataGridView1.DataSource = JsonConvert.DeserializeObject(newData.ToString());
                    int coon = dataGridView1.Columns.Count;
                    DataGridViewButtonColumn colHead = new DataGridViewButtonColumn();
                    colHead.HeaderText = "撤销备案";
                    this.dataGridView1.Columns.Add(colHead);
                    int rows = this.dataGridView1.Rows.Count;
                    for (int i = 0; i < rows; i++)
                    {
                        DataGridViewButtonCell btnCell = new DataGridViewButtonCell();
                        btnCell.Value = "撤销";
                        btnCell.FlatStyle = FlatStyle.Flat;
                        btnCell.Style.BackColor = Color.Tomato;
                        //dr.Cells.Add(btnCell);
                        this.dataGridView1.Rows[i].Cells[coon] = btnCell;
                    }
                    //根据数据自动调整列宽
                    for (int i = 0; i < coon + 1; i++)
                    {
                        dataGridView1.AutoResizeColumn(i);
                    }

                    this.dataGridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseDown);
                }
                else
                {
                    string errorMessage = obRtn["errorMessage"].ToString();
                    MessageBox.Show(errorMessage);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询备案列表异常，原因是：\r\n");
            }
        }

        /// <summary>
        /// 填充parms节内容
        /// </summary>
        /// <param name="types">业务名称</param>
        /// <param name="parNum">返回的业务接口</param>
        /// <returns></returns>
        private JObject SetGetDataJson(string types,string tranSCode, out string parNum)
        {
            parNum = "";
            dynamic joData = new JObject();
            switch (types)
            {
                case "查询转院备案列表":       //查询转院备案列表
                    {
                        parNum = "02030003";
                        break;
                    }
                case "查询慢特病备案列表":        //查询慢特病备案列表
                    {
                        parNum = "02030006";
                        break;
                    }
                case "查询定点备案列表":       //查询定点备案列表
                    {
                        parNum = "02030009";
                        break;
                    }
                default:
                    break;
            }
            joData.CardID = "";       //身份证号
            joData.TransCode = tranSCode;    //交易代码 2501
            joData.CreateDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");   //操作日期
            return joData;
        }
        #endregion

        #region 撤销操作

        /// <summary>
        /// 填充parms节内容
        /// </summary>
        /// <param name="types">业务名称</param>
        /// <param name="parNum">返回的业务接口</param>
        /// <returns></returns>
        private JObject SetBackDataJson(string types, out string parNum)
        {
            dynamic joData = new JObject();
            switch (types)
            {
                case "转院备案撤销":       //查询转院备案列表
                    {
                        parNum = "02030002";
                        break;
                    }
                case "慢特病备案撤销":        //查询慢特病备案列表
                    {
                        parNum = "02030005";
                        break;
                    }
                case "定点备案撤销":       //查询定点备案列表
                    {
                        parNum = "02030008";
                        break;
                    }
                default:
                    parNum = "";
                    break;
            }
            joData.ID = "";    //转诊记录ID
            joData.trtDclaDetlSn = "";   //流水号
            return joData;
        }


        #endregion

    }
}
