using DeviceRepair.Models;
using DeviceRepair.Models.Device;
using DeviceRepairAndOptimization.Biz;
using DeviceRepairAndOptimization.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization.Pages.Maintenance
{
    public partial class page_DriveTypeTree : FormBase
    {
        private List<DeviceInformationInfoTree> RawData;

        private string FilterString
        {
            get
            {
                return EditSearch.Text.Trim();
            }
        }


        public page_DriveTypeTree()
        {
            InitializeComponent();

            this.Load += Page_DriveTypeTree_Load;
        }

        private void Page_DriveTypeTree_Load(object sender, EventArgs e)
        {
            GridDataBind();
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            GridDataBind();
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {
        }


        void GridDataBind()
        {
            try
            {
                splashScreenManager1.ShowWaitForm();
                APIResponseData apiResponseData = DeviceManager.Instance.GetDeviceTreeDatas(FilterString);
                if (apiResponseData.Code != 1)
                    throw new Exception(apiResponseData.Message);

                RawData = apiResponseData.ToDeserializeObject<List<DeviceInformationInfoTree>>();
                tvDevices.DataSource = RawData;
                tvDevices.KeyFieldName = "RouteAutoId";
                tvDevices.ParentFieldName = "ParentRouteId";
                tvDevices.ExpandAll();
                tvDevices.BestFitColumns();
                splashScreenManager1.TryCloseWait();
            }
            catch (Exception ex)
            {
                splashScreenManager1.TryCloseWait();
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }
    }
}
