<template>
  <div class="kmc">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
      <el-row>
        <el-col :span="6">
          <el-form-item label="是否显示KOL入口" prop="resource">
            <el-radio-group v-model="ruleForm.resource">
              <el-radio label="2">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入口文案" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入入口文案，最长6个字"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-button type="primary" @click="handleAdd">添加</el-button>
        <span class="des">*说明：添加和移除kol后，需点击页面的保存按钮，方可生效</span>
      </el-row>
    </el-form>
    <el-row class="table-content">
      <el-table
        :data="tableData"
        border
        id="kolTable"
        style="width: 100%"
      >
        <el-table-column
          show-overflow-tooltip
          v-for="(item, key) in col"
          :key="`col_${key}`"
          :prop="col[key].prop"
          :label="item.label"
          align="center"
        >
          <template slot-scope="scope">
            <p>{{ scope.row[item.prop] }}</p>
            <div v-if="item.prop === 'operate'">
              <el-button size="small">移除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="save-btn">
        <el-button type="primary" @click="handleSave">保存</el-button>
      </el-row>
    </el-row>
  </div>
</template>

<script>
import Sortable from 'sortablejs';
import { kolCol } from './util/common';

export default {
  components: {
    Sortable
  },
  data() {
    return {
      ruleForm: {
        name: '',
        resource: '2'
      },
      rules: {
        resource: [
          { required: true, message: '请选择是否展示入口', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入入口文案', trigger: 'blur' },
          { max: 6, message: '入口文案长度不能多于6个字符', trigger: 'blur' }
        ]
      },
      col: kolCol,
      tableData: [
        {
          index: 1
        },
        {
          index: 2
        },
        {
          index: 3
        },
        {
          index: 4
        }
      ]
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.rowDrop();
    });
  },
  methods: {
    rowDrop() {
      const tbody = document.querySelector('#kolTable tbody');

      Sortable.create(tbody);
    },
    handleAdd() {},
    handleSave() {}
  },
}
</script>

<style lang="scss" scoped>
.des {
  font-size: 12px;
  color: #777;
  margin-left: 40px;
}
.table-content {
  margin-top: 15px;
}
.save-btn {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>
