public IActionResult PutCancelExecute([FromForm] ExecutiveViewModel model)
        {
            var date = DateTime.Now;
            var executiveordersdata = _context.ExecutiveOrders.Find(model.id);

            {
                executiveordersdata.Cancel       = 1;
                executiveordersdata.Canceldetail = model.Canceldetail;
            };

            _context.Entry(executiveordersdata).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.SaveChanges();

            return(Ok(new { Id = model.id }));
        }
        public IActionResult PutgotitExecute([FromForm] ExecutiveViewModel model)
        {
            var date = DateTime.Now;
            var executiveordersdata = _context.ExecutiveOrders.Find(model.id);

            {
                executiveordersdata.Accept = 1;
            };
            _context.Entry(executiveordersdata).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.SaveChanges();
            System.Console.WriteLine("1 : ");
            var executiveorderanswerdata = _context.ExecutiveOrderAnswers.Find(model.ExecutiveOrderAnswerId);

            {
                executiveorderanswerdata.Status       = "รับทราบข้อสั่งการ";
                executiveorderanswerdata.beaware_date = date;
            };
            _context.Entry(executiveorderanswerdata).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.SaveChanges();
            System.Console.WriteLine("2 : ");
            return(Ok(new { Id = model.id }));
        }
        public async Task <IActionResult> PutanswerExecute([FromForm] ExecutiveViewModel model)
        {
            var date = DateTime.Now;

            var executiveorderanswerdata = _context.ExecutiveOrderAnswers.Find(model.ExecutiveOrderAnswerId);

            {
                executiveorderanswerdata.Status = "รายงานผลเรียบร้อย";
            };
            _context.Entry(executiveorderanswerdata).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.SaveChanges();

            var executiveorderanswerdetaildata = new ExecutiveOrderAnswerDetail
            {
                ExecutiveOrderAnswerId = model.ExecutiveOrderAnswerId,
                Answerdetail           = model.Answerdetail,
                AnswerProblem          = model.AnswerProblem,
                AnswerCounsel          = model.AnswerCounsel,
                create_at = date,
                publics   = 1,
            };

            System.Console.WriteLine("1 PutanswerExecute : ");

            _context.ExecutiveOrderAnswerDetails.Add(executiveorderanswerdetaildata);
            _context.SaveChanges();

            // <!-- อัพไฟล์  -->
            if (!Directory.Exists(_environment.WebRootPath + "//executivefile//"))
            {
                Directory.CreateDirectory(_environment.WebRootPath + "//executivefile//"); //สร้าง Folder Upload ใน wwwroot
            }
            var filePath = _environment.WebRootPath + "//executivefile//";

            if (model.files != null)
            {
                foreach (var formFile in model.files.Select((value, index) => new { Value = value, Index = index }))
                {
                    var    random    = RandomString(10);
                    string filePath2 = formFile.Value.FileName;
                    string filename  = Path.GetFileName(filePath2);
                    string ext       = Path.GetExtension(filename);

                    if (formFile.Value.Length > 0)
                    {
                        using (var stream = System.IO.File.Create(filePath + random + filename))
                        {
                            await formFile.Value.CopyToAsync(stream);
                        }
                        var AnswerExecutiveOrderFile = new AnswerExecutiveOrderFile
                        {
                            ExecutiveOrderAnswerDetailId = executiveorderanswerdetaildata.Id,
                            Name = random + filename,
                        };
                        _context.AnswerExecutiveOrderFiles.Add(AnswerExecutiveOrderFile);
                        _context.SaveChanges();
                    }
                }
            }
            // <!--END อัพไฟล์  -->

            return(Ok(new { Id = model.id }));
        }
        public async Task <IActionResult> Put([FromForm] ExecutiveViewModel model)
        {
            var date = DateTime.Now;

            //Begin ลบแจ้ง
            var deletenotifications = _context.Notifications.Where(m => m.xe == model.id && m.status == 10);

            _context.Notifications.RemoveRange(deletenotifications);
            _context.SaveChanges();
            // END ลบแจ้ง
            var executiveordersdata = _context.ExecutiveOrders.Find(model.id); {
                executiveordersdata.Subject        = model.Subject;
                executiveordersdata.Subjectdetail  = model.Subjectdetail;
                executiveordersdata.Commanded_date = model.Commanded_date;
                executiveordersdata.Draft          = model.Draft;
                executiveordersdata.CreatedAt      = date;
            };

            _context.Entry(executiveordersdata).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.SaveChanges();
            System.Console.WriteLine("1 : ");
            // <! -- ลบข้อมูลผู้รับข้อสั่งการเดิม -->
            var OLDExecutiveOrderAnswer = _context.ExecutiveOrderAnswers
                                          .Where(m => m.ExecutiveOrderId == executiveordersdata.Id);

            _context.ExecutiveOrderAnswers.RemoveRange(OLDExecutiveOrderAnswer);
            _context.SaveChanges();
            // <! -- END ลบข้อมูลผู้รับข้อสั่งการเดิม -->

            // <!-- แก้ไขผู่รับข้อสั่งการ  -->
            System.Console.WriteLine("3 : ");
            foreach (var item in model.Answer_by)
            {
                var data = new ExecutiveOrderAnswer
                {
                    ExecutiveOrderId = executiveordersdata.Id,
                    Status           = "แจ้งแล้ว",
                    UserID           = item,
                    publics          = 1,
                };
                System.Console.WriteLine("3.2 : ");
                _context.ExecutiveOrderAnswers.Add(data);
                System.Console.WriteLine("3.3 : ");
                _context.SaveChanges();
            }
            // <!-- END แก้ไขผู้รับข้อสั่งการ  -->
            System.Console.WriteLine("4 : ");
            if (!Directory.Exists(_environment.WebRootPath + "//executivefile//"))
            {
                Directory.CreateDirectory(_environment.WebRootPath + "//executivefile//");  //สร้าง Folder Upload ใน wwwroot
            }
            //var BaseUrl = url.ActionContext.HttpContext.Request.Scheme;
            // path ที่เก็บไฟล์
            var filePath = _environment.WebRootPath + "//executivefile//";

            if (model.files != null)
            {
                foreach (var formFile in model.files.Select((value, index) => new { Value = value, Index = index }))
                //foreach (var formFile in data.files)
                {
                    var    random    = RandomString(10);
                    string filePath2 = formFile.Value.FileName;
                    string filename  = Path.GetFileName(filePath2);
                    string ext       = Path.GetExtension(filename);

                    if (formFile.Value.Length > 0)
                    {
                        // using (var stream = System.IO.File.Create(filePath + formFile.Value.FileName))
                        using (var stream = System.IO.File.Create(filePath + random + filename))
                        {
                            await formFile.Value.CopyToAsync(stream);
                        }
                        var ExecutiveOrderFile = new ExecutiveOrderFile
                        {
                            ExecutiveOrderId = model.id,
                            Name             = random + filename
                        };
                        _context.ExecutiveFiles.Add(ExecutiveOrderFile);
                        _context.SaveChanges();
                        System.Console.WriteLine("4.2 : ");
                    }
                }
            }
            System.Console.WriteLine("5 : ");
            return(Ok(new { Id = model.id, title = executiveordersdata.Subject }));
        }
        public async Task <IActionResult> Post([FromForm] ExecutiveViewModel model)
        {
            var date = DateTime.Now;

            var executiveordersdata = new ExecutiveOrder {
                UserID         = model.Commanded_by,
                Subject        = model.Subject,
                Subjectdetail  = model.Subjectdetail,
                CreatedAt      = date,
                Commanded_date = model.Commanded_date,
                publics        = 1,
                Draft          = model.Draft,
                Accept         = 0,
                Cancel         = 0,
            };

            System.Console.WriteLine("2 : ");
            _context.ExecutiveOrders.Add(executiveordersdata);
            _context.SaveChanges();

            // <!-- เพิ่มผู่รับข้อสั่งการ  -->
            System.Console.WriteLine("3 : ");
            foreach (var item in model.Answer_by)
            {
                var data = new ExecutiveOrderAnswer
                {
                    ExecutiveOrderId = executiveordersdata.Id,
                    Status           = "แจ้งแล้ว",
                    UserID           = item,
                    publics          = 1,
                };
                _context.ExecutiveOrderAnswers.Add(data);
                _context.SaveChanges();
                System.Console.WriteLine("3.2 : ");
            }
            // <!-- END เพิ่มผู่รับข้อสั่งการ  -->

            System.Console.WriteLine("4 : ");
            // <!-- อัพไฟล์  -->
            if (!Directory.Exists(_environment.WebRootPath + "//executivefile//"))
            {
                Directory.CreateDirectory(_environment.WebRootPath + "//executivefile//");  //สร้าง Folder Upload ใน wwwroot
            }
            var filePath = _environment.WebRootPath + "//executivefile//";

            if (model.files != null)
            {
                foreach (var formFile in model.files.Select((value, index) => new { Value = value, Index = index }))
                {
                    var    random    = RandomString(10);
                    string filePath2 = formFile.Value.FileName;
                    string filename  = Path.GetFileName(filePath2);
                    string ext       = Path.GetExtension(filename);

                    if (formFile.Value.Length > 0)
                    {
                        using (var stream = System.IO.File.Create(filePath + random + filename))
                        {
                            await formFile.Value.CopyToAsync(stream);
                        }
                        var ExecutiveOrderFile = new ExecutiveOrderFile
                        {
                            ExecutiveOrderId = executiveordersdata.Id,
                            Name             = random + filename,
                        };
                        _context.ExecutiveFiles.Add(ExecutiveOrderFile);
                        _context.SaveChanges();
                    }
                }
            }
            // <!--END อัพไฟล์  -->
            System.Console.WriteLine("5 : ");
            //return Ok (new { Id = executiveordersdata.Id, Answer_by = executiveordersdata.Answer_by }); //เดียวมาใช้
            return(Ok(new { Id = executiveordersdata.Id, title = executiveordersdata.Subject }));
        }