﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using LJH.Inventory.BusinessModel;

namespace LJH.Inventory.UI.Forms.Inventory
{
    public partial class Frm设置其它成本 : Form
    {
        public Frm设置其它成本()
        {
            InitializeComponent();
        }

        public List<CostItem> Costs { get; set; }

        private void FrmChangeCosts_Load(object sender, EventArgs e)
        {
            if (Costs != null && Costs.Count > 0)
            {
                foreach (var ci in Costs)
                {
                    if (ci.CostFlag  == CostFlag.运费)
                    {
                        txtTransCost.DecimalValue = ci != null ? ci.Amount  : 0;
                        rdWithTax_运费.Checked = ci != null && ci.TaxRate >0;
                        rdWithoutTax__运费.Checked = !rdWithTax_运费.Checked;
                    }
                    else if (ci.CostFlag  == CostFlag.其它费用)
                    {
                        txtOtherCost.DecimalValue = ci != null ? ci.Amount  : 0;
                        rdWithTax_其它费用.Checked = ci != null && ci.TaxRate >0;
                        rdWithoutTax__其它费用.Checked = !rdWithTax_其它费用.Checked;
                    }
                }
            }
        }

        private void chk运费_CheckedChanged(object sender, EventArgs e)
        {
            txtTransCost.Enabled = chk运费.Checked;
            pnlTrans.Enabled = chk运费.Checked;
        }

        private void chk其它费用_CheckedChanged(object sender, EventArgs e)
        {
            txtOtherCost.Enabled = chk其它费用.Checked;
            pnlOther.Enabled = chk其它费用.Checked;
        }

        private void btnOk1_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) return;
            Costs = new List<CostItem>();
            if (chk运费.Checked) Costs.Add(new CostItem() { CostFlag = CostFlag.运费, Amount = txtTransCost.DecimalValue, TaxRate = rdWithTax_运费.Checked ? UserSettings.Current.发票成本系数 : 0 });
            if (chk其它费用.Checked) Costs.Add(new CostItem() { CostFlag = CostFlag.其它费用, Amount = txtOtherCost.DecimalValue, TaxRate = rdWithTax_其它费用.Checked ? UserSettings.Current.发票成本系数 : 0 });
            if (Costs.Count == 0)
            {
                MessageBox.Show("请至少选择修改其中一种成本");
                return;
            }
            if (MessageBox.Show("修改入库价格会同时修改供应商的应收，是否继续?", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private bool CheckInput()
        {
            if (chk运费.Checked && txtTransCost.DecimalValue > 0 && !rdWithTax_运费.Checked && !rdWithoutTax__运费.Checked)
            {
                MessageBox.Show("没有指定运费是否含税");
                return false;
            }
            if (chk其它费用.Checked && txtOtherCost.DecimalValue > 0 && !rdWithTax_其它费用.Checked && !rdWithoutTax__其它费用.Checked)
            {
                MessageBox.Show("没有指定其它费用是否含税");
                return false;
            }
            return true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
