<template>
<div>
	<span align="center"><h4>Malaysia Bank Details</h4></span><br>
		<el-table :data="tableData" border style="width: 100%">
			<el-table-column prop="company_name" label="Company Name" width="90px"></el-table-column>
			<el-table-column prop="bank_name" label="Bank Name"></el-table-column>
			<el-table-column prop="acc_no" label="Bank Account No" width="125px"></el-table-column>
			<el-table-column prop="bank_address" label="Bank Address" width="121px"></el-table-column>
			<el-table-column prop="swift" label="SWIFT Code" width="101px"></el-table-column>
			<el-table-column prop="cnap" label="CNAP" width="80px"></el-table-column>
			<el-table-column prop="bank_branch" label="Bank Branch" width="111px"></el-table-column>
			<el-table-column label="Action">
				<template slot-scope="scope">
					<div id="mobile-toggle" v-bind:class="{ active: showMobileMenu }" v-on:click="showMobileMenu = !showMobileMenu"><toggle-button :value="scope.row.active === 1 ? true : false"
               color="#82C7EB"
               :sync="true"
			   @change="UpdateBankStatus(scope.row.id)"
               :labels="{checked: 'Active', unchecked: 'Inactive'}" :width="70"/></div>
					<el-button type="text" size="small" :loading="loading_btn" @click="PopOutEditMalaysiaBank(scope.$index)">Edit Information</el-button>
				</template>
			</el-table-column>
		</el-table><br>
		<div align="right">
			<el-button type="primary" icon="el-icon-plus" @click="PopOutAddMalaysiaBank">Add New Data</el-button>
		</div>
		<!-- popup add new data -->
			<el-dialog title="Add New Data" align="center" :visible.sync="AddDatadialogVisible" width="40%">
				<el-form label-width="150px">
					<el-form-item label="Company Name">
						<el-input v-model="malaysiaBankForm.company_name" name="company_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Name">
						<el-input v-model="malaysiaBankForm.bank_name" name="bank_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Account No">
						<el-input v-model="malaysiaBankForm.acc_no" name="acc_no"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Address">
						<el-input v-model="malaysiaBankForm.bank_address" name="bank_address"></el-input>
						
					</el-form-item>
					<el-form-item label="SWIFT Code">
						<el-input v-model="malaysiaBankForm.swift" name="swift"></el-input>
						
					</el-form-item>
					<el-form-item label="CNAP">
						<el-input v-model="malaysiaBankForm.cnap" name="cnap" ></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Branch">
						<el-input v-model="malaysiaBankForm.bank_branch" name="bank_branch"></el-input>
						
					</el-form-item>
				</el-form>
				<div align="center">
					<el-button @click="AddDatadialogVisible = false">Cancel</el-button>
					<el-button type="primary" :loading="loading_btn" @click="CreateMalaysiaBank" :disabled="malaysiaBankForm.company_name=='' || malaysiaBankForm.bank_name=='' || malaysiaBankForm.acc_no=='' ? true : false">OK</el-button>
				</div>
			</el-dialog>
			<el-dialog align="center" :visible.sync="DoneAddDatadialogVisible" width="30%">
				<span>Data Added Successfully</span><br><br>
				<div align="center">
						<el-button type="primary" :loading="loading_btn" @click="DoneAddDatadialogVisible = false">OK</el-button>
				</div>
			</el-dialog>
		<!-- popup add new data end -->
		<!-- popup edit data -->
			<el-dialog title="Edit Data" align="center" :visible.sync="EditDatadialogVisible" width="40%">
				<el-form label-width="150px">
					<el-form-item label="Company Name">
						<el-input v-model="malaysiaBankForm.company_name" name="company_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Name">
						<el-input v-model="malaysiaBankForm.bank_name" name="bank_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Account No">
						<el-input v-model="malaysiaBankForm.acc_no" name="acc_no"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Address">
						<el-input v-model="malaysiaBankForm.bank_address" name="bank_address"></el-input>
						
					</el-form-item>
					<el-form-item label="SWIFT Code">
						<el-input v-model="malaysiaBankForm.swift" name="swift"></el-input>
						
					</el-form-item>
					<el-form-item label="CNAP">
						<el-input v-model="malaysiaBankForm.cnap" name="cnap" ></el-input>
					
					</el-form-item>
					<el-form-item label="Bank Branch">
						<el-input v-model="malaysiaBankForm.bank_branch" name="bank_branch"></el-input>
						
					</el-form-item>
				</el-form>
				<div align="center">
					<el-button @click="EditDatadialogVisible = false">Cancel</el-button>
					<el-button type="primary" :loading="loading_btn" @click="EditMalaysiaBank" :disabled="malaysiaBankForm.company_name=='' || malaysiaBankForm.bank_name=='' || malaysiaBankForm.acc_no=='' ? true : false">Submit</el-button>
				</div>
			</el-dialog>
			<el-dialog align="center" :visible.sync="DoneUpdateDatadialogVisible" width="30%">
				<span>Data Updated Successfully</span><br><br>
				<div align="center">
					<el-button type="primary" :loading="loading_btn" @click="DoneUpdateDatadialogVisible = false">OK</el-button>
				</div>
			</el-dialog>
		<!-- popup edit data end -->
		<!-- popup delete -->
			<el-dialog align="center" :visible.sync="DeleteAdddialogVisible" width="30%">
				<span>Data Deleted</span><br><br>
				<div align="center">
					<el-button type="primary" :loading="loading_btn" @click="DeleteAdddialogVisible = false">OK</el-button>
				</div>
			</el-dialog>
		<!-- popup delete end -->
</div>
</template>

<script>
	import axios from 'axios'

	export default {
		data() {
			return {
				loading_btn: false,
				tableData: [],
				malaysiaBankForm : {
					company_name : '',
					bank_name : '',
					acc_no : '',
					bank_address : '',
					swift : '',
					cnap : '',
					bank_branch : '',
					
				},
				EditDatadialogVisible: false,
				AddDatadialogVisible: false,
				DoneAddDatadialogVisible: false,
				DoneUpdateDatadialogVisible: false,
				currentEdit : -1,
				
			}   
		},
	    mounted(){
	    	this.UpdateTableData();
	    },
		methods: {
			CreateMalaysiaBank : function(){
				this.loading_btn = true
	            this.AddDatadialogVisible = false;
				axios.post('/api/setting-malaysia-bank', this.malaysiaBankForm)
					.then((response) => {
						this.loading_btn = false
						this.DoneAddDatadialogVisible = true;
						this.ClearMalaysiaBankForm();
						this.UpdateTableData();
						this.$message({
                    showClose: true,
                    message: 'Success',
                    type: 'success',
                    duration: 5000
                });
            })
					.catch((error) => {
						this.loading_btn = false
						console.log(error);
						this.$message({
							showClose: true,
							message: 'Create Fail',
							type: 'error',
							duration: 10000
						});
						this.ClearMalaysiaBankForm();
					});
			},
			UpdateBankStatus : function(id){
				this.loading_btn = true
				axios.put('/api/setting-malaysia-bank/status/'+ id)
					.then((response) => {
						this.loading_btn = false;
						this.tableData = response.data;
					})
					.catch((error) => {
						this.loading_btn = false
						console.log(error);
						this.$message({
							showClose: true,
							message: 'Update Status fail',
							type: 'error',
							duration: 10000
						});
					})
			},
			UpdateTableData : function(){
				this.loading_btn = true
				axios.get('/api/setting-malaysia-bank', this.malaysiaBankForm)
					.then((response) => {
						this.loading_btn = false;
						this.tableData = response.data;
						
            
					})
					.catch((error) => {
						this.loading_btn = false
						console.log(error);
						this.$message({
							showClose: true,
							message: 'Fetch data fail',
							type: 'error',
							duration: 10000
						});
					})
			},
			EditMalaysiaBank : function(){
				this.loading_btn = true;
				this.EditDatadialogVisible = false;
				this.DoneUpdateDatadialogVisible = true;
				axios.put('/api/setting-malaysia-bank/' + this.currentEdit, this.malaysiaBankForm)
					.then((response) => {
						this.loading_btn = false;
						this.UpdateTableData();
						this.$message({
                    showClose: true,
                    message: 'Updated Successfully',
                    type: 'success',
                    duration: 5000
                });
            
					})
					.catch((error) => {
						console.log(error);
						this.$message({
							showClose: true,
							message: 'Update Fail',
							type: 'error',
							duration: 10000
						});
					});
			},
			PopOutAddMalaysiaBank : function(){
				this.AddDatadialogVisible = true;
				this.ClearMalaysiaBankForm();
			},
			PopOutEditMalaysiaBank : function(tableDataRow){
				this.EditDatadialogVisible = true;
				this.FillupMalaysiaBankForm(tableDataRow);
				this.currentEdit = this.tableData[tableDataRow].id;
			},
			ClearMalaysiaBankForm : function(){
				this.malaysiaBankForm.company_name = '';
				this.malaysiaBankForm.bank_name = '';
				this.malaysiaBankForm.acc_no = '';
				this.malaysiaBankForm.bank_address = '';
				this.malaysiaBankForm.swift = '';
				this.malaysiaBankForm.cnap = '';
				this.malaysiaBankForm.bank_branch = '';
			},
			FillupMalaysiaBankForm : function(tableDataRow){
				this.malaysiaBankForm.company_name = this.tableData[tableDataRow].company_name;
				this.malaysiaBankForm.bank_name = this.tableData[tableDataRow].bank_name;
				this.malaysiaBankForm.acc_no = this.tableData[tableDataRow].acc_no;
				this.malaysiaBankForm.bank_address = this.tableData[tableDataRow].bank_address;
				this.malaysiaBankForm.swift = this.tableData[tableDataRow].swift;
				this.malaysiaBankForm.cnap = this.tableData[tableDataRow].cnap;
				this.malaysiaBankForm.bank_branch = this.tableData[tableDataRow].bank_branch;
			}
		}
	}
</script>
<style type="text/css">
	.error-message{
		position: absolute;
		left: 0;
	}
</style>

