<template>
  <div class="root">
    <el-form ref="ruleForm" :model="listData" :rules="rules" label-width="150px" class="add-ruleForm">
      <el-form-item label="商铺名称" prop="shopName">
        <el-input v-model="listData.shopName"/>
      </el-form-item>
      <el-form-item label="商铺说明" prop="remark">
        <el-input v-model="listData.remark"/>
      </el-form-item>
      <el-form-item label="app首页展示图" prop="appIndexImg">
        <el-upload
          class="avatar-uploader"
          name="uploadFiles"
          :action="uploadImgUrl"
          :show-file-list="false"
          :on-success="handleAppIndexSuccess"
          :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="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="app商铺轮播图(最多六张)" 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="sort">
        <el-input v-model="listData.sort" onkeyup="this.value=this.value.replace(/\D/g,'')"/>
        <span style="font-size:0.7vw;color:#C0C0C0;">权重值越大，排序越靠前</span>
      </el-form-item>
      <el-form-item label="商铺类型" prop="shopTypeId">
        <el-select v-model="listData.shopTypeId" placeholder="商铺类型" clearable class="filter-item">
          <el-option v-for="item in shopTypeList" :key="item.id" :value="item.id" :label="item.typeName"/>
        </el-select>
        <a @click="shopSetting"><span style="font-size:20px;padding-left:4%;color:#00BFFF">维护类型</span></a>
      </el-form-item>
      <el-form-item label="上架状态" prop="shopStatus">
        <el-select v-model="listData.shopStatus" placeholder="请选择">
          <el-option label="已上架" value="1"/>
          <el-option label="已下架" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="shopTel">
        <el-input v-model="listData.shopTel" onkeyup="this.value=this.value.replace(/\D/g,'')"/>
      </el-form-item>
      <el-form-item label="商铺地址" prop="shopAddress">
        <el-input v-model="listData.shopAddress"/>
      </el-form-item>
      <el-form-item label="人均消费" prop="averagePrice">
        <el-input v-model="listData.averagePrice" onkeyup="this.value=this.value.replace(/\D/g,'')" placeholder="单位 元/人"/>
      </el-form-item>
      <el-form-item label="地图定位" prop="shopLatitude">
        <span  style="font-size:14px;color:#C0C0C0">在地图上搜索选择酒店位置确定坐标值，供APP端用户导航</span>
        <span>高德坐标</span>
        <el-input placeholder="经度" v-model="listData.shopLongitude" style="width: 100px;" />
        <el-input placeholder="纬度" v-model="listData.shopLatitude" 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.shopLongitude = e.lnglat.lng
          this.listData.shopLatitude = e.lnglat.lat
        }
      },
      listData: {
        shopName: '',
        remark: '',
        appIndexImg: '',
        appListImg: '',
        appCarouselImg: [],
        sort: '1',
        shopTypeId: '',
        shopStatus: '',
        shopTel: '',
        shopAddress: '',
        averagePrice: '',
        shopLongitude: '',
        shopLatitude: '',
        shopIntroduction: ''
      },
      dialogImageUrl: '',
      dialogVisible: false,
      uploadImgUrl: process.env.BASE_API + '/uploadimage',
      markers: [],
      shopTypeList: [],
      searchOption: {
        city: '南京',
        citylimit: false
      },
      mapCenter: [118.789509, 32.019989],
      tableKey: 0,
      rules: {
        typeName: [
          { required: true, message: '请输入商铺名称', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '请输入商铺说明', trigger: 'blur' }
        ],
        shopTel: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        shopAddress: [
          { required: true, message: '请输入商铺地址', trigger: 'blur' }
        ],
        appIndexImg: [
          { required: true, message: 'app首页展示图', trigger: 'blur' }
        ],
        appListImg: [
          { required: true, message: 'app列表展示图', trigger: 'blur' }
        ],
        appCarouselImg: [
          { required: true, message: 'app商铺轮播图', trigger: 'blur' }
        ],
        shopName: [
          { required: true, message: '请输入商铺名称', trigger: 'blur' }
        ],
        shopStatus: [
          { required: true, message: '请输入上架状态', trigger: 'blur' }
        ],
        shopLatitude: [
          { required: true, message: '请输入坐标', trigger: 'blur' }
        ],
        averagePrice: [
          { required: true, message: '请输入人均消费', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getShopTypeList()
  },
  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].response.data[0])
      }
    },
    handleSuccessCarouselImg(response, file, fileList) { // 活动配图上传成功时回调
      const resImg = response.data[0]
      this.listData.appCarouselImg.push(resImg)
      console.log('this.listData.appCarouselImg.length',this.listData.appCarouselImg.length)
      if(this.listData.appCarouselImg.length === 6){
         this.$message({
          message: '当期图片添加已到最大限制',
          type: 'warning'
        });
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    getShopTypeList() {
      this.$store.dispatch('shopType/FetchShopTypeSelect', this.listData).then((res) => {
        this.shopTypeList = res.data
      }).catch(() => {
        console.log('error ListAnnouncement')
      })
    },
    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.shopIntroduction = value // 在这里接受子组件传过来的参数，赋值给data里的参数
    },
    submitForm(formName) { // 提交
    console.log('this.listData.shopIntroduction',this.listData.shopIntroduction)
    if(this.listData.shopIntroduction ==='<p><br></p>'){
        this.$message.error('商铺介绍不能为空');
        return
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addShop()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) { // 重置
      // 重置为未注册
      this.isRegistered = true
      // 重置为非户主
      this.roleDisabled = false

      console.log(this.isRegistered)
      this.$refs[formName].resetFields()
    },
    addShop() {
      console.log('this.listData.shopIntroduction',this.listData.shopIntroduction)
      if(this.listData.shopIntroduction ===''){
        this.$message.error('商铺介绍不能为空');
        return
      }
      // 加载框
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.$store.dispatch('shopType/AddShop', this.listData).then((res) => {
        if (res.code === '0') {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.push({ name: 'shop-index' })
          loading.close()
          return
        }
        this.$message.error(res.message)
        loading.close()
      }).catch(() => {
        loading.close()
        console.log('error AddBuilding')
      })
    },
    shopSetting(){
       this.$router.push({ name: 'shop-setting'})
    }
  }
}
</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>
