<template>
  <div class="root">
    <el-form ref="ruleForm" :model="listData" :rules="rules" label-width="150px" class="add-ruleForm">
     
      <el-form-item class="filter-item" label="酒店开关">
        <span  style="font-size:0.7vw;color:#C0C0C0">关闭后业主在APP端将看不到酒店相关的页面</span>
        <el-switch style="padding-left:25%"
          v-model="listData.hotelSettingValue"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="酒店首页图" prop="appIndexImg">
        <span  style="font-size:0.7vw;color:#C0C0C0"> APP端首页显示酒店栏的背景图，如果有文案需求可以用PS在图上添加文字等信息</span>
        <el-upload
          class="avatar-uploader"
          name="uploadFiles"
          :action="uploadImgUrl"
          :show-file-list="false"
          :on-success="handleAppListSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="listData.appIndexImg" :src="listData.appIndexImg" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="酒店banner图" prop="appCarouselImg">
        <el-upload
          name="uploadFiles"
          :limit="6"
          :file-list="displayAppCarouselImg"
          :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="酒店名称">
        <el-input v-model="listData.hotelName"/>
      </el-form-item> -->
      <el-form-item label="联系电话" prop="hotelTel">
        <el-input v-model="listData.hotelTel"/>
      </el-form-item>
      <el-form-item label="酒店地址" prop="hotelAddress">
        <el-input v-model="listData.hotelAddress"/>
      </el-form-item>
      <el-form-item label="地图定位">
        <span  style="font-size:14px;color:#C0C0C0">在地图上搜索选择酒店位置确定坐标值，供APP端用户导航</span>
        <span>高德坐标</span>
        <el-input placeholder="经度" v-model="listData.hotelLongitude" style="width: 100px;" />
        <el-input placeholder="纬度" v-model="listData.hotelLatitude" 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="酒店介绍" prop="hotelIntroduction">
          <!-- <el-input v-model="addForm.bannerContent" placeholder="内容详情"/> -->
          <div id="father">
            <wangeditor :content="listData.hotelIntroduction" @wangeditorEvent="wangeditorValue"/>
          </div>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('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.hotelLongitude = e.lnglat.lng
          this.listData.hotelLatitude = e.lnglat.lat
        }
      },
      listData: {
        id: '',
        hotelSettingValue: false,
        appIndexImg: '',
        appCarouselImg: [],
        hotelTel: '',
        hotelAddress: '',
        hotelLongitude: '',
        hotelLatitude: '',
        hotelIntroduction: '',
        hotelName: ''
      },
      dialogImageUrl: '',
      dialogVisible: false,
      uploadImgUrl: process.env.BASE_API + '/uploadimage',
      markers: [],
      displayAppCarouselImg: [],
      shopTypeList: [],
      searchOption: {
        city: '南京',
        citylimit: false
      },
      mapCenter: [118.789509, 32.019989],
      tableKey: 0,
      rules: {
        hotelAddress: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ],
        hotelTel: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        appIndexImg: [
          { required: true, message: 'app列表展示图', trigger: 'blur' }
        ],
        appCarouselImg: [
          { required: true, message: '轮播图', trigger: 'blur' }
        ],
        hotelName: [
          { required: true, message: '酒店名称', trigger: 'blur' }
        ],
        hotelLatitude: [
          { required: true, message: '请输入坐标', trigger: 'blur' }
        ],
        hotelIntroduction: [
          { required: true, message: '酒店介绍不能为空', trigger: 'change' }
        ],
      }
    }
  },
  created() {
    this.getHotelData()
  },
  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])
    },
    getHotelData() {
      this.$store.dispatch('GetHotelData', this.listData.id).then((res) => {
        const rentalData = res.data || {}
        this.listData = { ...this.listData, ...rentalData }
        // 多张图片进行遍历
        for (let i = 0; i < rentalData.appCarouselImg.length; i++) {
          this.displayAppCarouselImg.push({ url: rentalData.appCarouselImg[i] })
        }
      })
    },
    beforeAvatarUpload(file) {
    },
    handleAppIndexSuccess(res, file) {
      this.listData.appIndexImg = res.data[0]
    },
    handleAppListSuccess(res, file) {
      this.listData.appIndexImg = res.data[0]
    },
    handleRemove(file, fileList) {
      this.listData.appCarouselImg = []
      for (let i = 0; i < fileList.length; i++) {
        this.listData.appCarouselImg.push(fileList[i].response.data[0])
      }
    },
    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.hotelIntroduction = value // 在这里接受子组件传过来的参数，赋值给data里的参数
    },
    submitForm(formName) { // 提交
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.updateRental()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    resetForm(formName) { // 重置
      // 重置为未注册
      this.isRegistered = true
      // 重置为非户主
      this.roleDisabled = false

      console.log(this.isRegistered)
      this.$refs[formName].resetFields()
    },
    updateRental() {
      // 加载框
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      console.log('this.listData.hotelIntroduction',this.listData.hotelIntroduction)
      if(this.listData.hotelIntroduction === '<p><br></p>'){
         this.$message.error('酒店介绍不能为空')
         loading.close()
         return
      }
      this.$store.dispatch('UpdateHotel', this.listData).then((res) => {
        if (res.code === '0') {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.push({ name: 'grogshop-configindex' })
          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>
