<template>
  <div
    @click.stop="handleSelectWidget(index)"
    class="widget-subtable"
    :class="{active: selectWidgetItem.key == element.key}"
  >
    <div
      class="drag-widget"
      title="拖拽"
      v-if="selectWidgetItem.key == element.key"
    >
      <i class="icon-drag"></i>
    </div>
    <div class="widget-view-action" v-if="selectWidgetItem.key == element.key">
      <i
        class="icon-application-delete del-icon"
        title="删除"
        @click.stop="handleWidgetDelete(index)"
      ></i>
    </div>
    <div style="overflow-x: auto; overflow-y: hidden; padding: 15px">
      <div
        v-if="element.options.showLabel"
        style="font-weight: bold; padding: 10px"
      >
        {{ element.desc_zh || element.desc }}
      </div>
      <el-row
        class="widget-col"
        v-if="element && element.key"
        type="flex"
        :class="{active: selectWidgetItem.key == element.key}"
        :gutter="element.options.gutter ? element.options.gutter : 0"
        :justify="element.options.justify"
        :align="element.options.align"
      >
        <el-col :span="24">
          <draggable
            v-model="element.list"
            :no-transition-on-drag="true"
            v-bind="{
              group: 'form',
              ghostClass: 'ghost',
              animation: 200,
              handle: '.drag-widget'
            }"
            @end="handleMoveEnd($event, element)"
            @start="handleMoveStart"
            @add="handleWidgetColAdd($event, element)"
          >
            <transition-group name="fade" tag="div" class="widget-col-list">
              <template v-for="(child, i) in element.list.filter(c => c.key)">
                <widget-grid-layout
                  v-if="child.ctrlType == 'grid'"
                  :key="child.key"
                  :element="child"
                  :index="i"
                  :select="selectWidgetItem"
                  :selectWidgetList.sync="selectWidgetList"
                  :data.sync="element"
                />

                <widget-suntable-layout
                  v-else-if="child.ctrlType == 'suntable'"
                  :key="child.key"
                  :data.sync="element"
                  :select="selectWidgetItem"
                  :selectWidgetList.sync="selectWidgetList"
                  :index="i"
                  :element.sync="child"
                />

                <widget-sundiv-layout
                  v-else-if="child.ctrlType == 'sunDiv'"
                  :key="child.key"
                  :data.sync="element"
                  :select="selectWidgetItem"
                  :selectWidgetList.sync="selectWidgetList"
                  :index="i"
                  :element.sync="child"
                />

                <widget-form-item
                  v-else
                  :key="child.key"
                  :select="selectWidgetItem"
                  :element="child"
                  :index="i"
                  :selectWidgetList.sync="selectWidgetList"
                  :data.sync="element"
                />
              </template>
            </transition-group>
          </draggable>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import {mapState, mapMutations} from 'vuex'
import Draggable from 'vuedraggable'
import WidgetFormItem from '@/components/form/WidgetFormItem.vue'
import WidgetGridLayout from '@/components/form/WidgetGridLayout.vue'
import WidgetSuntableLayout from '@/components/form/WidgetSuntableLayout.vue'
import WidgetSundivLayout from '@/components/form/WidgetSunDivLayout.vue'
import controlsApi from '@/api/controlsConfig.js'
import deepmerge from 'deepmerge'
import updateSelectControl from '@/mixins/updateSelectControl.js'
export default {
  name: 'widget-subdiv-layout',
  components: {
    Draggable,
    WidgetFormItem,
    WidgetGridLayout,
    WidgetSuntableLayout,
    WidgetSundivLayout
  },
  mixins: [updateSelectControl],
  props: ['element', 'select', 'index', 'data', 'selectWidgetList'],
  data() {
    return {}
  },
  computed: {
    ...mapState('form', ['selectWidget'])
  },
  methods: {
    ...mapMutations({
      setSelectWidget: 'form/setSelectWidget' // 设置当前选中控件
    }),
    handleMoveStart: function(evt) {
      // 新增控件时  将控件属性切断联系
      evt.item._underlying_vm_ = deepmerge({}, evt.item._underlying_vm_, {
        clone: true
      })
      const key =
        Date.parse(new Date()) + '_' + Math.ceil(Math.random() * 99999)
      evt.item._underlying_vm_.key = key

      evt.item._data_vm_=deepmerge({}, this.data.list, {
        clone: true
      })
      evt.item._p_vm_=this.data
    },
    handleSelectWidget(index) {
      this.setSelectWidget(this.data.list[index])
    },
    handleMoveEnd: function(evt, element) {
      if (evt.to.__vue__) {
        if (element.name == evt.item._underlying_vm_.boSubEntity) {
          //子表栅格布局放入到主表内，绑定是子表字段问题
          evt.item._underlying_vm_.boSubEntity = ''
          evt.item._underlying_vm_.parentNodeType = ''
          evt.item._underlying_vm_.parentType = ''
        }
      } else {
        for (let a = 0; a < this.element.list.length; a++) {
          //判断是否是孙表DIV布局
          if (this.element.list[a].ctrlType == 'sunDiv') {
            for (let b = 0; b < this.element.list[a].list.length; b++) {
              //判断孙表DIV布局是否存在栅格布局
              if (this.element.list[a].list[b].ctrlType == 'grid') {
                //判断孙表DIV布局中的栅格布局是否是sub拖拽过来的
                if (this.element.list[a].list[b].parentNodeType == 'sub') {
                  //修复子表栅格布局放入到其他的孙表内，绑定是子表字段问题
                  evt.item._underlying_vm_.boSubEntity = this.element.list[
                    a
                  ].name
                  evt.item._underlying_vm_.parentNodeType = 'sun'
                  evt.item._underlying_vm_.parentType = 'sunDiv'
                  break
                }
              }
            }
          }
        }
      }
    },
    handleAddBack($event, element, newIndex){
      element.list.splice(newIndex, 1)
      if($event.item._p_vm_){
        $event.item._p_vm_.list=$event.item._data_vm_
      }
    },
    handleWidgetColAdd($event, element) {
      const newIndex = $event.newIndex
      const oldIndex = $event.oldIndex
      const item = $event.item._underlying_vm_
      
      const pathLength =
        item && item.fieldPath && item.fieldPath.split('.').length
      if (item.ctrlType == 'grid') {
        if (
          item.columns.length &&
          item.columns.some(item => item.list.length)
        ) {
          this.$message.warning('子表中不允许拖入有控件的栅格布局')
          this.handleAddBack($event, element, newIndex)
          return false
        }
      }
      if (pathLength == 2 && !item.fieldPath.startsWith('item')&&
          !item.options.noBindModel) {
        this.$message.warning('子表中不允许拖入主表字段')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType == 'QRcode') {
        this.$message.warning('子表不允许存在二维码')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType == 'milepost') {
        this.$message.warning('子表不允许存在里程碑')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType == 'processForecast') {
        this.$message.warning('子表不允许存在审批预测')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType == 'table') {
        this.$message.warning('子表不允许存在表格布局')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType === 'customChart') {
        this.$message.warning('子表不允许存在图表')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType == 'amap') {
        this.$message.warning('子表不允许存在高德地图')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType == 'related-process') {
        this.$message.warning('子表不允许存在相关流程')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType === 'dataView') {
        this.$message.warning('子表不允许存在数据列表')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType === 'formTable') {
        this.$message.warning('子表不允许存在表单列表')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType === 'divider') {
        this.$message.warning('子表不允许存在分割线')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType === 'signature') {
        this.$message.warning('子表不允许存在签章')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType === 'handSignature') {
        this.$message.warning('子表不允许存在手动签章')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (item.ctrlType === 'htCustomComponent') {
        this.$message.warning('子表不允许存在DIY组件')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      // 拖动时禁止孙表拖动至子表
      if (this.isSunItem(newIndex, element)) {
        this.$message.warning('子表中不允许拖入孙表字段')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (
        !controlsApi.handleLayoutComponents(
          this,
          element,
          element.list,
          newIndex
        )
      ) {
        this.handleAddBack($event, element, newIndex)
        return false
      }
      if (!element.options.boSubEntity) {
        this.$message.warning('请先绑定子表')
        this.handleAddBack($event, element, newIndex)
        return false
      }
      // 防止布局元素的嵌套拖拽
      if ($event.item.isLayout && element.list.list) {
        element.list.list.splice(newIndex, 1)
        return false
      }
      let params = element.list[newIndex]
      params.parentNodeType = 'sub'
      params.parentType = 'subDiv'
      params.boSubEntity = element.options.boSubEntity
      this.setSelectWidget(params)
    },
    // 判断是否孙表拖动过来的字段
    isSunItem(newIndex, element) {
      let obj = element.list[newIndex]
      if (obj.fieldPath) {
        if (obj.fieldPath.split('.').length >= 4) {
          return true
        }
      }
      return false
    },
    handleWidgetDelete(index) {
      if (this.data.list.length - 1 === index) {
        if (index === 0) {
          this.setSelectWidget({options: {validateType: ''}})
        } else {
          this.setSelectWidget(this.data.list[index - 1])
        }
      } else {
        this.setSelectWidget(this.data.list[index + 1])
      }

      this.$nextTick(() => {
        this.data.list.splice(index, 1)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/form-editor.scss';
.widget-subtable {
  padding-bottom: 0;
  padding: 10px 5px;
  position: relative;
  margin: 28px 14px;
}

.widget-subtable.active {
  outline: 2px solid $--form-design-active-color !important;
}
div.widget-view-action {
  .del-icon {
    width: 20px;
    margin-left: 12px;
  }
}

.widget-col {
  padding-bottom: 0;
  padding: 15px 5px;
}

.el-col {
  min-height: 50px;
}

.widget-col-list {
  min-height: 50px;
  border: 1px solid #ededed;
}

div.widget-view-action i.icon-trash {
  margin: 0 10px;
}
</style>
