<template>
  <div>
    <div class="menu-title">
      条目管理
    </div>
    <div class="menu-content">
      <div>
        <el-form :inline="true" :model="qo" class="demo-form-inline">
          <el-form-item>
            <el-input v-model="qo.LIKES_categories" placeholder="请输入种类"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="qo.LIKES_labels" placeholder="请输入标签"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="qo.pageNo=1;queryData()">搜 索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="margin: 0px 0 20px 0">
        <el-button type="primary" @click="createRow()">添加</el-button>
      </div>
      <div>
        <el-table :data="result.records" style="width: 100%">
          <el-table-column type="index" label="行号" width="60"></el-table-column>
          <el-table-column prop="fid" label="fid"></el-table-column>
          <el-table-column prop="categories" label="种类">
            <div slot-scope="scope">
              <template v-for="(tag,index) in scope.row.categoriesArr">
                <el-tag :key="index" v-if="index < 5">{{ tag }}</el-tag>
              </template>
              <el-tag v-if="scope.row.categoriesArr.length > 10">...</el-tag>
            </div>
          </el-table-column>
          <el-table-column prop="labels" label="标签">
            <div slot-scope="scope">
              <template v-for="(tag,index) in scope.row.labelsArr">
                <el-tag :key="index" v-if="index < 5">{{ tag }}</el-tag>
              </template>
              <el-tag v-if="scope.row.labelsArr.length > 5">...</el-tag>
            </div>
          </el-table-column>
          <el-table-column prop="isHidden" label="是否隐藏" width="100">
            <template slot-scope="scope">
              {{ scope.row.isHidden ? '是' : '否' }}
            </template>
          </el-table-column>
          <el-table-column prop="description" label="描述">
            <template slot-scope="scope">
              {{ scope.row.description.substring(0, 100) }}{{ scope.row.description.length > 100 ? '...' : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click.native.prevent="modifyRow(scope.row)" type="text" size="small">
                编辑
              </el-button>
              <el-button @click.native.prevent="removeRow(scope.row)" type="text" size="small">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-box">
        <el-pagination background
                       @current-change="handleCurrentChange"
                       :current-page="qo.pageNo"
                       :page-size="qo.pageSize"
                       layout="total, prev, pager, next"
                       :total="result.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog
        :title="dialogName"
        :visible.sync="cmdDialogVisible"
        width="40%">
      <el-form ref="cmd" label-width="100px" :rules="rules" :model="cmd">
        <el-form-item label="fid" prop="fid">
          <el-input v-model="cmd.fid" placeholder="请输入fid"></el-input>
        </el-form-item>
        <el-form-item label="种类">
          <el-tag
              :key="tag"
              v-for="(tag,index) in cmd.categoriesArr"
              closable
              :disable-transitions="false"
              @close="handleTagClose(index, 'categories')">
            {{ tag }}
          </el-tag>
          <el-input
              class="input-new-tag"
              v-if="categoriesInputVisible"
              v-model="categoriesInputValue"
              ref="saveCategoriesTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm('categories')"
              @blur="handleInputConfirm('categories')"
          >
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showInput('categories')">+ 种 类</el-button>
        </el-form-item>
        <el-form-item label="标签">
          <el-tag
              :key="tag"
              v-for="(tag,index) in cmd.labelsArr"
              closable
              :disable-transitions="false"
              @close="handleTagClose(index, 'labels')">
            {{ tag }}
          </el-tag>
          <el-input
              class="input-new-tag"
              v-if="labelsInputVisible"
              v-model="labelsInputValue"
              ref="saveLabelsTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm('labels')"
              @blur="handleInputConfirm('labels')"
          >
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showInput('labels')">+ 标 签</el-button>
        </el-form-item>
        <el-form-item label="是否隐藏" prop="isHidden">
          <el-radio v-model="cmd.isHidden" :label="true">是</el-radio>
          <el-radio v-model="cmd.isHidden" :label="false">否</el-radio>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="cmd.description" placeholder="请输入描述" type="textarea" :rows="5"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cmdDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitModify">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request';

var _this;
export default {
  name: "entrys",
  data() {
    return {
      qo: {
        pageNo: 1,
        pageSize: 10,
        LIKES_categories: '',
        LIKES_labels: '',
      },
      result: {
        records: [],
        total: 0
      },
      sourceTypeList: [
        {name: '自定义本体', value: 1},
        {name: '标注系统', value: 2}
      ],
      dialogName: '编辑',
      cmdDialogVisible: false,
      cmd: {},
      rules: {
        fid: [
          {required: true, message: '请输入fid'}
        ],
        isHidden: [
          {required: true, message: '请选择是否隐藏'}
        ],
      },
      categoriesInputValue: '',
      categoriesInputVisible: false,
      labelsInputValue: '',
      labelsInputVisible: false
    }
  },
  mounted() {
    _this = this;
    _this.queryData();
  },
  methods: {
    queryData() {
      request({
        url: '/entrys/query_pages',
        method: 'post',
        data: _this.qo
      }).then(res => {
        res.data.records.forEach(row => {
          row.categoriesArr = row.categories ? row.categories.split(',') : [];
          row.labelsArr = row.labels ? row.labels.split(',') : [];
        });

        _this.result.records = res.data.records;
        _this.result.total = res.data.total;
      });
    },
    handleCurrentChange(val) {
      _this.qo.pageNo = val;
      _this.queryData();
    },
    removeRow(item) {
      _this.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request({
          url: '/entrys/remove/' + item.id,
          method: 'post'
        }).then(res => {
          _this.$message.success("删除成功");
          _this.queryData();
        });
      }).catch(() => {
      });
    },
    createRow() {
      _this.dialogName = '新建';
      _this.cmdDialogVisible = true;
      _this.$nextTick(_=> {
        _this.$refs['cmd'].clearValidate();
        _this.cmd = {
          fid: '',
          isHidden: false,
          categoriesArr: [],
          labelsArr: [],
          description: ''
        };
      })
    },
    modifyRow(item) {
      _this.dialogName = '编辑';
      _this.cmdDialogVisible = true;
      _this.$nextTick(_=> {
        _this.$refs['cmd'].clearValidate();
        _this.cmd = JSON.parse(JSON.stringify(item));
      })
    },
    submitModify() {
      this.$refs.cmd.validate((valid) => {
        if (valid) {
          if(_this.cmd.categoriesArr) {
            _this.cmd.categories = _this.cmd.categoriesArr.toString();
          }
          if(_this.cmd.labelsArr) {
            _this.cmd.labels = _this.cmd.labelsArr.toString();
          }

          if (_this.cmd.id) {
            request({
              url: '/entrys/modify',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("编辑成功");
              _this.queryData();
              _this.cmdDialogVisible = false;
            });
          } else { // 新建
            request({
              url: '/entrys/create',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("添加成功");
              _this.cmdDialogVisible = false;
            });
          }
        }
      });
    },
    showInput(typeName) {
      if (typeName === 'categories') {
        this.categoriesInputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveCategoriesTagInput.$refs.input.focus();
        });
      } else if (typeName === 'labels') {
        this.labelsInputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveLabelsTagInput.$refs.input.focus();
        });
      }
    },
    handleTagClose(index, typeName) {
      _this.cmd[typeName + "Arr"].splice(index, 1);
    },
    handleInputConfirm(typeName) {
      let inputValue = _this[typeName + "InputValue"];
      if(inputValue) {
        _this.cmd[typeName + "Arr"].push(inputValue);
      }
      _this[typeName + "InputVisible"] = false;
      _this[typeName + "InputValue"] = "";
    }
  }
}
</script>

<style lang="scss">
.input-new-tag {
  margin-bottom: 12px;
  height: 32px;
  width: 80px;

  input {
    height: 32px !important;
    width: 80px;
  }
}

.el-tag {
  margin-right: 10px;
  margin-bottom: 10px;
  height: 32px;
}

.button-new-tag {
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;

}
</style>
