<template>
  <div class="onlineCheckList">
    <table class="table">
      <tr>
        <th class="">模块</th>
        <th>模块名</th>
      </tr>
      <tr v-for="(d, index) in data.tableContent" :key="d.id">
        <td>
          <el-input v-if="isedit" v-model="d.module" placeholder="请输入模块" :size="size" />
          <span v-else>{{ d.module }}</span>
        </td>
        <td>
          <div v-if="isedit" class="addmodule">
            <el-select
              v-model="value"
              class="addSelect"
              :size="size"
              multiple
              filterable
              remote
              reserve-keyword
              placeholder="请选择模块"
              :remote-method="remoteMethod"
              :loading="loading"
              @change="clearHandle(index)"
            >
              <el-option
                v-for="item in options"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
            <span class="addBtn" @click="addModule(index)">添加</span>
          </div>
          <div v-if="isedit">
            <el-tag
              v-for="tag in d.moduleNames"
              :key="tag"
              class="tag"
              size="small"
              closable
              type="info"
              @close="handleClose(index, tag)"
            >
              {{ tag }}
            </el-tag>
          </div>
          <div v-else>
            <span v-for="tag in d.moduleNames" :key="tag" class="tag word">{{ tag }}</span>
          </div>
          <div v-if="isedit" class="btnGroup">
            <svg-icon icon-class="add" class="svg" @click="$emit('changeRow', 'add', moduleId, index)" />
            <svg-icon v-if="data.tableContent.length !== 1" icon-class="del" class="svg" @click="$emit('changeRow', 'del', moduleId, index)" />
          </div>
        </td>
      </tr>
    </table>
    <div class="inlineList">
      <div class="subTitle">
        上线顺序
        <el-tooltip effect="dark" content="鼠标拖动模块，可调整顺序" placement="top-start">
          <i class="el-icon-info" />
        </el-tooltip>
      </div>
      <draggable
        v-if="isedit"
        v-model="data.onlineOrder"
        chosen-class="chosen"
        force-fallback="true"
        group="people"
        animation="1000"
        @start="onStart"
        @end="onEnd"
      >
        <transition-group>
          <div
            v-for="(element, index) in data.onlineOrder"
            :key="element"
            class="onlineItem"
          >
            <span class="dragNo">{{ index+ 1 }}</span>
            <span class="dragword">{{ element }}</span>
          </div>
        </transition-group>
      </draggable>
      <div v-else>
        <div
          v-for="(element, index) in data.onlineOrder"
          :key="element"
          class="onlineItem textItem"
        >
          <span class="dragNo">{{ index+ 1 }}</span>
          <span class="dragword">{{ element }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import draggable from 'vuedraggable'
import { getOnlineBizModule } from '@/api/publishTask'
export default {
  components: {
    draggable
  },
  props: {
    isedit: {
      type: Boolean,
      required: false,
      default: false
    },
    data: {
      type: Object,
      required: false,
      default: () => {}
    },
    moduleId: {
      type: Number,
      required: true,
      default: -1
    },
    taskId: {
      type: Number,
      required: false,
      default: null
    }
  },
  data() {
    return {
      drag: false,
      options: [],
      value: [],
      loading: false,
      size: 'small',
      moduleName: '',
      name: ''
    }
  },
  methods: {
    async remoteMethod(moduleName) {
      this.moduleName = moduleName
      if (moduleName !== '') {
        this.loading = true
        const res = await getOnlineBizModule({ taskId: this.taskId, moduleName })
        this.options = res.data
        this.loading = false
      } else {
        this.options = []
      }
    },
    clearHandle(subIdx) {
      this.$emit('changeSelectedHandle', this.moduleId, subIdx, this.value[0], 'add')
      this.moduleName = ''
      this.value = null
    },
    addModule(subIdx) {
      if (this.moduleName) {
        this.$emit('changeSelectedHandle', this.moduleId, subIdx, this.moduleName, 'add')
      }
    },
    handleClose(subIdx, val) {
      this.$emit('changeSelectedHandle', this.moduleId, subIdx, val, 'del')
    },
    // 开始拖拽事件
    onStart() {
      this.drag = true
    },
    // 拖拽结束事件
    onEnd() {
      this.drag = false
    }
  }
}
</script>
<style scoped lang='scss'>
.onlineCheckList {
  max-width: 690px;
  .table {
    width: 100%;
    border-radius: 4px;
    border-collapse: collapse;
    border: none;
    th {
      background: #E8E8E8;
      text-align: center;
      color: '#333B4A';
      font-size: '14px';
      font-weight: '400';
      &:first-child {
        width: 216px;
      }
    }
    th, td {
      border: 1px solid #D1D0D0;
      padding: 12px 10px;
      position: relative;
      &:first-child {
        text-align: center;
      }
    }
    .btnGroup {
      position: absolute;
      bottom: 0px;
      right: -60px;
      width: 60px;
      text-align: center;
      .svg {
        margin: 0 5px;
        font-size: 14px;
      }
    }
  }
  .inlineList {
    .dragNo {
      display: inline-block;
      width: 16px;
      height:16px;
      line-height: 14px;
      text-align: center;
      border: 1px solid #999;
      border-radius: 50%;
      margin-right: 5px;
    }
    .dragword {
      font-size: 12px;
    }
    .subTitle {
      margin-top: 16px;
      .el-icon-info {
        color: #999;
      }
    }
  }
  .tag {
    margin-right: 24px;
    margin-bottom: 5px;
    &.word {
      margin-right: 0px;
      &::after{
        content: '，';
      }
      &:last-child::after {
        content: '';
      }
    }
  }
  .addmodule {
    margin-bottom: 5px;
    .addBtn {
      font-size: 12px;
      color: #409eff;
      margin-left: 6px;
      cursor: pointer;
    }
  }
}
.onlineItem {
  display: inline-block;
  margin: 10px 40px 10px 0px;
  font-size: 14px;
  border: solid 2px transparent;
  // margin-bottom: 10px;
  cursor: move;
  &.textItem{
    cursor: text;
  }
}
.chosen {
  border: solid 2px #3089dc !important;
}
</style>
