<template>
<div>
	<span align="center"><h4>Supplier Details</h4></span><br>
		<el-table :data="tableData" border style="width: 100%">
			<el-table-column prop="company_name" label="Company Name"></el-table-column>
			<el-table-column prop="supplier_reg_no" label="Registration No"></el-table-column>
			<el-table-column prop="gst_no" label="GST No"></el-table-column>
			<el-table-column prop="bank_name" label="Bank Name"></el-table-column>
			<el-table-column prop="acc_no" label="Account No"></el-table-column>
			<el-table-column label="Action">
				<template slot-scope="scope">
					<el-button type="text" size="small" @click="PopOutEditSupplier(scope.$index)">Edit</el-button>
					<el-button type="text" size="small" @click="DeleteSupplier(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="PopOutAddSupplier">Add New Supplier</el-button>
		</div>
		<!-- popup add new supplier -->
			<el-dialog title="Add New Supplier" align="center" :visible.sync="AddNewSupplierdialogVisible" width="40%">
				<el-form  label-width="150px" >
					<el-form-item label="Company Name">
						<el-input v-model="supplierForm.company_name" v-validate="'required|max:191'" name="company_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Registration No">
						<el-input v-model="supplierForm.supplier_reg_no" v-validate="'required|max:191'" name="supplier_reg_no"></el-input>
						
					</el-form-item>
					<el-form-item label="GST No">
						<el-input v-model="supplierForm.gst_no" v-validate="'required|max:191'" name="gst_no"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Name">
						<el-input v-model="supplierForm.bank_name" v-validate="'required|max:191'" name="bank_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Account No">
						<el-input v-model="supplierForm.acc_no" v-validate="'required|max:191'" name="acc_no"></el-input>
						
					</el-form-item>
				</el-form>
				<div align="center">
						<el-button @click="AddNewSupplierdialogVisible = false">Cancel</el-button>
						<el-button type="primary" @click="CreateSupplier" :disabled="supplierForm.company_name=='' || supplierForm.supplier_reg_no=='' || supplierForm.gst_no=='' || supplierForm.bank_name=='' || supplierForm.acc_no =='' ? true : false">OK</el-button>
				</div>
			</el-dialog>
			<el-dialog align="center" :visible.sync="DoneAddSupplierdialogVisible" width="30%">
				<span>Supplier Added Successfully</span><br><br>
				<div align="center">
						<el-button type="primary" @click="DoneAddSupplierdialogVisible = false;">OK</el-button>
				</div>
			</el-dialog>
		<!-- popup add new supplier end -->
		<!-- popup edit supplier -->
			<el-dialog title="Edit Supplier" align="center" :visible.sync="EditSupplierdialogVisible" width="40%">
					<el-form  label-width="150px" >
					<el-form-item label="Company Name">
						<el-input v-model="supplierForm.company_name" v-validate="'required|max:191'" name="company_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Registration No">
						<el-input v-model="supplierForm.supplier_reg_no" v-validate="'required|max:191'" name="supplier_reg_no"></el-input>
						
					</el-form-item>
					<el-form-item label="GST No">
						<el-input v-model="supplierForm.gst_no" v-validate="'required|max:191'" name="gst_no"></el-input>
						
					</el-form-item>
					<el-form-item label="Bank Name">
						<el-input v-model="supplierForm.bank_name" v-validate="'required|max:191'" name="bank_name"></el-input>
						
					</el-form-item>
					<el-form-item label="Account No">
						<el-input v-model="supplierForm.acc_no" v-validate="'required|max:191'" name="acc_no"></el-input>
						
					</el-form-item>
				</el-form>
				<div align="center">
						<el-button @click="EditSupplierdialogVisible = false">Cancel</el-button>
						<el-button type="primary" @click="EditSupplier" :disabled="supplierForm.company_name=='' || supplierForm.supplier_reg_no=='' || supplierForm.gst_no=='' || supplierForm.bank_name=='' || supplierForm.acc_no =='' ? true : false">OK</el-button>
				</div>
			</el-dialog>
			<el-dialog align="center" :visible.sync="DoneUpdateSupplierdialogVisible" width="30%">
				<span>Supplier's Details Updated Successfully</span><br><br>
				<div align="center">
						<el-button type="primary" @click="DoneUpdateSupplierdialogVisible = false" >OK</el-button>
				</div>
			</el-dialog>
		<!-- popup edit supplier end -->
		<!-- popup delete supplier -->
			<el-dialog align="center" :visible.sync="DeleteSupplierdialogVisible" width="30%">
				<span>Supplier's Details Deleted</span><br><br>
				<div align="center">
						<el-button type="primary" @click="DeleteSupplierdialogVisible = false">OK</el-button>
				</div>
			</el-dialog>
		<!-- popup delete supplier end -->
</div>
</template>

<script>
	import axios from 'axios'

	export default {
		data() {
			return {
				tableData: [],
				supplierForm : {
					company_name: '',
					supplier_reg_no: '',
					gst_no: '',
					bank_name: '', 
					acc_no: '', 
				},
				AddNewSupplierdialogVisible: false,
				EditSupplierdialogVisible: false,
				DoneAddSupplierdialogVisible: false,
				DoneUpdateSupplierdialogVisible: false,
				DeleteSupplierdialogVisible: false,
				currentEdit: -1,
			}
		},
		beforeMount(){
			this.UpdateTableData();
		},
		methods:{
			CreateSupplier : function(){
				this.AddNewSupplierdialogVisible = false; 
				axios.post('/api/setting-supplier', this.supplierForm)
				.then((response) => {
					this.DoneAddSupplierdialogVisible = true;
					this.UpdateTableData();
				})
				.catch((error) => {
					console.log(error);
					this.$message({
						showClose: true,
						message: 'Create Fail',
						type: 'error',
						duration: 10000
					});
				});
			},
			UpdateTableData : function(){
				axios.get('/api/setting-supplier', this.supplierForm)
					.then((response) => {
						console.log(response.data)
						this.tableData = response.data;
					})
					.catch((error) => {
						console.log(error);
						this.$message({
							showClose: true,
							message: 'Fetch data fail',
							type: 'error',
							duration: 10000
						});
					})
			},
			DeleteSupplier : function(supplier_id){
				this.DeleteSupplierdialogVisible = true;
				axios.delete('/api/setting-supplier/' + supplier_id)
					.then((response) => {
						this.UpdateTableData();
					})
					.catch((error) => {
						console.log(error);
						this.$message({
							showClose: true,
							message: 'Delete data fail',
							type: 'error',
							duration: 10000
						});
					})
			},
			EditSupplier : function(){
				this.EditSupplierdialogVisible = false; 
				this.DoneUpdateSupplierdialogVisible = true;

				axios.put('/api/setting-supplier/' + this.currentEdit, this.supplierForm)
				.then((response) => {
					this.UpdateTableData();
				})
				.catch((error) => {
					console.log(error);
					this.$message({
						showClose: true,
						message: 'Update Fail',
						type: 'error',
						duration: 10000
					});
				})
			},
			PopOutAddSupplier : function(){
				this.AddNewSupplierdialogVisible = true;
				this.ClearSupplierForm();
			},
			PopOutEditSupplier : function(tableDataRow){
				this.EditSupplierdialogVisible = true;
				this.FillupSupplierForm(tableDataRow);
				this.currentEdit = this.tableData[tableDataRow].id;
			},
			ClearSupplierForm : function(){
				this.supplierForm.company_name = '';
				this.supplierForm.supplier_reg_no = '';
				this.supplierForm.gst_no = '';
				this.supplierForm.bank_name = '';
				this.supplierForm.acc_no = '';
			},
			FillupSupplierForm : function(tableDataRow){
				this.supplierForm.company_name = this.tableData[tableDataRow].company_name;
				this.supplierForm.supplier_reg_no = this.tableData[tableDataRow].supplier_reg_no;
				this.supplierForm.gst_no = this.tableData[tableDataRow].gst_no;
				this.supplierForm.bank_name = this.tableData[tableDataRow].bank_name;
				this.supplierForm.acc_no = this.tableData[tableDataRow].acc_no;
			}
		}
	}
</script>
<style type="text/css">
	.error-message{
		position: absolute;
		left: 0;
	}
</style>
