using ButcherFactory.BO;
using ButcherFactory.BO.Utils;
using ButcherFactory.Controls;
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 ButcherFactory.SegmentProductionAuto_
{
    public partial class WeightView : Form
    {
        BindingList<WeightDetail> list;
        ClientGoodsSet_Detail goods;

        private SegmentProductionAutoForm form;

        private ButtonTag bTag;
        public WeightView()
        {
            InitializeComponent();
            taskGrid.RowPrePaint += uDataGridView1_RowPrePaint;
            taskGrid.CellPainting += uDataGridView1_CellPainting;

        }

        public WeightView(SegmentProductionAutoForm form, ButtonTag bTag)
            : this()
        {

            this.bTag = bTag;
            this.form = form;
            goods = ((ClientGoodsSet_Detail)bTag.Button.Tag);

            label1.Text = goods.Goods_Name;
            list = new BindingList<WeightDetail>();

            var l = new List<decimal>(0);
            if (form.wDic.ContainsKey(goods.Goods_ID))
            {
                l = form.wDic[goods.Goods_ID];
            }
            int i = 1;
            foreach (var item in l)
            {
                list.Add(new WeightDetail { No = i, Weight = item });
                i++;
            }
        }

        private void uDataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            //if (e.RowIndex == taskGrid.Rows.Count - 1)
            //{
            //    var row = taskGrid.Rows[e.RowIndex];
            //    row.DefaultCellStyle.SelectionForeColor = Color.Black;
            //    row.DefaultCellStyle.BackColor = Color.Khaki;
            //    row.DefaultCellStyle.SelectionBackColor = Color.Khaki;
            //}
        }

        private void uDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            var last = taskGrid.Rows.Count - 1;
            if (e.RowIndex == last)
            {
                if (e.ColumnIndex == 0)
                {

                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);


            taskGrid.DataSource = list;
            taskGrid.Refresh();
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void taskGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            var row = taskGrid.SelectedRows;
            if (row.Count == 0)
                return;
            if (MessageBox.Show("确定删除选中记录？", "删除确认", MessageBoxButtons.OKCancel) != DialogResult.OK)
                return;
            var tag = taskGrid.SelectedRows[0].DataBoundItem as WeightDetail;
            if (form.wDic.ContainsKey(goods.Goods_ID))
            {
                var l = form.wDic[goods.Goods_ID];
                l.RemoveAt(tag.No - 1);
                bTag.Label.MinCount = l.Count;
                form.mCache.Cache =  form.wDic;
                XmlUtil.SerializerObjToFile( form.mCache);
            }
            list.Remove(tag);
            int i = 1;
            foreach (var item in list)
            {
                item.No = i;
                i++;
            }
            taskGrid.DataSource = list;
            taskGrid.ClearSelection();

            taskGrid.Refresh();
        }
    }
}
