<template>
  <div class="Add">
    <u-navbar back-icon-color="#000" title="事件添加" title-color="#000" title-width="300" title-size="32" 
      :title-bold="true" :background="backgroundNavbar" :is-fixed="true" height="44"></u-navbar>
    <div class="header-content-bg">
      <img src="https://cdn.sinoecare.com/i/2024/07/12/6690a1303d423.png" alt="">
    </div>
    <div class="contents">
      <u-form :model="forms" ref="uForm" label-width="auto">
        <div class="border-r">
          <u-form-item label="事件类型" prop="groupName" required :border-bottom="false" right-icon="arrow-right" class="border">
            <span @click="show = true" class="right-span" :style="forms.groupName ? '' : 'color:#999;'">{{ forms.groupName || '请选择事件类型' }}</span>
            <u-select v-model="show" :list="myList" value-name="id" label-name="groupName" @confirm="selectStatus"></u-select>
          </u-form-item>
          <u-form-item label="事件描述" prop="content" required :border-bottom="false" label-position="top" class="form-contents">
            <u-input v-model="forms.content" placeholder="请输入事件描述..." type="textarea" auto-height height="100" maxlength="500"/>
          </u-form-item>
        </div>

        <div class="border-r">
          <u-form-item label="上报位置" prop="mapInfo" required :border-bottom="false" right-icon="arrow-right" class="border">
            <span @click="chooseAddress" class="right-span" :style="forms.address ? '' : 'color:#999;'">{{ forms.address || '请选择上报位置' }}</span>
          </u-form-item>
          <u-form-item label="所属网格" prop="girdName" required :border-bottom="false">
            <AiPagePicker type="gird" @select="confirmGird" valueObj nodeKey="id" :params="{formType:2, axiosUrl:'/app/appgirdmemberinfo/queryMyGirdList'}" class="right-span">
              <AiMore v-model="forms.girdName" placeholder="请选择所属网格"/>
            </AiPagePicker>
          </u-form-item>
        </div>

        <div class="border-r">
          <u-form-item label="图片上传(最多9张)" prop="files" :border-bottom="false" class="avatars" label-position="top">
            <AiUploader :def.sync="forms.files" multiple placeholder="上传图片" :limit="9" action="/admin/file/add2"></AiUploader>
          </u-form-item>
        </div>

        <div class="border-r">
          <u-form-item label="姓名" prop="name" required :border-bottom="false" class="border">
            <u-input v-model="forms.name" placeholder="请输入姓名" />
          </u-form-item>
          <u-form-item label="联系方式" prop="phone" required :border-bottom="false" >
            <u-input v-model="forms.phone" placeholder="请输入联系方式" />
          </u-form-item>
        </div>
      </u-form>
    </div>

    <!-- <div class="btn" @click="confirm">
      <span>提交</span>
    </div> -->
    <div class="footer" @click="confirm">
      <div class="btn">保存</div>
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: 'Add',
  customNavigation: true,
  data() {
    return {
      forms: {
        groupName: '',
        groupId: '',
        content: '',
        mapInfo: {address: ''},
        girdId: '',
        girdName: '',
        files: [],
        name: '',
        phone: '',
        girdMemberId: '',
        girdMemberName: '',
        address: '',
        lat: '',
        lng: ''
      },
      flag: false,
      show: false,
      myList: [],
      backgroundNavbar: {
        background: 'url(https://cdn.sinoecare.com/i/2024/07/12/6690a1309c7d3.png) no-repeat',
        backgroundSize: 'cover',
      },
    }
  },
  computed: {
    ...mapState(['user']),
  },
  onLoad() {
    this.typeList()
    this.forms.girdId = this.user.girdInfos2G[0].girdId
    this.forms.girdName = this.user.girdInfos2G[0].girdName
    // this.forms.girdMemberId = this.user.girdMemberId
    // this.forms.girdMemberName = this.user.name
  },
  onShow() {
    this.forms.name = this.user.name
    this.forms.phone = this.user.phone
  },
  methods: {
    chooseAddress() {
      uni.authorize({
        scope: 'scope.userLocation',
        success: () => {
          uni.chooseLocation({
            success: res => {
              this.forms.address = res.address
              this.forms.lat = res.latitude
              this.forms.lng = res.longitude
            }
          })
        },
        fail: () => {
          this.$dialog.confirm({
            content: '您未授权定位权限，无法选择位置'
          }).then(() => {
            wx.openSetting({
              success: res => {
                if (!res.authSetting['scope.userLocation']) {
                  this.$dialog.alert({
                    content: '您未授权定位权限，无法选择位置'
                  }).then(() => {
                  })
                } else {
                  console.log('设置定位权限')
                }
              }
            })
          })
        }
      })
    },
    typeList() {
      this.$instance.post(`/app/apppatrolreportgroupv2/list?current=1&size=9999`).then((res) => {
        if (res.code == 0) {
          this.myList = res.data.records
        }
      })
    },
    selectStatus(e) {
      this.forms.groupName = e[0].label
      this.forms.groupId = e[0].value
    },
    confirmGird(v) {
      this.forms.girdId = v.id
      this.forms.girdName = v.girdName
    },
    toMap() {
      uni.navigateTo({url: `./Map`})
    },
    confirm() {
      if(this.flag) return

      if (!this.forms.groupName) {
        return this.$u.toast('请选择事件类型')
      }
      if (!this.forms.content) {
        return this.$u.toast('请输入事件描述')
      }
      if (!this.forms.address) {
        return this.$u.toast('请选择上报位置')
      }
      if (!this.forms.girdName) {
        return this.$u.toast('请选择所属网格')
      }
      if (!this.forms.name) {
        return this.$u.toast('请输入姓名')
      }
      if (!this.forms.phone) {
        return this.$u.toast('请输入手机号')
      }
      this.flag = true
      this.$instance.post(`/app/apppatrolreportinfov2/addByGirdMember`, {...this.forms}).then((res) => {
        if (res.code == 0) {
          this.$u.toast('事件添加成功')
          uni.$emit('getListInit')
          setTimeout(() => {
            uni.navigateBack()
          }, 600)
        }
      })

    },

  },
}
</script>

<style scoped lang="scss">
.Add {
  height: 100%;
  .header-content-bg {
    width: 100%;
    position: fixed;
    z-index: -1;
    img {
      width: 100%;
      height: 592px;
    }
  }
  .contents {
    width: calc(100% - 64px);
    margin: 16px 0 0 32px;
    padding-bottom: 252px;
    .border-r {
      border-radius: 16px;
      margin-bottom: 32px;
      background-color: #fff;
    }
    ::v-deep .u-form {
      .u-form-item {
        padding-left: 32px;
        padding-right: 32px;

        .u-form-item__body {
          .u-form-item--right__content__slot {
            padding-bottom: 0;

            .u-input {
              text-align: right !important;
            }
          }
        }
      }
      .form-contents {
        padding-bottom: 20px !important;
        .u-form-item__body {
          .u-form-item--right__content__slot {
            .u-input {
              text-align: left !important;
            }
          }
        }
      }
      .avatars {
        padding-bottom: 20px !important;
      }
    }
    ::v-deep .u-form-left__content--required {
      font-size: 32px!important;
    }
  }
  .border {
    ::v-deep .u-form-item {
      border-bottom: 1px solid #ddd;
    }
  }

  .footer {
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;
    background-color: #F4F5FA;
    padding: 64px 64px 68px 64px;
    z-index: 9;
    .btn {
      width: calc(100% - 128px);
      line-height: 88px;
      background: #026AF2;
      border-radius: 44px;
      font-family: PingFangSC-Medium;
      font-weight: 500;
      font-size: 34px;
      color: #FFF;
      text-align: center;
    }
  }

  .right-span {
    display: inline-block;
    width: 100%;
    text-align: right;

    ::v-deep .u-icon__label {
      font-size: 28px!important;
    }

    ::v-deep .uicon-arrow-right{
      color: #c0c4cc!important;
    }
  }
  ::v-deep .uni-input-placeholder {
    color: #999!important;
    font-size: 28px!important;
  }
}
</style>
