// <CompileWithRoslyn />

namespace MercatorUi.MovableControls.ExpressionsCodes
{
    public static class Script
    {
        public static String Evaluate(MercatorUi.MovableControls.MovableExpression currentExpression)
        {
            // enter your customized code here

            MercatorUi.Forms.Billing.BillingForm billingForm = (MercatorUi.Forms.Billing.BillingForm)currentExpression.Form;
            MercatorUi.MovableControls.MovablePictureBox pictureBox = billingForm.MovableControls.Values.OfType<MercatorUi.MovableControls.MovablePictureBox>().FirstOrDefault();
            MercatorUi.MovableControls.MovablePictureBox pictureBox = (MercatorUi.MovableControls.MovablePictureBox)billingForm.MovableControls["AXJXLR1474"];
            if (pictureBox == null)
                return "PictureBox not found!";

            string id_article = billingForm.CurrentRowCellValue("id_article");
            if (!string.IsNullOrWhiteSpace(id_article) && (pictureBox.TagString != id_article))
            {
                object ox = MercatorController.xFunctions.xLookUp("STOCK", "S_ID", id_article, "S_IMGBIN");
                if ((ox is byte[] bb) && (bb.Length > 0))
                {
                    pictureBox.Image = Api.BytesToImage(bb);
                    if ((pictureBox.Image.Width <= pictureBox.Width) && (pictureBox.Image.Height <= pictureBox.Height))
                        pictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
                    else
                        pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
                }
                else
                {
                    pictureBox.Image = null;
                }
                pictureBox.TagString = id_article;
            }
            return "Photo article = " + id_article;
        }
    }
}