<template>
  <div class="root">
    <el-form ref="ruleForm" :model="listData" :rules="rules" label-width="150px" class="add-ruleForm">
      <el-form-item label="房间名称" prop="houseName">
        <el-input v-model="listData.houseName" placeholder="房间名称"/>
      </el-form-item>
      <el-form-item label="房间租金" prop="rentalPrice">
        <el-input v-model="listData.rentalPrice" onkeyup="this.value=this.value.replace(/\D/g,'')"  placeholder="元/月"/>
        <span style="font-size:0.7vw;color:#C0C0C0">季租,年租优惠可以写在房间介绍中</span>
      </el-form-item>
      <el-form-item label="面积" prop="area">
        <el-input v-model="listData.area" onkeyup="this.value=this.value.replace(/\D/g,'')" placeholder="单位 平米"/>
      </el-form-item>
      <el-form-item label="app列表展示图" prop="appListImg">
        <el-upload
          class="avatar-uploader"
          name="uploadFiles"
          :action="uploadImgUrl"
          :show-file-list="false"
          :on-success="handleAppListSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="listData.appListImg" :src="listData.appListImg" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="户型" prop="houseType">
        <el-select v-model="listData.houseType" placeholder="请选择">
            <el-option label="一室居" value="1"/>
            <el-option label="二室居" value="2"/>
            <el-option label="三室居" value="3"/>
            <el-option label="四室居及以上" value="4"/>
          </el-select>
      </el-form-item>
      <el-form-item label="租赁方式" prop="rentalType">
        <el-select v-model="listData.rentalType" placeholder="请选择">
            <el-option label="整租" value="1"/>
            <el-option label="合租" value="2"/>
          </el-select>
      </el-form-item>
      <el-form-item label="轮播图(最多六张)" prop="appCarouselImg">
        <el-upload
          name="uploadFiles"
          :limit="6"
          :action="uploadImgUrl"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="handleSuccessCarouselImg">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="上架状态" prop="houseStatus">
        <el-select v-model="listData.houseStatus" placeholder="请选择">
          <el-option label="已上架" value="1"/>
          <el-option label="已下架" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="楼层" prop="houseLevel">
         <el-input v-model="listData.houseLevel" placeholder="楼层"/>
      </el-form-item>
      <el-form-item label="特色标签" prop="houseLabel">
        <el-input v-model="listData.houseLabel"  placeholder="多个标签用,分割最多三个标签"/>
      </el-form-item>
      <el-form-item label="权重" prop="sort">
        <el-input v-model="listData.sort" onkeyup="this.value=this.value.replace(/\D/g,'')" placeholder="权重"/>
      </el-form-item>
      <el-form-item label="联系电话" prop="houseTel">
        <el-input v-model="listData.houseTel" onkeyup="this.value=this.value.replace(/\D/g,'')" placeholder="联系电话"/>
      </el-form-item>
      <el-form-item label="小区地址" prop="communityAddress">
        <el-input v-model="listData.communityAddress" placeholder="小区地址"/>
      </el-form-item>
      <el-form-item label="房屋配套">
        <el-checkbox v-model="listData.bed" true-label="1" false-label="0">床</el-checkbox>
        <el-checkbox v-model="listData.mattress" true-label="1" false-label="0">床垫</el-checkbox>
        <el-checkbox v-model="listData.wardrobe" true-label="1" false-label="0">衣柜</el-checkbox>
        <el-checkbox v-model="listData.sofa" true-label="1" false-label="0">沙发</el-checkbox>
        <el-checkbox v-model="listData.washingMachine" true-label="1" false-label="0">洗衣机</el-checkbox>
        <el-checkbox v-model="listData.airConditioning" true-label="1" false-label="0">空调</el-checkbox>
        <el-checkbox v-model="listData.fridge" true-label="1" false-label="0">冰箱</el-checkbox>
        <el-checkbox v-model="listData.TV" true-label="1" false-label="0">电视</el-checkbox>
        <el-checkbox v-model="listData.smartLock" true-label="1" false-label="0">智能锁</el-checkbox>
        <el-checkbox v-model="listData.waterHeater" true-label="1" false-label="0">热水器</el-checkbox>
        <el-checkbox v-model="listData.desk" true-label="1" false-label="0">桌子</el-checkbox>
        <el-checkbox v-model="listData.chair" true-label="1" false-label="0">椅子</el-checkbox>
        <el-checkbox v-model="listData.WiFi" true-label="1" false-label="0">网络</el-checkbox>
        <el-checkbox v-model="listData.kitchen" true-label="1" false-label="0">厨房</el-checkbox>
        <el-checkbox v-model="listData.bathroom" true-label="1" false-label="0">独卫</el-checkbox>
        <el-checkbox v-model="listData.balcony" true-label="1" false-label="0">阳台</el-checkbox>
      </el-form-item>
      <el-form-item label="地图定位" prop="communityLatitude">
        <span>高德坐标</span>
        <span  style="font-size:14px;color:#C0C0C0">在地图上搜索选择公寓位置确定坐标值，供APP端用户导航</span>
        <span>高德坐标</span>
        <el-input placeholder="经度" v-model="listData.communityLongitude" style="width: 100px;" />
        <el-input placeholder="纬度" v-model="listData.communityLatitude" style="width: 100px;" />
      </el-form-item>
      <el-form-item label="">
         <!-- 地图 -->
        <div style="width:100%;height:300px;" class="amap-page-container">
          <el-amap-search-box :search-option="searchOption" :on-search-result="onSearchResult" class="search-box"/>
          <el-amap ref="map" :events="events" :center="mapCenter" :zoom="12" class="amap-demo" vid="amapDemo">
            <el-amap-marker v-for="(item,index) in markers" :key="index" :position="item" />
          </el-amap>
        </div>
      </el-form-item>
      <el-form-item label="房间介绍">
          <!-- <el-input v-model="addForm.bannerContent" placeholder="内容详情"/> -->
          <div id="father">
            <wangeditor @wangeditorEvent="wangeditorValue"/>
          </div>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import Wangeditor from '@/components/Wangeditor'

export default {
  components: { Pagination, Wangeditor },
  data() {
    return {
      events: {
        click: e => {
          // _self.postData.Coordinate = e.lnglat.lat + ',' + e.lnglat.lng
          this.listData.communityLongitude = e.lnglat.lng
          this.listData.communityLatitude = e.lnglat.lat
        }
      },
      listData: {
        houseName: '',
        rentalPrice: '',
        area: '',
        appListImg: '',
        appCarouselImg: [],
        houseType: '',
        rentalType: '',
        houseStatus: '',
        houseLevel: '',
        houseLabel: '',
        sort: '',
        houseTel: '',
        communityAddress: '',
        communityLongitude: '',
        communityLatitude: '',
        houseIntroduction: '',
        bed: '',
        mattress: '',
        wardrobe: '',
        sofa: '',
        washingMachine: '',
        airConditioning: '',
        fridge: '',
        TV: '',
        smartLock: '',
        waterHeater: '',
        desk: '',
        chair: '',
        WiFi: '',
        kitchen: '',
        bathroom: '',
        balcony: ''
      },
      dialogImageUrl: '',
      dialogVisible: false,
      uploadImgUrl: process.env.BASE_API + '/uploadimage',
      markers: [],
      shopTypeList: [],
      searchOption: {
        city: '南京',
        citylimit: false
      },
      mapCenter: [118.789509, 32.019989],
      tableKey: 0,
      rules: {
        houseName: [
          { required: true, message: '请输入房间名称', trigger: 'blur' }
        ],
        rentalPrice: [
          { required: true, message: '请输入房间租金', trigger: 'change' }
        ],
        houseTel: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        houseStatus: [
          { required: true, message: '请选择上架状态', trigger: 'blur' }
        ],
        houseType: [
          { required: true, message: '请选择户型', trigger: 'blur' }
        ],
        rentalType: [
          { required: true, message: '请选择租赁方式', trigger: 'blur' }
        ],
        appListImg: [
          { required: true, message: 'app列表展示图', trigger: 'blur' }
        ],
        appCarouselImg: [
          { required: true, message: '轮播图', trigger: 'blur' }
        ],
        communityAddress: [
          { required: true, message: '请选择小区地址', trigger: 'blur' }
        ],
        communityLatitude: [
          { required: true, message: '请选择坐标', trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
  },
  methods: {
     addMarker() {
      const lng = 121.5 + Math.round(Math.random() * 1000) / 10000
      const lat = 31.197646 + Math.round(Math.random() * 500) / 10000
      this.markers.push([lng, lat])
    },
    beforeAvatarUpload(file) {
    },
    handleAppIndexSuccess(res, file) {
      this.listData.appIndexImg = res.data[0]
    },
    handleAppListSuccess(res, file) {
      this.listData.appListImg = res.data[0]
    },
    handleRemove(file, fileList) {
      this.listData.appCarouselImg = []
      for (let i = 0; i < fileList.length; i++) {
        this.listData.appCarouselImg.push(fileList[i].url)
      }
    },
    handleSuccessCarouselImg(response, file, fileList) { // 活动配图上传成功时回调
      const resImg = response.data[0]
      this.listData.appCarouselImg.push(resImg)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    onSearchResult(pois) {
      // 搜索地图
      let latSum = 0
      let lngSum = 0
      if (pois.length > 0) {
        pois.forEach(poi => {
          const { lng, lat } = poi
          lngSum += lng
          latSum += lat
          this.markers.push([poi.lng, poi.lat])
        })
        const center = {
          lng: lngSum / pois.length,
          lat: latSum / pois.length
        }
        this.mapCenter = [center.lng, center.lat]
      }
    },
    wangeditorValue(value) {
      this.listData.houseIntroduction = value // 在这里接受子组件传过来的参数，赋值给data里的参数
    },
    submitForm(formName) { // 提交
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addRental()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) { // 重置
      // 重置为未注册
      this.isRegistered = true
      // 重置为非户主
      this.roleDisabled = false

      console.log(this.isRegistered)
      this.$refs[formName].resetFields()
    },
    addRental() {
      // 加载框
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      if(this.listData.appCarouselImg == 0){
         this.$message.error("轮播图不能为空")
        loading.close()
        return
      }
      this.$store.dispatch('rental/AddRental', this.listData).then((res) => {
        if (res.code === '0') {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.push({ name: 'rental-index' })
          loading.close()
          return
        }
        this.$message.error(res.message)
        loading.close()
      }).catch(() => {
        loading.close()
        console.log('error addRental')
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  
.add-ruleForm{
  width: 800px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 50px;
}
</style>
<style scoped>
.search-box {
  position: absolute;
  top: 25px;
  left: 20px;
}

.amap-page-container {
  position: relative;
}
</style>
