<template>
	<view class="">
		<view class="data-from" v-for="(item,index) in firmList">
      <view class="p-t30" v-if="editIndex!==index">
        <view  class="sys-from-background-color p-20 r-20">
          <view class="row-justify-sb center">
            <text class="size-28 sys-weight-600">企业信息({{index+1}})</text>
            <view class="row-c sys-background-fff r-100 p-lr40 p-tb10">
              <image class="wh-30"
                     src="/page_task/static/img/information/edit.png"
                     mode="aspectFill" @click="onEditInfo(index)"></image>
              <view class="title-line m-lr20"></view>
              <image class="wh-30"
                     src="/page_task/static/img/information/delete.png"
                     mode="aspectFill" @click="onDeleteInfo(index)">
              </image>
            </view>
          </view>
          <view class="row p-t30" v-if="verifyKey('firm_name')">
            <view class="text-color-666"><text></text>企业名称：</view>
            <text class="flex">{{item.data.firm_name}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('establish_date')">
            <view class="text-color-666"><text></text>成立时间：</view>
            <text class="flex">{{item.data.establish_date}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('year_money')">
            <view class="text-color-666"><text></text>年交费额：</view>
            <text class="flex">{{item.data.year_money}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('establish_city')">
            <view class="text-color-666"><text></text>实际经营地：</view>
            <text class="flex">{{item.data.establish_city}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('establish_address')">
            <view class="text-color-666"><text></text>经营详细地址：</view>
            <text class="flex">{{item.data.establish_address}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('year_tax')">
            <view class="text-color-666"><text></text>年交税额：</view>
            <text class="flex">{{item.data.year_tax}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('year_invoice')">
            <view class="text-color-666"><text></text>年开票额：</view>
            <text class="flex">{{item.data.year_invoice}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('tax_grade')">
            <view class="text-color-666"><text></text>税务等级：</view>
            <text class="flex">{{getTypeText(item.data.tax_grade)}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('is_end_tax')">
            <view class="text-color-666"><text></text>税务有无断缴：</view>
            <text class="flex">{{item.data.is_end_tax===1?'有':'无'}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('firm_img')">
            <view class="text-color-666"><text></text>企业相关图片：</view>
            <EnImage :img="item.data.firm_img"></EnImage>
          </view>

          <view class="row p-t30">
            <view class="text-color-666"><text></text>备注：</view>
            <text class="flex">{{item.data.remark}}</text>
          </view>
        </view>
      </view>
      <view class="p-t30" v-else>
        <view class="row-justify-sb center p-b10">
          <text class="size-28 sys-weight-600">企业信息({{index+1}})</text>
          <view class="row-c sys-background-fff r-100">
            <image class="wh-30"
                   src="/page_task/static/img/information/delete.png"
                   mode="aspectFill" @click="onDeleteInfo(index)">
            </image>
          </view>
        </view>
			<en-input label="企业名称" type="text" v-model="item.data.firm_name" v-if="verifyKey('firm_name')"
				placeholder="请输入公司名称"></en-input>
			<en-date label="成立时间" v-model="item.data.establish_date" v-if="verifyKey('establish_date')"
				placeholder="选择成立时间"></en-date>
			<en-input label="年交费额" v-model="item.data.year_money" v-if="verifyKey('year_money')" type="number"
				placeholder="请输入年交费额" rightText="元"></en-input>
			<en-city label="实际经营地" v-model="item.data.establish_city" v-if="verifyKey('establish_city')"
				placeholder="请选择省、市、区"></en-city>
			<en-input label="经营详细地址" v-model="item.data.establish_address" v-if="verifyKey('establish_address')" type="text"
				placeholder="请输入经营详细地址"></en-input>
			<en-upload label="上传企业相关图片" v-model="item.data.firm_img" v-if="verifyKey('firm_img')"
				:imageWidth="180"></en-upload>
			<en-input label="年交税额" v-model="item.data.year_tax" v-if="verifyKey('year_tax')" type="number"
				placeholder="请输入年交税额" rightText="元"></en-input>
			<en-input label="年开票额" v-model="item.data.year_invoice" v-if="verifyKey('year_invoice')" type="number"
				placeholder="请输入年开票额" rightText="元"></en-input>
			<en-select label="税务等级" v-model="item.data.tax_grade" v-if="verifyKey('tax_grade')" placeholder="请选择税务等级"
				:local-data="taxGradeData"></en-select>
			<en-radio label="税务有无断缴" v-model="item.data.is_end_tax" v-if="verifyKey('is_end_tax')" :list="endData"
				:noBox="true"></en-radio>
      </view>
		</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";
  import EnImage from "@/components/en-utils/en-image/en-image.vue";

	export default {
		name: 'property-guarantee',
		components: {
      EnImage,
			EnBlank,
			EnDate,
			EnSelect,
			EnUpload,
			EnCity,
			enInput
		},
		props: {
			'showKeys': {
				default: []
			},
			'value': {
				default: {}
			},
			'itemKey': {
				default: 0
			}
		},
		data() {
			return {
        editIndex:0,
				endData: [{
					'value': 1,
					'text': '有'
				}, {
					'value': 0,
					'text': '无'
				}],
				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: {
      getTypeText(val){
        val=val*1
        for (const typeKey in   this.taxGradeData) {
          if(val===this.taxGradeData[typeKey].value){
            return this.taxGradeData[typeKey].text
          }
        }


      },
      onDeleteInfo(itemKey) {
        uni.showModal({
          title: '是否删除当前资产信息',
          content: '确定删除?',
          success: (res) => {
            if (res.confirm) {
              this.firmList.splice(itemKey, 1)
              this.editIndex = itemKey-1
            }
          }
        })

      },
      onEditInfo(index){
        this.editIndex = index
      },
			addItem() {
				tools.showLoading()
				getSn().then((res) => {
					if (res.code === 1) {
						this.firmList.push({
							'property_type': 4,
							'property_sn': res.data,
							'data': JSON.parse(JSON.stringify(this.firmItem))
						})
            this.editIndex=this.firmList.length-1
						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>
