<template>
  <div class="SelectGird">
    <div class="header-middle">
      <div class="hint">
        <span v-for="(item, index) in selectList" :key="index">
          <span v-if="index" style="margin:0 4px;" v-text="`/`"/>
          <span style="color:#3F8DF5" @click="girdNameClick(item, index)" v-text="item.girdName"/>
        </span>
      </div>
      <div class="showTypes">
        <div v-if="options.length > 0">
          <div class="cards" v-for="(item, index) in options" :key="index" @click="itemClick(item)">
            <div class="imges">
              <img src="./img/xzh.png" alt="" class="imgselect" v-if="item.isChecked"
                   @click.stop="girdClick(item, index)"/>
              <img src="./img/xz.png" alt="" class="imgselect" v-else @click.stop="girdClick(item, index)"/>
              <img src="./img/gird--select-icon.png" alt="" class="avatras"/>
            </div>
            <div class="rightes fill">
              <div class="applicationNames fill">{{ item.girdName }}</div>
            </div>
          </div>
        </div>
        <AiEmpty :description="`暂无数据`" class="emptyWrap" v-else/>
      </div>
    </div>
    <div class="subBtn flex">
      <div v-if="clearable" class="cancel" @click="cancel">清空</div>
      <div @click="submit">确定选择</div>
    </div>
  </div>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: 'selectGird',
  appName: "网格选择",
  data() {
    return {
      SelectGird: {},
      allData: null,
      options: [],
      selectList: [],
      parentGirdId: '',
      query: {}
    }
  },
  computed: {
    ...mapState(['user']),
    //是否展示所有网格(随手拍)
    isApply: v => v.query?.formType == 2,
    clearable: v => v.query?.clearable,
    selected: v => [v.query?.selected].flat(),
  },
  onLoad(query) {
    this.query = query
    this.getAllGrids()
  },
  methods: {
    getAllGrids() {
      this.selectList = []
      let url = `/app/appgirdinfo/listByInfo`
      this.$instance.post(url).then(res => {
        if (res?.data) {
          let parents = res.data?.map(e => e.parentGirdId)
          this.allData = res.data?.map(e => ({...e, hasChildren: parents.includes(e.id)}))
          this.treeInit()
        }
      })
    },
    treeInit(isClick) {
      let last = uni.getStorageSync("lastSelectedGrid")
      if (!isClick && last && !this.isApply) {
        this.$instance.post("/app/appgirdinfo/listFatherGirdInfo", null, {
          params: {girdId: last}
        }).then(res => {
          if (res?.data) {
            this.selectList = [{girdName: '可选范围', id: ''}, res.data.filter(e => !!this.allData.find(a => a.id == e.id))].flat()
            this.getGridsByGridMemberAndParent({id: last})
          }
        })
      } else {
        this.options = this.allData.filter((e, i, arr) => !arr?.map(e => e.id).includes(e.parentGirdId))
        this.options?.map((item) => item.isChecked = this.selected.includes(item.id))
        let obj = {girdName: '可选范围', id: ''}
        this.selectList.push(obj)
      }
    },
    itemClick(row) {
      if (row.hasChildren) {
        let obj = {
          girdName: row.girdName,
          id: row.id,
        }
        this.selectList.push(obj)
        this.getGridsByGridMemberAndParent(row)
      }
    },
    getGridsByGridMemberAndParent(row) {
      let {id: parentGirdId} = row
      this.options = this.allData.filter(e => e.parentGirdId == parentGirdId)
      this.options?.map((item) => item.isChecked = this.selected.includes(item.id))
    },
    girdNameClick(row, index) {
      if (!index) { //第一级别
        this.selectList = []
        this.treeInit(true)
      } else {
        this.selectList.splice(index, 8)
        this.getGridsByGridMemberAndParent(row)
      }
    },
    girdClick(row, index) {
      if (this.options[index].isChecked) {//取消
        this.options[index].isChecked = false
        this.SelectGird = {}
      } else {
        this.options?.map((item) => {
          item.isChecked = false
        })
        this.options[index].isChecked = true
        this.SelectGird = row
      }
      this.$forceUpdate()
    },
    submit() {
      if (this.SelectGird.id != null) {
        if (!this.isApply) {
          uni.setStorageSync("lastSelectedGrid", this.SelectGird.parentGirdId)
        }
        uni.navigateBack({
          success: () => {
            uni.$emit("pagePicker:gird", this.SelectGird)
          }
        })
      } else {
        return this.$u.toast('请选择网格')
      }
    },
    cancel() {
      this.SelectGird = {}
      uni.navigateBack({
        success: () => {
          uni.$emit("pagePicker:gird", this.SelectGird)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.SelectGird {
  min-height: 100vh;
  background: #fff;
  padding-bottom: 140px;
  box-sizing: border-box;

  .header-middle {
    .hint {
      padding: 28px 20px 28px 32px;
      line-height: 56px;
      box-shadow: 0px 1px 0px 0px #e4e5e6;
      font-size: 30px;
      font-weight: 500;
      word-break: break-all;
    }

    .showTypes {
      .cards {
        display: flex;
        align-items: center;
        height: 120px;
        line-height: 120px;
        // background: pink;
        padding: 0 0 0 32px;

        .imges {
          display: flex;
          align-items: center;
          // width: 200px;
          .imgselect {
            width: 48px;
            height: 48px;
            vertical-align: middle;
          }

          .avatras {
            width: 74px;
            height: 74px;
            border-radius: 8px;
            margin-left: 36px;
          }
        }

        img {
          width: 74px;
          height: 74px;
          border-radius: 8px;
        }

        .rightes {
          display: flex;
          border-bottom: 1px solid #e4e5e6;
          padding: 0 16px;

          .applicationNames {
            display: inline-block;
            font-size: 36px;
            font-weight: 500;
            color: #333333;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            vertical-align: bottom;
          }
        }
      }
    }
  }

  .subBtn {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 118px;
    background: #f4f8fb;
    justify-content: flex-end;

    div {
      width: 192px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      border: 2px solid #1365dd;
      background: #1365dd;
      border-radius: 4px;
      font-size: 32px;
      color: #fff;
      margin-right: 32px;

      &.cancel {
        color: #333;
        background: #fff;
        border-color: #ddd;
      }
    }
  }
}
</style>
