<template>
  <div>
    <el-row>
      <el-col :span="24">
        <btn-list :msg="btn" class="contHide" @on-result-change="_btnClick" />
      </el-col>
      <el-col :span="12">
        <el-card>
          <div slot="header">
            选择列表
          </div>
          <div :style="{height}">
            <draggable v-model="contractList" element="span" :options="dragOptions" :move="onMove" @sort="_handleSort">
              <transition-group
                type="transition"
                class="iview-admin-draggable-list list-group"
                :name="'flip-list'"
                tag="ul"
                @start="isDragging=true"
                @end="isDragging=false"
              >
                <li v-for="(item) in contractList" :key="item.key" class="todolist-item" style="white-space: nowrap">
                  <span style="display: inline-block;width: 50%">{{ item.label }}</span>
                  <span style="display: inline-block;width: 50%">{{ item.key }}</span>
                </li>
              </transition-group>
            </draggable>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12" class="padding-left-10">
        <el-card>
          <div slot="header">
            结果
          </div>
          <div :style="{height}">
            <draggable v-model="contractResultList" element="span" :options="dragOptions" :move="onMove" @sort="_handleSort">
              <transition-group
                name="no"
                class="iview-admin-draggable-list list-group"
                tag="ul"
                @start="isDragging=true"
                @end="isDragging=false"
              >
                <li v-for="item in contractResultList" :key="item.key" class="todolist-item" style="white-space: nowrap">
                  <span style="display: inline-block;width: 50%">{{ item.label }}</span>
                  <span style="display: inline-block;width: 50%">{{ item.key }}</span>
                </li>
              </transition-group>
            </draggable>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import draggable from 'vuedraggable'
import { componentFlow } from '../../../../api'

export default {
  components: {
    draggable
  },
  data() {
    return {
      btn: [{ type: 'primary', id: '', name: '保存' }],
      height: '',
      contractList: [],
      contractResultList: [],
      editable: true,
      isDragging: false,
      delayedDragging: false,
      companyId: '',
      list: []
    }
  },
  computed: {
    dragOptions() {
      return {
        animation: 0,
        group: 'description',
        disabled: !this.editable,
        ghostClass: 'ghost'
      }
    }
  },
  watch: {
    isDragging(newValue) {
      if (newValue) {
        this.delayedDragging = true
        return
      }
      this.$nextTick(() => {
        this.delayedDragging = false
      })
    }
  },
  methods: {
    _open(companyId) {
      this.height = document.documentElement.clientHeight - 320 + 'px'
      this.companyId = companyId
      this._getAllStatus()
      this._getLeftList()
      this._getRightList()
    },
    _getLeftList: async function() {
      const result = await componentFlow.flowOption({
        type: 15,
        companyId: this.companyId
      })
      if (result) {
        this.contractList = result
        for (let i = 0; i < this.contractList.length; i++) {
          const key = this.contractList[i].key
          for (let j = 0; j < this.list.length; j++) {
            if (key === this.list[j].key) {
              this.contractList[i].sortNo = this.list[j].sortNo
              break
            }
          }
        }
      }
    },
    _getRightList: async function() {
      const result = await componentFlow.flowResult({
        type: 15,
        companyId: this.companyId
      })
      if (result) {
        this.contractResultList = result
        for (let i = 0; i < this.contractResultList.length; i++) {
          const key = this.contractResultList[i].key
          for (let j = 0; j < this.list.length; j++) {
            if (key === this.list[j].key) {
              this.contractResultList[i].sortNo = this.list[j].sortNo
              break
            }
          }
        }
      }
    },
    _getAllStatus: async function() {
      const result = await componentFlow.allStatus({ type: 15 })
      if (result) {
        this.list = result
      }
    },
    onMove({ relatedContext, draggedContext }) {
      const relatedElement = relatedContext.element
      const draggedElement = draggedContext.element
      return (!relatedElement || !relatedElement.fixed) && !draggedElement.fixed
    },
    _btnClick(msg) {
      switch (msg) {
        case '保存':
          this._save()
          break
      }
    },
    _save: async function() {
      const list = []
      for (let i = 0; i < this.contractResultList.length; i++) {
        list.push(this.contractResultList[i].key)
      }
      if (list.length === 0) {
        this.$message.error('请配置流程')
        return
      }
      const obj = {
        dto: { flows: list },
        companyId: this.companyId
      }
      const result = await componentFlow.saveSimpleContract(obj)
      if (result) {
        this.$message.success('保存成功！')
      }
    },
    _handleSort() {
      this.contractList.sort((e1, e2) => {
        return Number(e1.sortNo) - Number(e2.sortNo)
      })
      this.contractResultList.sort((e1, e2) => {
        return Number(e1.sortNo) - Number(e2.sortNo)
      })
    }
  }
}
</script>
