<template>
	<view class="">
    <view class="data-from" v-for="(item,index) in insuranceList">
      <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('insurance_name')">
            <view class="text-color-666"><text></text>保险公司：</view>
            <text class="flex">{{item.data.insurance_name}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('insurance_type')">
            <view class="text-color-666"><text></text>保险类型：</view>
            <text class="flex">{{getTypeText(item.data.insurance_type,1)}}</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('is_fees')">
            <view class="text-color-666"><text></text>满足缴费情况：</view>
            <text class="flex">{{item.data.is_fees===1?'满足':'未满足'}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('validity_date')">
            <view class="text-color-666"><text></text>保单有效期：</view>
            <text class="flex">{{item.data.validity_date}}</text>
          </view>
          <view class="row p-t30" v-if="verifyKey('insurance_img')">
            <view class="text-color-666"><text></text>保单相关图片：</view>
            <EnImage :img="item.data.insurance_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" placeholder="请输入保险公司名称" v-model="item.data.insurance_name"
                  v-if="verifyKey('insurance_name')"></en-input>
        <en-select label="保险类型" placeholder="请选择保险类型" v-model="item.data.insurance_type"
                   v-if="verifyKey('insurance_type')" :local-data="insuranceTypeData"></en-select>
        <en-input label="年交费额" type="number" v-model="item.data.year_money" v-if="verifyKey('year_money')"
                  placeholder="请输入年交费额" rightText="元"></en-input>
        <en-radio label="满足缴费情况" v-model="item.data.is_fees" v-if="verifyKey('is_fees')"
                  :list="payData"></en-radio>
        <en-date label="保单有效期" v-model="item.data.validity_date" v-if="verifyKey('validity_date')"
                 item="选择保单有效期"></en-date>
        <en-upload label="上传保单相关图片" v-model="item.data.insurance_img" v-if="verifyKey('insurance_img')"
                   :imageWidth="180"></en-upload>
        <en-input label="备注" v-model="item.data.remark" v-if="verifyKey('remark')" type="text" placeholder="请输入备注信息"
                  :noBox="true"></en-input>
      </view>
    </view>
    <en-blank message="暂无保单信息,快来添加吧！" v-if="insuranceList.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,
				payData: [{
					'value': 1,
					'text': '满足'
				}, {
					'value': 0,
					'text': '未足'
				}],
				insuranceTypeData: [],
				insuranceItem: {
					'insurance_name': '',
					'insurance_type': '',
					'year_money': '',
					'is_fees': '1',
					'validity_date': '',
					'insurance_img': [],
					'remark': '',
				},
				insuranceList: []
			}
		},
		watch: {
			'insuranceList': {
				handler() {
					this.$emit("input", this.insuranceList);
				},
				deep: true
			},
			'value': {
				handler() {
					if (this.value) {
						this.setValue()
					}
				},
				deep: true
			},
		},
		mounted() {
			this.getTaskOptions()
			this.setValue()
      console.log(this.payData)
		},
		methods: {
      getTypeText(val){
        val=val*1
        for (const typeKey in   this.insuranceTypeData) {
          if(val===this.insuranceTypeData[typeKey].value){
            return this.insuranceTypeData[typeKey].text
          }
        }


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

      },
      onEditInfo(index){
        this.editIndex = index
      },
			addItem() {
				tools.showLoading()
				getSn().then((res) => {
					if (res.code === 1) {
						this.insuranceList.push({
							'property_type': 3,
							'property_sn': res.data,
							'data': JSON.parse(JSON.stringify(this.insuranceItem))
						})
            this.editIndex=this.insuranceList.length-1
						this.$emit('onResize')
					} else {
						tools.error('编号生成失败')
					}
					tools.hideLoading()
				})
			},
			setSendMd5() {
				let str = JSON.stringify(this.value)
				this.sendMd5 = md5(JSON.stringify(this.insuranceList))
				return md5(str)
			},
			setValue() {
				if (this.value) {
					let sendMd5 = this.setSendMd5()
					if (sendMd5 !== this.sendMd5) {
						this.insuranceList = this.value
					}
				}
			},
			verifyKey(field) {
				return this.showKeys.indexOf(field) >= 0
			},
			async getTaskOptions() {
				const res = await getTaskOptions({
					'type': 4
				})
				if (res.code === 1) {
					res.data.insuranceType.forEach((val) => {
						this.insuranceTypeData.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>
