<template>
	<view class="">
		<view class="data-from" v-for="item in firmList">
			<en-input label="企业名称" type="text" v-model="item.firm_name" v-if="verifyKey('firm_name')"
				placeholder="请输入公司名称"></en-input>
			<en-date label="成立时间" v-model="item.establish_date" v-if="verifyKey('establish_date')"
				placeholder="选择成立时间"></en-date>
			<en-input label="年交费额" v-model="item.year_money" v-if="verifyKey('year_money')" type="number"
				placeholder="请输入年交费额" rightText="元"></en-input>
			<en-city label="实际经营地" v-model="item.establish_city" v-if="verifyKey('establish_city')"
				placeholder="请选择省、市、区"></en-city>
			<en-input label="经营详细地址" v-model="item.establish_address" v-if="verifyKey('establish_address')" type="text"
				placeholder="请输入经营详细地址"></en-input>
			<en-upload label="上传企业相关图片" v-model="item.firm_img" v-if="verifyKey('firm_img')"
				:imageWidth="180"></en-upload>
			<en-input label="年交税额" v-model="item.year_tax" v-if="verifyKey('year_tax')" type="number"
				placeholder="请输入年交税额" rightText="元"></en-input>
			<en-input label="年开票额" v-model="item.year_invoice" v-if="verifyKey('year_invoice')" type="number"
				placeholder="请输入年开票额" rightText="元"></en-input>
			<en-select label="税务等级" v-model="item.tax_grade" v-if="verifyKey('tax_grade')" placeholder="请选择税务等级"
				:local-data="taxGradeData"></en-select>
			<en-radio label="税务有无断缴" v-model="item.is_end_tax" v-if="verifyKey('is_end_tax')" :radio-data="endData"
				:noBox="true"></en-radio>
		</view>
		<en-blank message="暂无企业信息,快来添加吧！" v-if="firmList.length<=0"></en-blank>
		<button class="size-26 r-10 button-color house-button m-t30" hover-class="is-hover"
			@click="addItem">+添加企业</button>
	</view>
</template>

<script>
	import enInput from "@/components/en-from/en-input/en-input.vue"
	import EnCity from "@/components/en-from/en-city/en-city.vue";
	import EnUpload from "@/components/en-from/en-upload/en-upload.vue";
	import EnSelect from "@/components/en-from/en-select/en-select.vue";
	import {
		getTaskOptions
	} from "@/api/task";
	import md5 from "js-md5";
	import EnDate from "@/components/en-from/en-date/en-date.vue";
	import EnBlank from "@/components/en-utils/en-blank/en-blank.vue";
	import tools from "@/service/tools";
	import {
		getSn
	} from "@/api/common";

	export default {
		name: 'property-guarantee',
		components: {
			EnBlank,
			EnDate,
			EnSelect,
			EnUpload,
			EnCity,
			enInput
		},
		props: {
			'showKeys': {
				default: []
			},
			'value': {
				default: {}
			},
			'itemKey': {
				default: 0
			}
		},
		data() {
			return {
				endData: [{
					'id': 1,
					'name': '有'
				}, {
					'id': 0,
					'name': '无'
				}],
				taxGradeData: [],
				firmItem: {
					'firm_name': '',
					'establish_date': '',
					'year_money': '',
					'establish_city': '',
					'establish_address': '',
					'firm_img': [],
					'year_tax': '',
					'tax_grade': '',
					'is_end_tax': '0',
					'year_invoice': '',
				},
				firmList: []
			}
		},
		watch: {
			'firmList': {
				handler() {
					this.$emit("input", this.firmList);
				},
				deep: true
			},
			'value': {
				handler() {
					if (this.value) {
						this.setValue()
					}
				},
				deep: true
			},
		},
		mounted() {
			this.getTaskOptions()
			this.setValue()
		},
		methods: {
			addItem() {
				tools.showLoading()
				getSn().then((res) => {
					if (res.code === 1) {
						this.firmList.push({
							'property_type': 4,
							'property_sn': res.data,
							'data': this.firmItem
						})
						this.$emit('onResize')
					} else {
						tools.error('编号生成失败')
					}
					tools.hideLoading()
				})
			},
			setSendMd5() {
				let str = JSON.stringify(this.value)
				this.sendMd5 = md5(JSON.stringify(this.firmList))
				return md5(str)
			},
			setValue() {
				if (this.value) {
					let sendMd5 = this.setSendMd5()
					if (sendMd5 !== this.sendMd5) {
						this.firmList = this.value
					}
				}
			},
			verifyKey(field) {
				return this.showKeys.indexOf(field) >= 0
			},
			async getTaskOptions() {
				const res = await getTaskOptions({
					'type': 5
				})
				if (res.code === 1) {
					res.data.taxGrade.forEach((val) => {
						this.taxGradeData.push({
							'text': val.name,
							'value': val.value
						})
					})
				}
			},
			delItem() {
				uni.showModal({
					title: '警告',
					content: '是否删除当前企业信息！',
					success: (res) => {
						if (res.confirm) {
							this.$emit('delItem', this.itemKey)
						}
					}
				});
			}
		}
	}
</script>

<style>
	.house-button {
		height: 70rpx;
		line-height: 70rpx;
		background: #FFFFFF;
		border: 1rpx solid #0FB160;
	}
</style>