<template>
	<div class="credit-customer-approval-page" v-loading="loading">
		<el-card shadow="never" class="approval-card">
			<template #header>
				<div class="card-header">挂账人信息</div>
			</template>
			<el-form :model="inputForm" ref="inputForm" :class="isReadOnly ? 'readonly' : ''" :disabled="isReadOnly"
				label-width="130px" @submit.native.prevent>
				<div class="form-section-title">基本信息</div>
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="姓名" prop="customerName"
							:rules="[{ required: true, message: '姓名不能为空', trigger: 'blur' }]">
							<el-input v-model="inputForm.customerName" maxlength="50" placeholder="请输入姓名"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="手机号" prop="mobile"
							:rules="[{ required: true, message: '手机号不能为空', trigger: 'blur' }]">
							<el-input v-model="inputForm.mobile" maxlength="30" placeholder="请输入手机号"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="挂账状态" prop="creditStatus">
							<el-select v-model="inputForm.creditStatus" placeholder="请选择挂账状态" clearable style="width: 100%">
								<el-option v-for="item in $dictUtils.getDictList('credit_status')" :key="item.value"
									:label="item.label" :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="生日" prop="birthday">
							<el-date-picker v-model="inputForm.birthday" type="date" value-format="YYYY-MM-DD"
								placeholder="请选择生日" style="width: 100%"></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="身份证号" prop="idCard">
							<el-input v-model="inputForm.idCard" maxlength="30" placeholder="请输入身份证号"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="年龄" prop="age">
							<el-input-number v-model="inputForm.age" :min="0" :max="150" :precision="0"
								style="width: 100%"></el-input-number>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="职业" prop="occupation">
							<el-input v-model="inputForm.occupation" maxlength="100" placeholder="请输入职业"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="公司名称" prop="companyName">
							<el-input v-model="inputForm.companyName" maxlength="200" placeholder="请输入公司名称"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="紧急联系人" prop="emergencyContact">
							<el-input v-model="inputForm.emergencyContact" maxlength="50" placeholder="请输入紧急联系人"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="紧急联系人电话" prop="emergencyContactPhone">
							<el-input v-model="inputForm.emergencyContactPhone" maxlength="30" placeholder="请输入紧急联系人电话"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="16">
						<el-form-item label="住址" prop="address">
							<el-input v-model="inputForm.address" maxlength="500" placeholder="请输入住址"></el-input>
						</el-form-item>
					</el-col>
				</el-row>

				<div class="form-section-title">开票信息</div>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="抬头" prop="invoiceTitle">
							<el-input v-model="inputForm.invoiceTitle" maxlength="200" placeholder="请输入开票抬头"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="税号" prop="taxNo">
							<el-input v-model="inputForm.taxNo" maxlength="100" placeholder="请输入税号"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="开户行" prop="bankName">
							<el-input v-model="inputForm.bankName" maxlength="200" placeholder="请输入开户行"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="账号" prop="bankAccount">
							<el-input v-model="inputForm.bankAccount" maxlength="100" placeholder="请输入账号"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="24">
						<el-form-item label="开票地址电话" prop="invoiceAddressPhone">
							<el-input v-model="inputForm.invoiceAddressPhone" maxlength="500" placeholder="请输入开票地址电话"></el-input>
						</el-form-item>
					</el-col>
				</el-row>

				<div class="form-section-title">挂账信息</div>
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="累计欠款金额" prop="totalArrearsAmount">
							<el-input-number v-model="inputForm.totalArrearsAmount" :min="0" :precision="2"
								style="width: 100%"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="未结账单数" prop="unsettledBillCount">
							<el-input-number v-model="inputForm.unsettledBillCount" :min="0" :precision="0"
								style="width: 100%"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="首次挂账时间" prop="firstCreditTime">
							<el-date-picker v-model="inputForm.firstCreditTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
								placeholder="请选择首次挂账时间" style="width: 100%"></el-date-picker>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="最后消费时间" prop="lastConsumeTime">
							<el-date-picker v-model="inputForm.lastConsumeTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
								placeholder="请选择最后消费时间" style="width: 100%"></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="录入人" prop="createName">
							<el-input v-model="inputForm.createName" disabled placeholder="保存后自动记录"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="审核人" prop="auditorName">
							<el-input v-model="inputForm.auditorName" disabled placeholder="暂未审核"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="24">
						<el-form-item label="备注" prop="remarks">
							<el-input v-model="inputForm.remarks" type="textarea" :rows="4" maxlength="500"
								placeholder="请输入备注" show-word-limit></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
	</div>
</template>

<script>
import CreditCustomerService from '@/api/psi/CreditCustomerService'
const BUSINESS_TABLE = 'psi_credit_customer'

export default {
	props: {
		businessId: {
			type: String,
			default: ''
		},
		formReadOnly: {
			type: Boolean,
			default: false
		},
		status: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			loading: false,
			inputForm: this.defaultForm()
		}
	},
	creditCustomerService: null,
	computed: {
		isAuditStatus() {
			return ['audit', 'agreeAndReject'].includes(this.status)
		},
		isReadOnly() {
			return this.isAuditStatus || this.formReadOnly === true || this.formReadOnly === 'true'
		}
	},
	created() {
		this.creditCustomerService = new CreditCustomerService()
		this.init()
	},
	activated() {
		this.init()
	},
	watch: {
		businessId() {
			this.init()
		}
	},
	methods: {
		defaultForm() {
			return {
				customerName: '',
				mobile: '',
				birthday: '',
				idCard: '',
				age: undefined,
				occupation: '',
				companyName: '',
				address: '',
				emergencyContact: '',
				emergencyContactPhone: '',
				invoiceTitle: '',
				taxNo: '',
				bankName: '',
				bankAccount: '',
				invoiceAddressPhone: '',
				creditStatus: '1',
				totalArrearsAmount: 0,
				unsettledBillCount: 0,
				firstCreditTime: '',
				lastConsumeTime: '',
				auditorId: '',
				auditorName: '',
				createName: '',
				status: '',
				remarks: ''
			}
		},
		getBusinessId() {
			return this.businessId || this.$route.query.businessId || this.$route.query.id || ''
		},
		init() {
			const id = this.getBusinessId()
			this.inputForm = this.defaultForm()
			this.$nextTick(() => {
				this.$refs.inputForm && this.$refs.inputForm.clearValidate()
			})
			if (!id || id === 'false') {
				return
			}
			this.loading = true
			this.$emit('changeLoading', true)
			this.creditCustomerService.findById(id).then((data) => {
				this.inputForm = this.recover(this.inputForm, data)
				this.inputForm.totalArrearsAmount = Number(this.inputForm.totalArrearsAmount || 0)
				this.inputForm.unsettledBillCount = Number(this.inputForm.unsettledBillCount || 0)
				this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
			}).finally(() => {
				this.loading = false
				this.$emit('changeLoading', false)
			})
		},
		getKeyWatch() {
			this.init()
		},
		close() {
			this.$refs.inputForm && this.$refs.inputForm.resetFields()
			this.inputForm = this.defaultForm()
		},
		validateForm() {
			return new Promise((resolve) => {
				this.$refs.inputForm.validate((valid) => {
					resolve(valid)
				})
			})
		},
		saveForFlow(callback, status = '2') {
			this.validateForm().then((valid) => {
				if (!valid) {
					return
				}
				this.loading = true
				this.$emit('changeLoading', true)
				this.inputForm.status = status
				this.creditCustomerService.saveFlow(this.inputForm).then((data) => {
					this.inputForm.id = data.businessId || this.inputForm.id
					callback && callback(data.businessTable || BUSINESS_TABLE, this.inputForm.id, this.inputForm)
				}).finally(() => {
					this.loading = false
					this.$emit('changeLoading', false)
				})
			})
		},
		saveForm(callback) {
			this.saveForFlow(() => {
				callback && callback()
			}, '1')
		},
		startForm(callback) {
			this.saveForFlow(callback, '2')
		},
		reapplyForm(callback) {
			this.saveForFlow(callback, '2')
		},
		agreeForm(callback) {
			this.updateAuditStatus('5', () => {
				callback && callback(BUSINESS_TABLE, this.getBusinessId(), this.inputForm)
			})
		},
		updateStatusById(type, callback) {
			const statusMap = {
				reject: '4',
				reback: '3',
				hold: '1'
			}
			const status = statusMap[type]
			if (!status) {
				callback && callback()
				return
			}
			this.updateAuditStatus(status, callback)
		},
		updateAuditStatus(status, callback) {
			const id = this.inputForm.id || this.getBusinessId()
			if (!id || id === 'false') {
				callback && callback()
				return
			}
			this.loading = true
			this.$emit('changeLoading', true)
			this.creditCustomerService.updateStatusById({ id, status }).then(() => {
				this.inputForm.status = status
				callback && callback()
			}).finally(() => {
				this.loading = false
				this.$emit('changeLoading', false)
			})
		}
	}
}
</script>

<style scoped>
.credit-customer-approval-page {
	padding: 10px 0 66px;
	min-height: 100px;
}

.approval-card {
	border-radius: 6px;
}

.card-header {
	font-size: 15px;
	font-weight: 600;
	color: #303133;
}

.form-section-title {
	font-size: 14px;
	font-weight: 600;
	color: #303133;
	padding: 4px 0 12px;
}

.form-section-title:not(:first-child) {
	margin-top: 8px;
	border-top: 1px solid #ebeef5;
	padding-top: 16px;
}
</style>
