<template>
  <div class="sendScoped">
    <div class="checkedBox">
      <div class="item" v-for="(item,index) in checkList" :key="index" @click="checkBtn(item)">
        <div class="left">
          <icon type="success" size="18" v-if="item.value == sendScope"/>
          <icon type="circle" size="18" v-if="item.value != sendScope" />
          <div class="title">{{ item.name }}</div>
        </div>
        <div class="right" v-if="item.value != 0">
          <div v-if="item.value == '1' && deptSelect.length">已选择{{ deptSelect.length }}个部门</div>
          <div v-if="item.value == '2' && girdSelect.length">已选择{{ girdSelect.length }}个网格</div>
          <u-icon name="arrow-right" color="#CCD0D3" ></u-icon>
        </div>
      </div>
    </div>
    <div class="btn">
      <div class="submitBtn" @click="submit">确定</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'sendScoped',
  data() {
    return {
      value: '',
      checkList: [
        { name: '全部居民群', value: '0'},
        { name: '按部门选择', value: '1'},
        { name: '按网格选择', value: '2'}
      ],
      showContent: false,
      sendType: '',
      deptSelect: [],
      girdSelect: [],
      sendScope: 0,
      girdListIds: [],
      deptListIds: [],
      wxGroups: [],  // 居民群,居民,朋友圈
      sex: '',
      ResidentTags: [],
      ResidentTagsRemove: [],
      circleTags: [],
      circleTagsRemove: [],
      startTime: '',
      endTime: '',
    }
  },
  methods: {
    checkBtn(e) {
      if(e.value == '1' || e.value == '2') {
        uni.navigateTo({url: `./scopedSelect?type=${e.value}&sendType=${this.sendType}`})
      }
      this.sendScope = e.value;
      uni.setStorageSync('sendScope', this.sendScope)
    },
    submit() {
      this.$loading()
      if(this.sendType == 'ResidentsGroup') {
        this.getWxGroups()
      }
      if(this.sendType == 'Residents' || this.sendType == 'CircleOfFriends') {
        this.getSendScope()
      }
      uni.setStorageSync('sendScope', this.sendScope)
      
    },
    // 群发居民群
    getWxGroups() {
      uni.showLoading({title: '加载中'})
      this.$http.post(`/app/appmasssendingtask/queryWxGroups?sendScope=${this.sendScope}`,
      {
        filterCriteria: this.girdListIds.join(',') || this.deptListIds.join(',') || ''
      }).then(res => {
        if (res.code === 0) {
          this.wxGroups = res.data
          uni.hideLoading()
          uni.setStorageSync('wxGroupsUser', this.wxGroups)
        }
      }).then(()=> {
        uni.hideLoading()
        uni.navigateBack()
      }).finally(()=> {
        uni.hideLoading()
      })
    },
    // 群发朋友圈、居民
    getSendScope() {
      uni.showLoading({title: '加载中'})
      let formData = {}
      if(this.sendScope == 0) {
        formData = {
          sendScope: this.sendScope,
          taskType: this.sendType == 'Residents'? 1:0,
        }
      } else if(this.sendScope == 1 || this.sendScope == 2) {
        let resTags='', cirTags='', resRemove='', cirRemove=''
        if(this.ResidentTags.length) {
          resTags = this.ResidentTags?.map(e=> e.id).toString()
        } else if(this.circleTags.length) {
          cirTags = this.circleTags?.map(e=> e.id).toString()
        } else if(this.ResidentTagsRemove.length) {
          resRemove = this.ResidentTagsRemove?.map(e=> e.id).toString() || ''
        } else if(this.circleTagsRemove.length) {
          cirRemove = this.circleTagsRemove?.map(e=> e.id).toString() || ''
        }
        
        formData = {
          filterCriteria: this.girdListIds.toString() || this.deptListIds.toString() || '',
          sendScope: this.sendScope,
          tags: resTags || cirTags,
          excludeTags: resRemove || cirRemove,
          addFromTime: this.startTime,
          addEndTime: this.endTime,
          gender: this.sex,
          taskType: this.sendType == 'Residents'? 1:0,
        }
      }
      this.$http.post(`/app/whchatmomentstask/getSendScope`,{...formData}).then(res => {
        if (res.code === 0) {
          if(this.sendType == 'Residents') {
            this.wxGroups = res.data
          } else if(this.sendType == 'CircleOfFriends') {
            this.wxGroups = res.data
          }
          uni.setStorageSync('wxGroupsUser', this.wxGroups)
        }
      }).then(()=> {
        uni.hideLoading()
        uni.navigateBack()
      }).finally(()=> {
        uni.hideLoading()
      })
    }
  },
  onLoad(o) {
    this.sendType = o.type;
    document.title = "选择发送范围"
  },
  onShow() {
    this.sendScope = uni.getStorageSync('sendScope') || ''
    // 部门、网格
    const girdArr = uni.getStorageSync('girdSelect')
    if(girdArr.length) {
      this.girdListIds = girdArr.map(e=>e.id)
    }
    const deptArr = uni.getStorageSync('deptList')
    if(deptArr.length) {
      this.deptListIds = deptArr.map(v=>v.id)
    }
    // 性别
    this.sex = uni.getStorageSync('gender')
    // 标签
    if(this.sendType == 'Residents') { 
      this.ResidentTags = uni.getStorageSync('ResidentTags') // 居民包含
      this.ResidentTagsRemove = uni.getStorageSync('ResidentTagsRemove') // 居民剔除
    } else if(this.sendType == 'CircleOfFriends') {
      this.circleTags = uni.getStorageSync('circleTags') // 朋友圈包含
      this.circleTagsRemove = uni.getStorageSync('circleTagsRemove') // 朋友圈剔除
    }
    // 时间
    this.startTime = uni.getStorageSync('startTime')
    this.endTime = uni.getStorageSync('endTime')
    
  }
}
</script>
<style lang="scss" scoped>
.sendScoped {
  .checkedBox {
    background: #FFF;
    padding: 0 32px;
    box-sizing: border-box;
    .item {
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #DDD;
      padding: 30px 0;
      box-sizing: border-box;
      .left,
      .right {
        display: flex;
        align-items: center;
        .title {
          margin-left: 12px;
        }
      }
    }
  }
  


  .btn {
    position: fixed;
    bottom: 0;
    left: 0;
    background: #FFF;
    display: flex;
    width: 100%;
    height: 128px;
    padding: 24px 32px;
    box-sizing: border-box;

    .submitBtn {
      flex: 1;
      height: 80px;
      line-height: 80px;
      text-align: center;
      border-radius: 8px;
      border: 2px solid #CCCCCC;
      background: #1365DD;
      color: #FFF;
    }
  }
}
</style>