<template>
  <el-row>
    <el-col :span="3">
      <el-select size="mini" v-model="form.factor">
        <el-option value="0" label="全部"></el-option>
        <el-option v-for="it in factors" :value="it.value" :label="it.name" :key="it.value"/>
      </el-select>
    </el-col>
    <el-col :span="3">
      <el-select size="mini" v-model="form.source">
        <el-option v-for="it in score" :value="it.value" :label="it.name"/>
      </el-select>
    </el-col>
    <el-col :span="3">
      <el-select size="mini" v-model="form.condition">
        <el-option v-for="it in conditionList" :value="it.value" :label="it.name" :key="it.value"/>
      </el-select>
    </el-col>
    <el-col :span="3">
      <el-input size="mini" v-model="form.val"/>
    </el-col>
    <el-col :span="3">
      <a class="y-btn-puls" href="javascript:;" @click="add"
         v-if="(!lastBtnIndex||(lastBtnIndex-1)==index)&&buts.includes('isAdd')">
        <i class="el-icon-plus"/>
      </a>
      <a class="y-btn-puls" href="javascript:;" @click="del" v-if="buts.includes('isDel')">
        <i class="el-icon-delete"/>
      </a>
    </el-col>
  </el-row>
</template>

<script>
import tool from '../../tool'

export default {
  name: 'condition-setting-item',
  props: {
    data: [Array, Object],// 当前行因子规则
    dataSource: [Array, Object], // 当前数据源
    index: [Number],// 当前数据排序
    lastBtnIndex: [Number], // 包含此参数，则显示最后一个添加按钮
    buts: [Array, Object]
  },
  data () {
    return {
      form: {},
      factors: [],
      score: [
        { name: '得分', value: '1' }
      ],
      conditionList: [
        { name: '等于', value: '==' },
        { name: '不等于', value: '!=' },
        { name: '大于', value: '>' },
        { name: '大于等于', value: '>=' },
        { name: '小于', value: '<' },
        { name: '小于等于', value: '<=' },
      ]
    }
  },
  created () {
    if (!this.data.uid) {
      let uid = tool.random.snowflakeId()
      this.form = {
        uid: uid,
        factor: '',
        source: '1',
        condition: '==',
        val: uid,
      }
    } else {
      this.form = this.data
    }
    this.factors = this.dataSource
  },
  methods: {
    add () {
      let uid = tool.random.snowflakeId()
      this.$emit('add', {
        uid: uid,
        factor: '',
        source: '1',
        condition: '==',
        val: uid,
      })
    },
    del () {
      this.$emit('del', this.form, this.index)
    },
  }
}
</script>

<style scoped>
.el-row {
  margin: 10px 0;
}

.el-col {
  margin: 0 10px;
}

.el-col:last-child {
  margin: 0;
}

.el-col:first-child {
  margin: 0 10px 0 0;
}

.el-row .el-select {
  width: 100%;
}

.y-btn-puls {
  height: 28px;
  width: 28px;
  line-height: 28px;
  margin: 0 5px;
  padding: 2px;
  border-radius: 2rem;
  border: 1px #cccccc solid;
  background-color: white;
}

.y-btn-puls:first-child {
  margin: 0;
}

.y-btn-puls i {
  color: #cccccc;
}
</style>
