<template>
<div>
	<span align="center"><h4>CIEF China 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="115px"></el-table-column>
			<el-table-column prop="bank_address" label="Bank Address" width="111px"></el-table-column>
			<el-table-column prop="swift" label="SWIFT Code"></el-table-column>
			<el-table-column prop="cnap" label="CNAP" width="80px"></el-table-column>
			<el-table-column prop="bank_branch" label="Bank Branch"></el-table-column>
			<el-table-column label="Action">
				<template slot-scope="scope">
					<el-button type="text" size="small" @click="PopOutEditBeneficiaries(scope.$index)">Edit</el-button>
					<el-button type="text" size="small" @click="DeleteBeneficiaries(tableData[scope.$index].id)">Delete</el-button>
				</template>
			</el-table-column>
		</el-table><br>
		<div align="right">
			<el-button type="primary" icon="el-icon-plus" @click="PopOutAddBeneficiaries">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="beneficiariesForm.company_name" name="company_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Name">
						<el-input v-model="beneficiariesForm.bank_name" name="bank_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Account No">
						<el-input v-model="beneficiariesForm.acc_no" name="acc_no"></el-input>
					
					</el-form-item>
					<el-form-item label="Bank Address">
						<el-input v-model="beneficiariesForm.bank_address" name="bank_address"></el-input>
					
					</el-form-item>
					<el-form-item label="SWIFT Code">
						<el-input v-model="beneficiariesForm.swift" name="swift"></el-input>
					
					</el-form-item>
					<el-form-item label="CNAP">
						<el-input v-model="beneficiariesForm.cnap" name="cnap" ></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Branch">
						<el-input v-model="beneficiariesForm.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="CreateBeneficiaries" :disabled="beneficiariesForm.company_name=='' || beneficiariesForm.bank_name=='' || beneficiariesForm.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="beneficiariesForm.company_name" name="company_name"></el-input>	
					</el-form-item>
					<el-form-item label="Bank Name">
						<el-input v-model="beneficiariesForm.bank_name" name="bank_name"></el-input>
					</el-form-item>
					<el-form-item label="Bank Account No">
						<el-input v-model="beneficiariesForm.acc_no" name="acc_no"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Address">
						<el-input v-model="beneficiariesForm.bank_address" name="bank_address"></el-input>
						
					</el-form-item>
					<el-form-item label="SWIFT Code">
						<el-input v-model="beneficiariesForm.swift" name="swift"></el-input>
						
					</el-form-item>
					<el-form-item label="CNAP">
						<el-input v-model="beneficiariesForm.cnap" name="cnap" ></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Branch">
						<el-input v-model="beneficiariesForm.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="EditBeneficiaries" :disabled="beneficiariesForm.company_name=='' || beneficiariesForm.bank_name=='' || beneficiariesForm.acc_no=='' ? true : false">Submit</el-button>
				</div>
				<span slot="footer" class="dialog-footer">
					* Please key in the data in Chinese
				</span>
			</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: [],
				beneficiariesForm : {
					company_name : '',
					bank_name : '',
					acc_no : '',
					bank_address : '',
					swift : '',
					cnap : '',
					bank_branch : '',
				},
				EditDatadialogVisible: false,
				DeleteAdddialogVisible: false,
				AddDatadialogVisible: false,
				DoneAddDatadialogVisible: false,
				DoneUpdateDatadialogVisible: false,
				currentEdit : -1,
			}
		},
		beforeMount(){
			this.UpdateTableData();
		},
		methods: {
			CreateBeneficiaries : function(){
				this.loading_btn = true
				this.AddDatadialogVisible = false;
				axios.post('/api/setting-beneficiary', this.beneficiariesForm)
					.then((response) => {
						this.loading_btn = false
						this.DoneAddDatadialogVisible = true;
						this.ClearBeneficiariesForm();
						this.UpdateTableData();
					})
					.catch((error) => {
						this.loading_btn = false
						console.log(error);
						this.$message({
							showClose: true,
							message: 'Create Fail',
							type: 'error',
							duration: 10000
						});
						this.ClearBeneficiariesForm();
					});
			},
			UpdateTableData : function(){
				this.loading_btn = true
				axios.get('/api/setting-beneficiary', this.beneficiariesForm)
					.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
						});
					})
			},
			DeleteBeneficiaries : function(beneficiaries_id){
				this.loading_btn = true
				this.DeleteAdddialogVisible = true;
				axios.delete('/api/setting-beneficiary/' + beneficiaries_id)
					.then((response) => {
						this.loading_btn = false
						this.UpdateTableData();
						this.$message({
						showClose: true,
						message: 'Data Deleted Successfully',
						type: 'success',
						duration: 5000
					});
					})
					.catch((error) => {
						this.loading_btn = false
						console.log(error);
						this.$message({
							showClose: true,
							message: 'Delete data fail',
							type: 'error',
							duration: 10000
						});
					});
			},
			EditBeneficiaries : function(){
				this.loading_btn = true
				this.EditDatadialogVisible = false; 
				this.DoneUpdateDatadialogVisible = true;

				axios.put('/api/setting-beneficiary/' + this.currentEdit, this.beneficiariesForm)
					.then((response) => {
						this.loading_btn = false
						this.UpdateTableData();
						this.$message({
						showClose: true,
						message: 'Data Updated Succesfully',
						type: 'success',
						duration: 5000
					});
				
					})
					.catch((error) => {
						this.loading_btn = false
						console.log(error);
						this.$message({
							showClose: true,
							message: 'Update Fail',
							type: 'error',
							duration: 10000
						});
					});
			},
			PopOutAddBeneficiaries : function(){
				this.AddDatadialogVisible = true;
				this.ClearBeneficiariesForm();
			},
			PopOutEditBeneficiaries : function(tableDataRow){
				this.EditDatadialogVisible = true;
				this.FillupBeneficiariesForm(tableDataRow);
				this.currentEdit = this.tableData[tableDataRow].id;
			},
			ClearBeneficiariesForm : function(){
				this.beneficiariesForm.company_name = '';
				this.beneficiariesForm.bank_name = '';
				this.beneficiariesForm.acc_no = '';
				this.beneficiariesForm.bank_address = '';
				this.beneficiariesForm.swift = '';
				this.beneficiariesForm.cnap = '';
				this.beneficiariesForm.bank_branch = '';
			},
			FillupBeneficiariesForm : function(tableDataRow){
				this.beneficiariesForm.company_name = this.tableData[tableDataRow].company_name;
				this.beneficiariesForm.bank_name = this.tableData[tableDataRow].bank_name;
				this.beneficiariesForm.acc_no = this.tableData[tableDataRow].acc_no;
				this.beneficiariesForm.bank_address = this.tableData[tableDataRow].bank_address;
				this.beneficiariesForm.swift = this.tableData[tableDataRow].swift;
				this.beneficiariesForm.cnap = this.tableData[tableDataRow].cnap;
				this.beneficiariesForm.bank_branch = this.tableData[tableDataRow].bank_branch;
			}
		}
	}
</script>
<style type="text/css">
	.error-message{
		position: absolute;
		left: 0;
	}
</style>

