<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" draggable width="980px" @close="close"
			@keyup.enter.native="doSubmit" v-model="visible">
			<el-form :model="inputForm" ref="inputForm" v-loading="loading"
				:class="method === 'view' ? 'readonly' : ''" :disabled="method === 'view'" 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>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
					<el-button type="primary" v-if="method !== 'view'" @click="doSubmit()"
						icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import CreditCustomerService from '@/api/psi/CreditCustomerService'
export default {
	data() {
		return {
			title: '',
			method: '',
			visible: false,
			loading: false,
			inputForm: this.defaultForm()
		}
	},
	creditCustomerService: null,
	created() {
		this.creditCustomerService = new CreditCustomerService()
	},
	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: ''
			}
		},
		init(method, id) {
			this.method = method
			this.inputForm = this.defaultForm()
			if (method === 'add') {
				this.title = '新建挂账人'
			} else if (method === 'edit') {
				this.title = '修改挂账人'
				this.inputForm.id = id
			} else if (method === 'view') {
				this.title = '查看挂账人'
				this.inputForm.id = id
			}
			this.visible = true
			this.loading = false
			this.$nextTick(() => {
				this.$refs.inputForm && this.$refs.inputForm.clearValidate()
				if (method === 'edit' || method === 'view') {
					this.loading = true
					this.creditCustomerService.findById(this.inputForm.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))
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		doSubmit() {
			this.$refs.inputForm.validate((valid) => {
				if (valid) {
					this.loading = true
					this.creditCustomerService.save(this.inputForm).then((data) => {
						if (data && data.indexOf('重复') > -1) {
							this.$message.error(data)
						} else {
							this.$message.success(data)
							this.close()
							this.$emit('refreshList')
						}
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		close() {
			this.$refs.inputForm && this.$refs.inputForm.resetFields()
			this.visible = false
		}
	}
}
</script>

<style scoped>
.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>
