<template>
  <section class="chooseUser">
    <div class="select-content">
      <div class="area-flex">
        <p class="title">地区</p>
        <AiAreaPicker v-model="areaList" multiple class="value" extra>
          <span class="label" v-if="areaList.length">已选择</span>
          <span v-else style="color:#999;">请选择</span>
          <u-icon name="arrow-right" color="#999" size="24" style="margin-left:8px;"/>
        </AiAreaPicker>
      </div>
      <div class="area-flex">
        <p class="title">部门</p>
        <div class="value">
          <AiPagePicker type="dept" :selected.sync="deptList" nodeKey="id" :isRequire="0">
            <AiMore v-model="moreTextDept"/>
          </AiPagePicker>
        </div>
      </div>
      <div class="area-flex">
        <p class="title">人员</p>
        <div class="value">
          <AiPagePicker type="sysUser" :selected.sync="userList" action="/app/wxcp/wxuser/list?status=1" nodeKey="id" :isRequire="0">
            <AiMore v-model="moreText"/>
          </AiPagePicker>
        </div>
      </div>

      <div class="type-content">
        <div class="type-list" v-for="(item, index) in tagList" :key="index">
          <p>{{ item.name }}</p>
          <div class="list">
            <div class="item" :class="items.isCheck ? 'active' : ''" v-for="(items, indexs) in item.tagList" :key="indexs" @click="typeClick(index, indexs)">
              {{ items.name }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="bg-144"></div>
    <div class="footer">
      <div class="confirm" @click="confirm">确定</div>
    </div>
  </section>
</template>
<script>
import {mapState} from 'vuex'

export default {
  name: "chooseUser",
  data() {
    return {
      value: '',
      areaId: '',
      areaName: '',
      areaList: [],
      tagList: [],
      tagIdList: [],
      userList: [],
      deptList: [],
    }
  },
  computed: {
    ...mapState(['user']),
    moreText() {
      if (this.userList.length) return '已选择'
    },
    moreTextDept() {
      if (this.deptList.length) return '已选择'
    },
  },
  methods: {
    getTagList() {
      this.$http.post("/app/wxcp/wxcorptag/listAll?size=100").then(res => {
        if (res?.code == 0) {
          res.data.records.map((item) => {
            item.tagList.map((items) => {
              items.isCheck = false
              if (this.tagIdList.includes(items.id)) {
                items.isCheck = true
              }
            })
          })
          this.tagList = res.data.records
        }
      })
    },
    typeClick(index, indexs) {
      this.tagList[index].tagList[indexs].isCheck = !this.tagList[index].tagList[indexs].isCheck
    },
    confirm() {
      // if(!this.areaList.length) {
      //   return this.$u.toast('请选择地区')
      // }
      this.tagIdList = []
      this.tagList.map((item) => {
        item.tagList.map((items) => {
          if (items.isCheck) {
            this.tagIdList.push(items.id)
          }
        })
      })
      // if(!this.tagIdList.length) {
      //   return this.$u.toast('请选择标签')
      // }
      var userIdList = [], deptIdList = []
      this.userList.map((item) => {
        userIdList.push(item.id)
      })
      this.deptList.map((item) => {
        deptIdList.push(item.id)
      })
      uni.$emit('selectTag', {
        areaIdList: this.areaList,
        tagIdList: this.tagIdList,
        userList: userIdList,
        deptList: deptIdList
      })
      uni.navigateBack({})
    },
    toSelectDept() {
      uni.navigateTo({url: `./selectDept`})
    },
  },
  onShow() {
    document.title = '人员选择'
  },
  onLoad(option) {
    console.log(option)
    this.areaId = this.user.areaId
    this.areaName = this.user.areaName
    if (option.areaList) {
      this.areaList = option.areaList.split(',')
    }
    if (option.tagIdList) {
      this.tagIdList = option.tagIdList.split(',')
    }
    if (option.userList) {
      var list = option.userList.split(',')
      list.map(item => {
        var info = {
          id: item
        }
        this.userList.push(info)
      })

    }
    if (option.deptList) {
      var list = option.deptList.split(',')
      list.map(item => {
        var info = {
          id: item
        }
        this.deptList.push(info)
      })
    }
    this.getTagList()
  },
}
</script>
<style lang="scss" scoped>
.chooseUser {
  .select-content {
    .area-flex {
      display: flex;
      justify-content: space-between;
      padding: 32px;
      line-height: 44px;
      margin-bottom: 16px;
      background-color: #fff;

      .title {
        width: 150px;
        margin-bottom: 0;
      }

      .value {
        width: calc(100% - 150px);
        text-align: right;
      }
    }

    .area-content {
      padding: 16px 32px 32px;
      border-bottom: 1px solid #ddd;

      .area-item {
        display: inline-block;
        margin-right: 16px;

        .u-icon {
          margin-left: 4px;
        }
      }
    }

    .title {
      line-height: 44px;
      margin-bottom: 16px;
      font-size: 32px;
      color: #666;

      .tips {
        font-size: 34px;
        color: #f46;
        vertical-align: middle;
      }
    }

    .local-icon {
      width: 32px;
      height: 32px;
      margin-right: 4px;
    }

    .type-list {
      padding-left: 16px;
      margin-bottom: 16px;
      padding: 34px 32px 32px;
      background-color: #fff;

      p {
        line-height: 44px;
        margin-bottom: 24px;
        font-size: 30px;
        color: #666;
      }

      .list {
        overflow: hidden;

        .item {
          padding: 12px 32px;
          float: left;
          font-size: 28px;
          font-family: PingFangSC-Regular, PingFang SC;
          color: #333;
          line-height: 40px;
          background-color: #F3F4F7;
          border-radius: 4px;
          margin: 0 16px 16px 0;
        }

        .active {
          background-color: #3192F4;
          color: #fff;
        }
      }
    }
  }

  .bg-144 {
    height: 144px;
  }

  .footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 112px;
    line-height: 112px;
    background: #fff;
    display: flex;
    font-size: 36px;
    font-family: PingFangSC-Regular, PingFang SC;

    .confirm {
      color: #fff;
      background: #1365dd;
    }

    div {
      flex: 1;
      text-align: center;
      color: #333;
    }
  }

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

}
</style>
