<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :top-list="topList"
      :moren="moren"
    />
    <div class="thetopbox">
      <el-form ref="form" :model="queryParams" label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'结算期数'" prop="memberSettlePeriodId">
              <el-select
                v-model="queryParams.memberSettlePeriodId"
                :placeholder="'请选择'"
                @change="getChildNodeList"
              >
                <el-option
                  v-for="item in memberSettlePeriodList"
                  :key="item.pkId"
                  :label="item.settleDate"
                  :value="item.pkId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'会员编号'" prop="memberCode">
              <el-input
                v-model="queryParams.memberCode"
                clearable
                @blur="getChildNodeList"
              />
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item label="阶段" prop="stage">
              <el-select
                v-model="queryParams.stage"
                placeholder="请选择"
                @change="getChildNodeList"
              >
                <el-option
                  v-for="(item, index) in levelList"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="状态" prop="stageStatus">
              <el-select
                v-model="queryParams.stageStatus"
                :placeholder="'请选择'"
                @change="getChildNodeList"
              >
                <el-option
                  v-for="(item, index) in statusEnum"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="子点位" prop="childNode">
              <el-select v-model="queryParams.childNode" clearable filterable :placeholder="'请选择'">
                <el-option
                  v-for="(item, index) in childNodeList"
                  :key="index"
                  :label="item.childNode"
                  :value="item.childNode"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4" style="margin-left: 30px">
            <div class="searchbox">
              <el-button
                :loading="loading"
                class="searchbtn"
                @click="getSearch"
              > {{ '搜索' }}</el-button>
              <el-button @click="resetHandle"> {{ '重置' }}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="lefttop">
      <div v-for="(item, index) in avaerInfoList" :key="index" class="single">
        <!-- <img :src="item.value" alt=""> -->
        <!-- <div class="singletitle">{{ item.name }}</div> -->
      </div>
    </div>
    <div class="tree">
      <div class="tree-content" @wheel="handleWheel" @mousedown.stop="move">
        <div :style="{ transform: `scale(${scale})`, height: '100%' }">
          <vue2-org-tree
            ref="orgTree"
            style="height: 100%"
            :data="data"
            collapsable
            :render-content="renderContent"
            @on-expand="onExpand"
            @on-node-click="NodeClick"
            @on-node-mouseover="onMouseover"
            @on-node-mouseout="onMouseout"
          />
        </div>
      </div>
    </div>
    <!-- </el-scrollbar> -->

    <div v-show="false" ref="copyContent" class="copyContent">
      <div>{{ '会员编号' }}：{{ treeData.memberCode }}</div>
      <div>{{ '会员姓名' }}：{{ treeData.name }}</div>
      <div>{{ '支付日期' }}：{{ treeData.payDate }}</div>
      <div>{{ '业绩' }}({{ isLocals() }})&nbsp;&nbsp;左区&nbsp;&nbsp;右区</div>
      <div>
        真实新增&nbsp;&nbsp;{{ treeData.leftRealNewPv }}&nbsp;&nbsp;{{
          treeData.rightRealNewPv
        }}
      </div>
      <div>
        首购新增&nbsp;&nbsp;{{ treeData.leftFirstPurchaseAdd }}&nbsp;&nbsp;{{
          treeData.rightFirstPurchaseAdd
        }}
      </div>
      <div>
        复购新增{{ treeData.leftRepeatPurchaseAdd }}&nbsp;&nbsp;
        {{ treeData.rightRepeatPurchaseAdd }}
      </div>
      <div>
        真实累计{{ treeData.leftRealTotal }}&nbsp;&nbsp;{{
          treeData.rightRealTotal
        }}
      </div>
      <div>
        首购累计{{ treeData.leftFirstTotal }}&nbsp;&nbsp;{{
          treeData.rightFirstTotal
        }}
      </div>
      <div>
        复购累计 {{ treeData.leftRepeatPurchaseTotal }}&nbsp;&nbsp;{{
          treeData.rightRepeatPurchaseTotal
        }}
      </div>
      <div>
        首购结余{{ treeData.leftFirstSurplus }}&nbsp;&nbsp;{{
          treeData.rightFirstSurplus
        }}
      </div>
      <div>
        复购结余 {{ treeData.leftRepeatPurchaseSurplus }}&nbsp;&nbsp;{{
          treeData.rightRepeatPurchaseSurplus
        }}
      </div>
    </div>
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import {
  getAzFramework,
  getMemberSettlePeriod,
  getChildList
} from '@/api/archityecture'
import { isLocals } from '../../../utils/numberToCurrency'
import tabBarMixin from './mixins/tab-bar'
export default {
  name: 'Azjg',
  components: {
    topBar
  },
  mixins: [tabBarMixin],
  data() {
    return {
      moren: 'azjg',
      queryParams: {
        memberSettlePeriodId: '', // 期数
        memberCode: '', // 会员编号
        stage: 1,
        stageStatus: '',
        childNode: ''
      },
      levelList: [
        { value: 1, label: '一阶段' },
        { value: 2, label: '二阶段' },
        { value: 3, label: '三阶段' }
      ],
      statusEnum: [
        { value: 0, label: '已完成' },
        { value: 1, label: '未完成' }
      ],
      basicSwitch: false,
      basicInfo: { id: null, label: null },
      data: {},
      treeData: {},
      ifShow: false,
      avaerInfoList: [],
      memberSettlePeriodList: [], // 期数
      tzList: [
        { value: 0, label: '是' },
        { value: 1, label: '否' }
      ],
      scale: 1,
      loading: false,
      childNodeList: []
    }
  },
  created() {
    getMemberSettlePeriod().then((res) => {
      if (res.code === 200) {
        this.memberSettlePeriodList = (res.data || []).filter(item => item.isThisDay !== 0)
        this.queryParams.memberSettlePeriodId = this.memberSettlePeriodList[0].pkId
      }
    })
  },
  methods: {
    handleWheel(event) {
      // 暂时禁用滚轮缩放功能
      // event.preventDefault()
      // const delta = Math.sign(event.deltaY)
      // const newScale = this.scale - delta * 0.1
      // this.scale = Math.max(0.01, Math.min(newScale, 10))
      return
    },
    isLocals,
    copyText() {
      const copyContent = this.$refs.copyContent
      const textLines = Array.from(copyContent.querySelectorAll('div')).map(
        (div) => div.textContent.trim()
      )
      const text = textLines.join('\n')
      const tempInput = document.createElement('textarea')
      tempInput.value = text
      document.body.appendChild(tempInput)
      tempInput.select()
      try {
        document.execCommand('copy')
        this.$message({
          message: '复制成功',
          type: 'success'
        })
      } catch (error) {
        this.$message({
          message: '复制失败',
          type: 'warning'
        })
      }
      tempInput.remove()
      window.getSelection().removeAllRanges()
    },

    // 返回
    goback() {
      this.queryParams.memberCode = this.data.parentMemberCode
      this.getSearch()
    },

    // 拖拽移动
    move(e) {
      const odiv = e.currentTarget // 获取元素
      console.log(e)

      // 算出鼠标相对元素的位置
      const disX = e.clientX - odiv.offsetLeft
      const disY = e.clientY - odiv.offsetTop
      document.onmousemove = (e) => {
        // 鼠标按下并移动的事件
        // 用鼠标的位置减去鼠标相对元素的位置，得到元素的位置
        const left = e.clientX - disX
        const top = e.clientY - disY

        // 绑定元素位置到positionX和positionY上面
        this.positionX = top
        this.positionY = left

        // 移动当前元素
        odiv.style.left = left + 'px'
        odiv.style.top = top + 'px'
      }
      document.onmouseup = () => {
        document.onmousemove = null
        document.onmouseup = null
      }
    },

    // 鼠标移入
    onMouseover(e, data) {
      this.basicInfo = data
      this.basicSwitch = true
    },
    // 鼠标移出
    onMouseout(e, data) {
      this.basicSwitch = false
    },
    // 渲染节点
    renderContent(h, data) {
      if (Object.keys(data).length > 0) {
        return (
          <div class='render-content-item'>
            {/* 节点编号 */}
            {data.nodeCode !== '0-root' && (
              <div class='node-code'>

                <span>{data.nodeCode}</span>
              </div>
            )}

            {/* 会员信息或空点位 */}
            <div class='node-content'>
              {data.memberCode && data.memberName ? (
                <div class='member-info'>

                  <div class='member-details'>
                    <div class='member-code'>{data.memberCode}</div>
                    <div class='member-name'>{data.memberName}</div>
                  </div>
                </div>
              ) : (
                <div class='empty-node'>
                  <div class='empty-text'>空点位</div>
                </div>
              )}
            </div>
          </div>)
      } else {
        return null
      }
    },
    // 右键点击
    rightClick(e, data) {
      event.preventDefault()
      this.queryParams.memberCode = data.memberCode
      this.getSearch()
    },

    // 点击节点
    NodeClick(e, data) {},
    // 默认展开
    toggleExpand(data, val) {
      if (Array.isArray(data)) {
        data.forEach((item) => {
          this.$set(item, 'expand', val)
          if (item.children) {
            this.toggleExpand(item.children, val)
          }
        })
      } else {
        this.$set(data, 'expand', val)
        if (data.children) {
          this.toggleExpand(data.children, val)
        }
      }
    },
    collapse(list) {
      list.forEach((child) => {
        if (child.expand) {
          child.expand = false
        }
        child.children && this.collapse(child.children)
      })
    },
    // 展开
    onExpand(e, data) {
    },
    getSearch() {
      console.log(this.queryParams, '...this.')
      if (!this.queryParams.childNode || !this.queryParams.memberCode) {
        this.$message.warning('会员编号和子点位不能为空')
        return
      }
      this.loading = true

      getAzFramework(this.queryParams).then((res) => {
        this.data = res.data[0]
        this.toggleExpand(this.data, true)
      }).finally(() => {
        this.loading = false
      })
    },

    resetHandle() {
      this.queryParams = {
        memberSettlePeriodId: '', // 期数
        memberCode: '', // 会员编号
        stage: 7,
        type: 1
      }
      this.data = {}
      this.childNodeList = []
    },
    getChildNodeList() {
      if (!this.queryParams.memberCode) {
        return
      }
      getChildList(this.queryParams).then((res) => {
        if (res.code === 200) {
          this.childNodeList = res.data || []
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__headerbtn{
  top: 5px;
  right: 10px;
  border: 1px solid #c8c3c3;
  padding: 2px 3px;
  border-radius: 50%;
}
::v-deep .tree-content {
  background: rgba(0, 0, 0, 0) !important;
}
.tree {
  background: #fff;
  // min-height: 100vh;
  min-height: calc(100vh - 110px);
  position: relative;
  text-align: center;
  // overflow-x: scroll;
  width: 100%;
  height: 100%;
  margin: auto;
  // overflow-y: hidden;
  // margin-left: -60px;
  // margin-top: 20px;
  // background: red;
  &-content {
    text-align: center;
    width: 100%;
    height: 100%;
    background: #fff;
    position: absolute;
    top: 0;
    left: 0;
    // overflow: auto;
  }
}
::v-deep .org-tree-node-btn {
  display: none;
}
::v-deep .org-tree-node-label {
  display: block;
}
::v-deep .org-tree-node-label-inner {
  background: transparent !important;

  padding: 0 !important;
  border: none;
  box-shadow: none !important;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: auto;
  //  margin-left: -20px;
  // z-index: -1;
}
::v-deep .el-dialog__body {
  padding: 0;
}
::v-deep .org-tree-container {
  display: block;
  background: rgba(1, 1, 1, 0) !important;
  overflow-x: auto;
}
::v-deep .render-content-item {
  border-radius: 12px;
  margin: auto;
  z-index: 99;
  padding: 0;
  border: none;
  max-width: 200px;
  text-align: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  overflow: hidden;
  position: relative;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  }

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4);
  }

  .node-code {
    background: rgba(255, 255, 255, 0.1);
    padding: 8px 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font-size: 12px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);

    i {
      font-size: 14px;
      color: #ffd700;
    }

    span {
      letter-spacing: 0.5px;
    }
  }

  .node-content {
    padding: 16px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    position: relative;
  }

  .member-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    position: relative;

    .member-avatar {
      width: 48px;
      height: 48px;
      border-radius: 50%;
      background: linear-gradient(135deg, #667eea, #764ba2);
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 4px;
      box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);

      i {
        font-size: 24px;
        color: white;
      }
    }

    .member-details {
      text-align: center;

      .member-code {
        font-size: 13px;
        font-weight: 600;
        color: #2c3e50;
        margin-bottom: 2px;
        letter-spacing: 0.3px;
      }

      .member-name {
        font-size: 12px;
        color: #7f8c8d;
        font-weight: 500;
      }
    }

    .status-indicator.active {
      position: absolute;
      top: -2px;
      right: -2px;
      width: 12px;
      height: 12px;
      background: #27ae60;
      border-radius: 50%;
      border: 2px solid white;
      box-shadow: 0 2px 8px rgba(39, 174, 96, 0.3);
      animation: pulse-green 2s infinite;
    }
  }

  .empty-node {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    opacity: 0.8;
    position: relative;

    .empty-avatar {
      width: 48px;
      height: 48px;
      border-radius: 50%;
      background: linear-gradient(135deg, #bdc3c7, #95a5a6);
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 4px;
      border: 2px dashed #95a5a6;
      transition: all 0.3s ease;

      i {
        font-size: 20px;
        color: white;
      }
    }

    .empty-text {
      font-size: 12px;
      color: #7f8c8d;
      font-weight: 500;
    }

    .status-indicator.inactive {
      position: absolute;
      top: -2px;
      right: -2px;
      width: 12px;
      height: 12px;
      background: #95a5a6;
      border-radius: 50%;
      border: 2px solid white;
      box-shadow: 0 2px 8px rgba(149, 165, 166, 0.3);
    }

    &:hover .empty-avatar {
      background: linear-gradient(135deg, #3498db, #2980b9);
      border-color: #3498db;
      transform: scale(1.05);
    }
  }
}

@keyframes pulse-green {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.7;
    transform: scale(1.1);
  }
}

.lefttop {
  display: flex;
  justify-items: center;
  align-items: center;
  padding-left: 20px;
  background: #ffffff;
  .single {
    margin: 0 5px;
    img {
      width: 36px;
      height: 36px;
      border-radius: 50%;
      // box-shadow: 0px 2px 20px 0px rgba(204, 204, 204, 0.5);
    }
    .singletitle {
      text-align: center;
      font-size: 10px;
      font-family: PingFang SC-Regular, PingFang SC;
    }
  }
}
::v-deep .rendercontent {
  .toprender {
    display: flex;
    border-bottom: 1px solid #cccccc;
    .leftimg {
      border-right: 1px solid #cccccc;
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 10px;
      .img1 {
        width: 75px;
        height: 75px;
        border-radius: 50%;
      }
      .img2 {
        margin-top: 10px;
        width: 50px;
        height: 30px;
        border-radius: 8px;
      }
    }
    .centerbox {
      padding: 10px;
      .lineboex {
        display: flex;
        align-items: center;
        margin: 10px 0;
        .linetitle {
          margin-right: 5px;
          font-size: 10px;
          font-family: MicrosoftYaHei-Bold, MicrosoftYaHei;
          font-weight: bold;
          color: #333333;
        }
        .linecontent {
          font-size: 10px;
          font-family: MicrosoftYaHei;
          color: #333333;
          display: flex;
          justify-content: center;
          align-items: center;
          padding: 10px 0;
          width: 166px;
          height: 20px;
          background: rgba(216, 216, 216, 0.3);
          border-radius: 4px;
          border: 1px solid #cccccc;
        }
      }
    }
    .rightimg {
      flex: 1;
      padding: 10px;
      font-size: 10px;
      display: flex;
      align-items: center;
      flex-direction: column;
      justify-content: center;
      font-family: MicrosoftYaHei-Bold, MicrosoftYaHei;
      font-weight: bold;
      color: #333333;
      text-align: center;
      img {
        width: 75px;
        height: 50px;
        border-radius: 8px;
        margin-bottom: 10px;
      }
    }
  }
  .bottomrender {
    padding: 10px;
    display: flex;
    .flexbox {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      .thetitle {
        font-size: 10px;
        font-family: MicrosoftYaHei-Bold, MicrosoftYaHei;
        font-weight: bold;
        color: #333333;
      }
      .linecontent {
        margin-top: 5px;
        padding: 10px 0;
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #333333;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 110px;
        height: 20px;
        background: rgba(216, 216, 216, 0.3);
        border-radius: 4px;
        border: 1px solid #cccccc;
      }
    }
  }
  .footerbox {
    display: flex;
    padding: 10px;
    justify-content: space-around;
    .footerbtn {
      width: 78px;
      height: 28px;
      border-radius: 5px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      font-family: MicrosoftYaHei;
      color: #ffffff;
      padding: 10px;
      cursor: pointer;
    }
    .btn1 {
      background: #ee1e26;
    }
    .btn2 {
      background: #f37825;
    }
    .btn3 {
      background: #21c8f4;
    }
  }
}

.page {
  background: #ffffff;
  padding: 10px 20px !important;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 20px;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #c8161d;
        color: #ffffff;
      }
    }
  }
}
</style>
