<template>
  <div class="selectDept">
    <AiTopFixed>
      <AiTreePath :node="cursor" :paths="depts.map" :prop="{parent:'parentid'}" @click="changeList"/>
    </AiTopFixed>
    <div class="user-list">
      <template v-if="list.length>0">
        <div class="item" v-for="(item, index) in list" :key="index" flex>
          <div class="select-img" @click="checkClick(item)">
            <img :src="item.isCheck ? checkIcon : cirIcon" alt="">
          </div>
          <div class="user-info fill" flex @click="getCursor(item)">
            <div class="fill" v-text="item.name"/>
            <u-icon v-if="Array.isArray(item.children)" class="mar-r16" name="arrow-right" size="40" color="#999"/>
          </div>
        </div>
      </template>
      <template v-else>
        <AiEmpty/>
        <div class="pad-b118"/>
      </template>
    </div>
    <div class="pad-b118"/>
    <div class="footer">
      <div class="btn" @click="confirm">确定选择</div>
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: "selectDept",
  appName: "选择部门",
  data() {
    return {
      current: 1,
      total: 0,
      name: '',
      list: [],
      depts: [],
      cirIcon: require('./img/xz.png'),
      checkIcon: require('./img/xzh.png'),
      selected: [],
      cursor: {}
    }
  },
  computed: {
    ...mapState(['user']),
    isSingle: v => !!v.$route.query.single,
    nodeKey: v => v.$route.query.nodeKey || "idNumber",
    isRequire: v => v.$route.query.isRequire || 1,
    isAuth: v => !!v.$route.query.isAuth, //是否只能选择自己当前部门及以下
  },
  onLoad(query) {
    console.log(query)
    if (query.selected) {
      this.selected = query.selected?.split(",").map(e => Number(e)) || []
    }
    this.getList()
  },
  methods: {
    getList() {
      var url = this.isAuth ? `/app/wxcp/wxdepartment/listByUser?name=${this.name}` : `/app/wxcp/wxdepartment/listAll?name=${this.name}`
      this.$http.post(url).then(res => {
        if (res?.data) {
          res.data.forEach(e => e.isCheck = this.selected.includes(e[this.nodeKey]))
          this.depts = new this.$tree(res.data, {parent: 'parentid'})
          this.list = this.depts.tree
        }
      })
    },
    checkClick(item) {
      if (this.isSingle && this.isRequire == 1) {
        this.depts.every(e => {
          e.isCheck = item.id == e.id
        })
      } else item.isCheck = !item.isCheck

    },
    confirm() {
      let checkList = []
      this.depts.every((item) => {
        if (item.isCheck) {
          checkList.push(item)
        }
      })
      if (!checkList.length && this.isRequire == 1) {
        return this.$u.toast('请先选择部门')
      } else {
        uni.navigateBack({
          success: () => {
            uni.$emit("pagePicker:dept", checkList)
          }
        })
      }
    },
    changeList(item) {
      if (item == "all") {
        this.cursor = {}
        this.list = this.depts.tree
      } else {
        this.cursor = item
        this.list = item.children
      }
    },
    getCursor(item) {
      if (item.children?.length > 0) {
        this.cursor = item
        this.list = item.children
      }
    }
  },
}
</script>

<style lang="scss" scoped>
.selectDept {
  ::v-deep .AiTopFixed .u-search {
    margin-bottom: 0 !important;
  }

  .pad-b118 {
    padding-bottom: 118px;
  }

  .user-list {
    background-color: #fff;

    .item {
      width: 100vw;

      .select-img {
        display: inline-block;

        img {
          width: 48px;
          height: 48px;
          margin: 12px 36px 12px 30px;
          vertical-align: middle;
        }
      }

      .user-info {
        padding: 20px 0 20px 0;
        height: 100%;
        border-bottom: 1px solid #E4E5E6;
        font-size: 36px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #333;
        line-height: 74px;
      }
    }
  }

  .footer {
    width: 100%;
    height: 118px;
    background: #F4F8FB;
    position: fixed;
    left: 0;
    bottom: 0;
    text-align: right;

    .btn {
      display: inline-block;
      width: 192px;
      height: 80px;
      line-height: 80px;
      background: #1365DD;
      border-radius: 4px;
      text-align: center;
      font-size: 32px;
      font-family: PingFangSC-Regular, PingFang SC;
      color: #FFF;
      margin: 20px 34px 0 0;
    }
  }
}
</style>
