<template>
  <div class="topButton">
    <a-space>
      <a-button @click="save">保存</a-button>
      <a-button @click="reload">刷新</a-button>
      <a-button type="primary" @click="addOne()" class="addBtn"><Icon icon="ant-design:plus-outlined"/></a-button>
    </a-space>
  </div>

  <!-- <div v-for="(one, onei) in dataSource" :key="onei" class="oneCard localCard">
    <a-card :title="onei+1">
      <div style="background: #f0f2f5;">
        <div class="oneInput">
          <a-input v-model:value="one.name"/>
        </div>
        <div v-for="(two, twoi) in one.childrenList" :key="twoi" class="oneCard localCard">
          <a-card :title="twoi+1">
            <div style="background: #f0f2f5;">
              <div class="oneInput">
                <a-input v-model:value="two.name"/>
              </div>
              <div v-for="(three, threei) in two.childrenList" :key="threei" class="oneCard localCard">
                <a-card :title="threei+1">
                  <div style="background: #f0f2f5;">
                    <div class="oneInput">
                      <a-input v-model:value="two.name"/>
                    </div>
                  </div>
                </a-card>
                <a-button v-if="!three.childrenList?.length" type="dashed" block @click="insertThree(two, threei)"><Icon icon="ant-design:arrow-down-outlined"/><Icon icon="ant-design:plus-outlined"/>insertThree</a-button>
              </div>
              <a-button type="dashed" block @click="addThree(two)" class="oenAddBtn"><Icon icon="ant-design:plus-outlined"/>addThree</a-button>
            </div>
          </a-card>
          <a-button v-if="!two.childrenList?.length" type="dashed" block @click="insertTwo(one, twoi)"><Icon icon="ant-design:arrow-down-outlined"/><Icon icon="ant-design:plus-outlined"/>insertTwo</a-button>
        </div>
      </div>
      <a-button type="dashed" block @click="addTwo(one)" class="oenAddBtn"><Icon icon="ant-design:plus-outlined"/>addTwo</a-button>
    </a-card>
    <a-button v-if="!one.childrenList?.length" type="dashed" block @click="insertOne(one, onei)"><Icon icon="ant-design:arrow-down-outlined"/><Icon icon="ant-design:plus-outlined"/>insertOne</a-button>
  </div>
  <a-button type="dashed" block @click="addOne()" class="oenAddBtn"><Icon icon="ant-design:plus-outlined"/>addOne</a-button> -->



  <div class="maxDiv">
    <draggable @move="moveDraggable" @end="endDraggable" v-model="dataSource" item-key="_id">
      <template #item="{ element: one }">
        <div class="box">
          <!-- 下一层 -->
          <a-card>
            <a-collapse v-model:activeKey="oneActiveKey" ghost expandIconPosition="right">
              <a-collapse-panel :key="one._id">
                <template #header>
                  <div class="topDiv">
                    <div>{{ one.sort }}一级标题:</div>
                    <div class="inputd"><a-input :value="one.name" @change="changeInput($event, one, 'name')" @click="stop" class="ainput"/></div>
                  </div>
                </template>
                <template #extra>
                  <a-space>
                    <div><a-button type="primary" @click="addTwo($event, one)" class="addBtn"><Icon icon="ant-design:plus-outlined"/></a-button></div>
                    <div><a-button type="primary" danger @click="delOne($event, one)" class="addBtn"><Icon icon="ant-design:delete"/></a-button></div>
                  </a-space>
                </template>
                <draggable @end="endDraggable2" v-model="one.childrenList" item-key="_id">
                  <template #item="{ element: two }">
                    <div class="box">
                      <!-- 下一层 -->
                      <a-card>
                        <a-collapse v-model:activeKey="twoActiveKey" ghost expandIconPosition="right">
                          <a-collapse-panel :key="two._id">
                            <template #header>
                              <div class="twoTopDiv">
                                <div class="topDiv">
                                  <div>{{ two.sort }}二级标题:</div>
                                  <div class="twoInputd"><a-input :value="two.name" @change="changeInput($event, two, 'name')" @click="stop" class="ainput"/></div>
                                </div>
                              </div>
                            </template>
                            <template #extra>
                              <div><a-button type="primary" danger @click="delTwo($event, one, two)" class="addBtn"><Icon icon="ant-design:delete"/></a-button></div>
                            </template>
                            <div style="padding-top: 1rem;">
                              <a-space>
                                <div><a-button type="primary" @click="addThree($event, two, 'video')" class="addBtn"><Icon icon="ant-design:plus-outlined"/>视频</a-button></div>
                                <div><a-button type="primary" @click="addThree($event, two, 'document')" class="addBtn"><Icon icon="ant-design:plus-outlined"/>文档</a-button></div>
                                <div><a-button type="primary" @click="addThree($event, two, 'richText')" class="addBtn"><Icon icon="ant-design:plus-outlined"/>富文本</a-button></div>
                                <!-- <div><a-button type="primary" @click="addThree($event, two, 'classroomTest')" class="addBtn"><Icon icon="ant-design:plus-outlined"/>随堂测试</a-button></div>
                                <div><a-button type="primary" @click="addThree($event, two, 'discuss')" class="addBtn"><Icon icon="ant-design:plus-outlined"/>讨论</a-button></div> -->
                              </a-space>
                            </div>
                            <draggable @end="endDraggable" v-model="two.childrenList" item-key="_id">
                              <template #item="{ element: three }">
                                <div class="box">
                                  <a-card :title="three.sort + '三级内容:'">
                                    <div class="topDiv">
                                      <template v-if="three.type == 'video'">
                                        <!-- <j-upload v-model:value="three.filePath" maxCount="1" suffixList="avi,mov,mkv,mpeg,asf,3gp,wmv,mp4,flv,rmvb"/> video/mp4,video/webm,video/ogv-->
                                        <j-upload v-model:value="three.filePath" maxCount="1" text="上传视频" accept=".mp4,.webm,.ogv" :forceAcceptVerify="true"/>
                                      </template>
                                      <template v-if="three.type == 'document'">
                                        <j-upload v-model:value="three.filePath" maxCount="1"/>
                                      </template>
                                      <template v-if="three.type == 'richText'">
                                        -{{ three.richText }}-
                                        <j-editor :ref="el => addRef(el, three._id)" v-model:value="three.richText" :disabled="disabled"/>
                                      </template>
                                      <!-- <template v-if="three.type == 'classroomTest'">
                                        随堂测试
                                      </template>
                                      <template v-if="three.type == 'discuss'">
                                        讨论
                                      </template> -->
                                    </div>
                                    <template #extra>
                                      <div><a-button type="primary" danger @click="delThree($event, two, three)" class="addBtn"><Icon icon="ant-design:delete"/></a-button></div>
                                    </template>
                                  </a-card>
                                </div>
                              </template>
                            </draggable>
                          </a-collapse-panel>
                        </a-collapse>
                      </a-card>
                    </div>
                  </template>
                </draggable>
              </a-collapse-panel>
            </a-collapse>
          </a-card>
        </div>
      </template>
      <!-- <template #footer> -->
        <!-- <a-button @click="sureChange" type="primary" style="margin-top: 100px">确定</a-button> -->
      <!-- </template> -->
    </draggable>
  </div>


  <!-- <a-card >
    <div style="background: #f0f2f5;">
      <div class="oneInput">
        <a-input v-model:value="one.name"/>
      </div>
      <a-card v-for="(two, twoi) in one.childrenList" :key="twoi">
        <a-input v-model:value="two.name"/>
        <a-card v-for="(three, threei) in two.childrenList" :key="threei">
          <a-input v-model:value="three.name"/>
        </a-card>
        <a-button type="dashed" block @click="addThree(two)">
          <Icon icon="ant-design:plus-outlined"/>
        </a-button>

      </a-card>
      <a-button type="dashed" block @click="addTwo(one)">
        <Icon icon="ant-design:plus-outlined"/>
      </a-button>
    </div>
  </a-card>
  <a-button type="dashed" block @click="addOne()" class="oenAddBtn">
    <Icon icon="ant-design:plus-outlined"/>
  </a-button> -->

</template>

<script lang="ts" name="jiaoXueDanYuanNeiRongIndex" setup>
  import { ref, reactive, onMounted, unref, nextTick } from 'vue';
  import { Input, Popover, Pagination, Empty } from 'ant-design-vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from "/@/hooks/web/useMessage";
  import { useRouter } from 'vue-router';
  import { getRandom } from '/@/utils/common/compUtils'

  import draggable from 'vuedraggable';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import JEditor from '/@/components/Form/src/jeecg/components/JEditor.vue';


  //src\components\Form\src\jeecg\components\JEditor.vue

  //当前路由信息
  const { currentRoute } = useRouter();
  const { query } = unref(currentRoute);
  const { rwbh, xqxn, type } = query;//获取传递参数

  const { createConfirm, createMessage } = useMessage();
  const queryParam = ref<any>({});
  const dataSource = ref<any>([]);

  const editorRefs = ref<any>({});

  function addRef(el,id) {
    editorRefs.value[id] = el;
  }

  // dataSource.value = [
  // ];

  function reload() {
    loadData();
  }
  function loadData() {
    let rdata = [
      {
        "id": "5079",
        "name": "2qweqwe",
        "sort": 1,
        "childrenList": []
      },
      {
        "id": "5643",
        "name": "qweqwe",
        "sort": 2,
        "childrenList": [
          {
            "id": "3620",
            "name": "",
            "sort": 1,
            "childrenList": [
              {
                "id": "564311",
                "sort": 1,
                "type": "video",
                filePath: null,
                richText: null,
              },
              {
                "id": "5643112",
                "sort": 2,
                "type": "richText",
                filePath: null,
                richText: null,
              },
            ]
          },
          {
            "id": "2937",
            "name": "",
            "sort": 2,
            "childrenList": []
          }
        ]
      }
    ];

    dataRecursion(rdata, (x) => x._id = x.id);

    dataSource.value = rdata;
  }

  //递归执行方法
  function dataRecursion(list, fn){
    let _list = list;
    _list.forEach((x,i) => {
      fn(x,i);
      if(x.childrenList && x.childrenList.length){
        dataRecursion(x.childrenList, fn);
      }
    })
  }

  //刷新数据的排序
  function refreshDataSort(){
    dataRecursion(dataSource.value, (x, i) => x.sort = i+1);
  }


  function createNoneData(){
    let data = {
      _id: getRandom(10),
      name: '',
      sort: 0,
      childrenList: [],
    }
    return data;
  }

  function stop(e) {
    e.stopPropagation();
  }

  function changeInput(e, pdata, key) {
    let { target } = e;
    let { value } = target;
    pdata[key] = value;
  }

  function addOne(){
    dataSource.value.push(createNoneData());
    nextTick(() => {
      refreshDataSort();
    })
  }

  function delOne(e, one){
    stop(e);
    let index = dataSource.value.findIndex(x => x == one);
    if(index == -1) return;
    dataSource.value.splice(index, 1);
    nextTick(() => {
      refreshDataSort();
    })
  }

  // function insertOne(index){
  //   dataSource.value.splice(index,0, createNoneData());
  // }

  function addTwo(e, one){
    stop(e);
    one.childrenList.push(createNoneData());
    nextTick(() => {
      refreshDataSort();
    })
  }

  function delTwo(e, one, two){
    stop(e);
    let index = one.childrenList.findIndex(x => x == two);
    if(index == -1) return;
    one.childrenList.splice(index, 1);
    nextTick(() => {
      refreshDataSort();
    })
  }

  // function insertTwo(one, index){
  //   one.childrenList.splice(index,0, createNoneData());
  // }

  function addThree(e, two, type){
    stop(e);
    let data = createNoneData();
    data.type = type;
    data.richText = '';
    data.filePath = '';
    two.childrenList.push(data);
    nextTick(() => {
      refreshDataSort();
    })
  }

  function delThree(e, two, three){
    stop(e);
    let index = two.childrenList.findIndex(x => x == three);
    if(index == -1) return;
    two.childrenList.splice(index, 1);
    nextTick(() => {
      refreshDataSort();
    })
  }


  function endDraggable(evt){

    console.log(`🚀 -----------------------------🚀`);
    console.log(`🚀 ~ endDraggable ~ evt:`, evt);
    console.log(`🚀 -----------------------------🚀`);

    nextTick(() => {
      refreshDataSort();
    })
  }

  function moveDraggable(...d) {

    console.log(`🚀 --------------------------------🚀`);
    console.log(`🚀 ~ moveDraggable ~ ...d:`, ...d, editorRefs);
    console.log(`🚀 --------------------------------🚀`);
  }

  function save(){
    console.log(unref(dataSource));
  }

  loadData();
</script>

<style lang="less" scoped>
.box {
  margin: .5rem 0 0 0;
  .itemTop {
    width: 100%;
  }
}


.oneCard {
  margin: .5rem 0 0 0;
}
.oenAddBtn {
  margin-top: .5rem;
}
.oneInput {
  margin-bottom: .5rem;
}


.topButton {
  padding-top: 1rem;
}

.ainput {
  width: 100%;
}

.inputd {
  width: calc(100%  - 100px);
}
.topDiv {
  width: 100%;
  display: flex;
  flex-wrap: nowrap;
  justify-content: flex-start;
  align-items: center;
}
.twoInputd {
  width: calc(100%  - 100px);
}
.twoTopDiv {
  width: 100%;
}

:deep(.maxDiv) .ant-collapse-header{
  align-items: center;
}
</style>
