<template>
  <div class="mod-shopInfo-add-or-update">
    <div class="title">我的店铺</div>
    <div id="shopForm">
      <el-form
        :model="dataForm"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="120px"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="店铺名称" prop="shopName">
              <el-input v-model="dataForm.shopName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="店铺状态" prop="shopStatus">
              <el-tag v-if="dataForm.shopStatus == -1">未开通</el-tag>
              <el-tag v-else-if="dataForm.shopStatus == 0" type="danger">停业中</el-tag>
              <el-tag v-else-if="dataForm.shopStatus == 1" type="success">营业中</el-tag>
              <el-tag v-else-if="dataForm.shopStatus == 2" type="danger">违规下线</el-tag>
              <el-tag v-else-if="dataForm.shopStatus == 3" type="warning">平台审核</el-tag>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="绑定手机号" prop="mobile">
              <el-input v-model="dataForm.mobile" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="tel">
              <el-input v-model="dataForm.tel"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="省份" prop="province">
              <el-select v-model="dataForm.provinceId" placeholder="请选择" @change="selectProvince">
                <el-option
                  v-for="province in provinceList"
                  :key="province.code"
                  :label="province.areaName"
                  :value="province.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="city" label="城市">
              <el-select v-model="dataForm.cityId" placeholder="请选择" @change="selectCity">
                <el-option
                  v-for="city in cityList"
                  :key="city.code"
                  :label="city.areaName"
                  :value="city.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="area" label="区/县">
              <el-select v-model="dataForm.areaId" placeholder="请选择" @change="mapLocation(true)">
                <el-option
                  v-for="area in areaList"
                  :key="area.code"
                  :label="area.areaName"
                  :value="area.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="店铺详细地址" prop="shopAddress">
          <el-input v-model="dataForm.shopAddress" @change="mapLocation">
            <el-button slot="append" icon="el-icon-location" @click="mapLocation">定位</el-button>
          </el-input>
        </el-form-item>

        <el-form-item>
          <baidu-map
            class="map"
            :scroll-wheel-zoom="false"
            :center="center"
            @moving="syncCenterAndZoom"
            @moveend="syncCenterAndZoom"
            @zoomend="syncCenterAndZoom"
            :zoom="zoom"
          >
            <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
            <bm-marker :position="center" :dragging="false" @click="infoWindowOpen">
              <bm-info-window :show="show" @close="infoWindowClose" @open="infoWindowOpen">店铺所在位置</bm-info-window>
            </bm-marker>
          </baidu-map>
        </el-form-item>

        <el-form-item label="店铺描述" prop="intro">
          <el-input type="textarea" :rows="2" v-model="dataForm.intro"></el-input>
        </el-form-item>

        <el-row>
          <el-col :span="8">
            <el-form-item label="店铺logo" prop="shopLogo">
              <img-upload v-model="dataForm.shopLogo" class="upload-logo"></img-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="营业执照" prop="businessLicense">
              <pic-upload v-model="dataForm.businessLicense" :disabled="false" class="license"></pic-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8" class="idcard-width">
            <el-form-item label="身份证正面" prop="identityCardFront">
              <pic-upload v-model="dataForm.identityCardFront" :disabled="false" class="idcard"></pic-upload>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="idcard-width">
            <el-form-item label="身份证反面" prop="identityCardLater">
              <pic-upload v-model="dataForm.identityCardLater" :disabled="false" class="idcard"></pic-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="物品类别" prop="category">
          <el-checkbox-group 
            v-model="dataForm.category"
            >
            <el-checkbox v-for="item in typeOptions" :label="item.value" :key="item.label">{{item.label}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-row style="margin-top: 30px;">
          <el-form-item>
            <!-- <el-button @click="resetForm()">重置</el-button> -->
            <el-button type="primary" @click="dataFormSubmit()">提交修改</el-button>
            <!-- <el-button type="primary"
                       icon="el-icon-document-checked"
                       v-if="dataForm.shopStatus > 1"
            @click="auditEventHandle()">下线管理</el-button>-->
          </el-form-item>
        </el-row>
      </el-form>
    </div>

    <!-- 下线管理弹窗-->
    <offlineEventHandle
      v-if="offlineEventHandleVisible"
      selectUrl="/shop/shopDetail/getOfflineHandleEventByShopId"
      applyUrl="/shop/shopDetail/auditApply"
      ref="offlineEvent"
      @refreshDataList="refreshChange"
    ></offlineEventHandle>
  </div>
</template>

<script>
import PicUpload from '@/components/pic-upload'
import ImgUpload from '@/components/img-upload'
import OfflineEventHandle from '@/components/offline-event-handle'
export default {
  data () {
    return {
      typeOptions: [
        {
          value: 1,
          label: '食品'
        },
        {
          value: 2,
          label: '饮品'
        },
        {
          value: 3,
          label: '鲜花'
        },
        {
          value: 4,
          label: '票务'
        },
        {
          value: 5,
          label: '超市'
        },
        {
          value: 6,
          label: '水果'
        },
        {
          value: 7,
          label: '医药'
        },
        {
          value: 8,
          label: '蛋糕'
        },
        {
          value: 9,
          label: '酒品'
        },
        {
          value: 10,
          label: '服装'
        },
        {
          value: 11,
          label: '汽配'
        },
        {
          value: 12,
          label: '数码'
        },
        {
          value: 13,
          label: '夜宵烧烤'
        },
        {
          value: 14,
          label: '水产'
        },
        {
          value: 15,
          label: '百货'
        },
        {
          value: 16,
          label: '成人'
        },
        {
          value: 17,
          label: '火锅'
        },
        {
          value: 99,
          label: '其他'
        }
      ],
      provinceList: [],
      cityList: [],
      areaList: [],
      dataForm: {
        shopId: '',
        shopStatus: '',
        shopName: '',
        shopOwner: '',
        mobile: '',
        tel: '',
        shopLat: '',
        shopLng: '',
        province: '',
        city: '',
        area: '',
        shopAddress: '',
        intro: '',
        shopLogo: '',
        identityCardFront: '',
        identityCardLater: '',
        businessLicense: '',
        areaId: null,
        cityId: null,
        provinceId: null,
        category: []
      },
      show: false,
      center: {
        lng: 113.327955,
        lat: 23.136783
      },
      zoom: 18,
      offlineEventHandleVisible: false
    }
  },
  components: {
    ImgUpload,
    PicUpload,
    OfflineEventHandle
  },
  created: function () {
    this.init()
    this.$http({
        url: this.$http.adornUrl(`/admin/area/list`),
        method: 'get',
        params: this.$http.adornParams({
           areaName:'' ,
        parentId:420000
        })
      })
  },
  methods: {
    init () {
      this.visible = true
      this.show = false
      this.setCenter()
      this.getDataInfo()
    },
    // 获取店铺详情数据
    getDataInfo () {
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.$refs.dataForm.resetFields()
        this.cityList = []
        this.areaList = []
        this.dataForm.provinceId = null
        this.dataForm.cityId = null
        this.dataForm.areaId = null
      })
      this.listAreaByParentId().then(({ data }) => {
        this.provinceList = data
      })
      this.$http({
        url: this.$http.adornUrl('/shop/shopDetail/info'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.dataForm.shopId = data.shopId
        this.dataForm.shopStatus = data.shopStatus
        this.dataForm.shopName = data.shopName
        this.dataForm.shopOwner = data.shopOwner
        this.dataForm.mobile = data.mobile
        this.dataForm.tel = data.tel
        this.dataForm.shopLat = data.shopLat
        this.dataForm.shopLng = data.shopLng
        this.dataForm.shopAddress = data.shopAddress
        this.dataForm.intro = data.intro
        this.dataForm.shopLogo = data.shopLogo
        this.dataForm.identityCardFront = data.identityCardFront
        this.dataForm.identityCardLater = data.identityCardLater
        this.dataForm.businessLicense = data.businessLicense
        this.dataForm.areaId = data.areaId
        this.dataForm.cityId = data.cityId
        this.dataForm.provinceId = data.provinceId
        this.dataForm.category = data.category?data.category.split(',').map(Number):[]
        this.listAreaByParentId(data.provinceId).then(({ data }) => {
          this.cityList = data
        })
        this.listAreaByParentId(data.cityId).then(({ data }) => {
          this.areaList = data
          this.center = {
            lng: this.dataForm.shopLng,
            lat: this.dataForm.shopLat
          }
        })
      })
    },
    listAreaByParentId (pid) {
      if (!pid) pid = 0
      return this.$http({
        url: this.$http.adornUrl(`/admin/area/listByPid`),
        method: 'get',
        params: this.$http.adornParams({ pid })
      })
    },
    // 选择省
    selectProvince (val) {
      this.dataForm.cityId = null
      this.dataForm.city = ''
      // 获取城市的select
      this.listAreaByParentId(val).then(({ data }) => {
        this.cityList = data
      })
      this.mapLocation(true)
    },
    // 选择市
    selectCity (val) {
      this.dataForm.areaId = null
      this.dataForm.area = ''
      // 获取区的select
      this.listAreaByParentId(val).then(({ data }) => {
        this.areaList = data
      })
      this.mapLocation(true)
    },
    // 重置表单
    // resetForm () {
    //   this.$refs.dataForm.resetFields()
    // },
    // 表单提交
    dataFormSubmit () {
      this.getAreaName()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/shop/shopDetail'),
            method: 'put',
            data: this.$http.adornData({
              shopName: this.dataForm.shopName,
              shopOwner: this.dataForm.shopOwner,
              mobile: this.dataForm.mobile,
              tel: this.dataForm.tel,
              shopLat: this.center.lat,
              shopLng: this.center.lng,
              area: this.dataForm.area,
              city: this.dataForm.city,
              province: this.dataForm.province,
              shopAddress: this.dataForm.shopAddress,
              intro: this.dataForm.intro,
              shopLogo: this.dataForm.shopLogo,
              identityCardFront: this.dataForm.identityCardFront,
              identityCardLater: this.dataForm.identityCardLater,
              businessLicense: this.dataForm.businessLicense,
              areaId: this.dataForm.areaId,
              cityId: this.dataForm.cityId,
              provinceId: this.dataForm.provinceId,
              category: this.dataForm.category.join(',')
            })
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList', this.page)
              }
            })
          })
        }
      })
    },
    /**
     * 初始化地图数据
     */
    setCenter () {
      this.center = null
      this.center = {
        lng: 113.327955,
        lat: 23.136783
      }
      this.zoom = 18
    },
    /**
     * 获取地图移动后回调的位置参数
     */
    syncCenterAndZoom (e) {
      this.setCenter()
      const { lng, lat } = e.target.getCenter()
      this.center.lng = lng
      this.center.lat = lat
      this.zoom = e.target.getZoom()
    },
    /**
     * 通过省市区id,得到省市区名称
     */
    getAreaName () {
      for (let i = 0; i < this.provinceList.length; i++) {
        if (this.provinceList[i].code === this.dataForm.provinceId) {
          // 将省名字保存起来
          this.dataForm.province = this.provinceList[i].areaName
        }
      }
      for (let i = 0; i < this.cityList.length; i++) {
        if (this.cityList[i].code === this.dataForm.cityId) {
          // 将市名字保存起来
          this.dataForm.city = this.cityList[i].areaName
        }
      }
      for (let i = 0; i < this.areaList.length; i++) {
        if (this.areaList[i].code === this.dataForm.areaId) {
          // 将市名字保存起来
          this.dataForm.area = this.areaList[i].areaName
        }
      }
    },
    // 定位地图
    mapLocation (isTrue) {
      let area = ''
      this.getAreaName()
      if (isTrue === true) {
        // 选择省市区时，定位地图
        if (this.dataForm.provinceId) {
          area = this.dataForm.province
          this.zoom = 6
          if (this.dataForm.cityId) {
            area = area + this.dataForm.city
            this.zoom = 10
            if (this.dataForm.areaId) {
              area = area + this.dataForm.area
              this.zoom = 14
              if (this.dataForm.addr !== null) {
                // 详细地址
                area = this.dataForm.province + this.dataForm.city + this.dataForm.area + this.dataForm.addr
                this.zoom = 18
              }
            }
          }
        }
      } else {
        this.center = null
        if (!this.dataForm.provinceId || !this.dataForm.cityId || !this.dataForm.areaId) {
          this.dataForm.shopAddress = null
          this.errorMsg('请先选择省市区')
          return
        }
        area = this.dataForm.province + this.dataForm.city + this.dataForm.area + this.dataForm.shopAddress
        this.zoom = 18
      }
      this.center = area
    },
    /**
     * 关闭地图说明标签
     */
    infoWindowClose () {
      this.show = false
    },
    /**
     * 打开地图说明标签
     */
    infoWindowOpen () {
      this.show = true
    },
    // 下线管理
    auditEventHandle () {
      this.offlineEventHandleVisible = true
      this.$nextTick(() => {
        this.$refs.offlineEvent.init(this.dataForm.shopId)
      })
    },
    // 刷新数据
    refreshChange () {
      this.getDataInfo()
    }
  }
}
</script>
<style lang="scss" >
.mod-shopInfo-add-or-update {
  margin: 0px auto;

  #shopForm {
    //border:1px blue solid;
    width: 1000px;
    margin: 0px auto;
  }

  .title {
    margin-bottom: 20px;
    font-size: 20px;
    text-align: center;
  }

  .shop {
    width: 500px;
  }

  // .upload-logo .pic-uploader-component,
  // .upload-logo .pic-uploader-component > .el-upload,
  // .upload-logo .pic-uploader-component > .el-upload > img {
  //   width: 300px !important;
  //   height: 100px !important;
  // }

  .idcard-width {
    width: 40% !important;
  }
  .idcard .pic-uploader-component,
  .idcard .pic-uploader-component .el-upload--text > img {
    width: 260px !important;
    height: 150px !important;
  }

  .license,
  .license .pic-uploader-component .el-upload > img {
    height: 220px !important;
  }
}
.map {
  width: 100%;
  height: 500px;
}
</style>
