condition.vue 7.71 KB
<!--
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-12-25 13:34:43
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2024-01-16 10:47:30
 * @FilePath: \scrm-micro-peregrination\src\views\journeyModel\components\condition.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->

<template>
  <div>
    <div class="condition-select flex">
      <el-select v-model="step" placeholder="请选择" :size="size" @change="selectStep">
        <el-option v-for="item in stepList" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
      <div class="flex">
        <el-select v-model="conditionList.flag" placeholder="请选择" :size="size" class="m-r-10" v-if="conditionList.conditions && conditionList.conditions.length > 1">
          <el-option v-for="item in conditionList.conditions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <template v-if="condition && condition.operators && condition.operators.length">
          <el-select v-model="operators" @change="condition.operators[operators].value = ''" placeholder="请选择" :size="size" class="m-r-10">
            <el-option v-for="(item, index) in condition.operators" :key="index" :label="item.name" :value="index"></el-option>
          </el-select>
          <div class="condition-input" v-if="condition.operators[operators]">
            <el-input v-model="condition.operators[operators].value" maxLength="3" oninput="value=value.replace(/^0+|[^1-9\d]/g, '')"></el-input>
          </div>
          <span>天&nbsp;&nbsp;切换到此旅程阶段</span>
        </template>
      </div>
    </div>
    <div class="condition-more" v-if="condition && condition.subConditions">
      <div>
        <template>
          <div class="flex-column-center m-t-10" v-for="(item, k) in condition.subConditions" :key="item.id">
            <label>并且满足</label>
            <el-select v-model="item.name" placeholder="请选择" :size="size" class="condition-1" v-if="item.operators">
              <el-option v-for="itm in [item.name]" :key="itm" :label="itm" :value="itm"></el-option>
            </el-select>
            <el-select v-model="item.value" @change="setInputValue(item.operators)" placeholder="请选择" :size="size" class="condition-1" v-if="item.operators">
              <el-option v-for="(itm, i) in item.operators" :key="i" :label="itm.name" :value="i"></el-option>
            </el-select>
            <div class="condition-input" v-if="item.operators[item.value].style == 'input'">
              <el-input v-model="item.operators[item.value].value"></el-input>
            </div>
            <el-select v-model="item.operators[item.value].value" placeholder="请选择" :size="size" class="condition-2" v-if="item.operators[item.value].style == 'select'">
              <el-option v-for="i in journeyList" :key="i" :label="i" :value="i"></el-option>
            </el-select>
            <span>{{ item.operators[item.value].unit }}</span>
            <i class="el-icon-delete" @click="delCondition(condition.subConditions, k)"></i>
          </div>
          <div class="condition-add" @click="addCondition(condition.subConditions)">
            <i class="el-icon-circle-plus-outline"></i>
            添加条件
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      step: null,
      addFinal: {},
      addInfo: {},
      condition_i: null,
      conditionIndex: 0,
      operators: 0,
      conditionList: [],
      condition: '',
      journeyList: [],
      stepList: []
    }
  },
  inject: ['stepList', 'app'],
  mounted() {
    const stepList = this.app.stepList || []
    stepList.forEach(item => {
      item.flag = ''
      item.conditions.forEach(i => {
        if (i.subConditions)
          i.subConditions.forEach(j => {
            j.value = 0
          })
      })
    })
    this.stepList = stepList
    this.journeyList = this.app.journeyList.filter(i => i)
  },
  watch: {
    'conditionList.flag'(val) {
      if (!this.conditionList.conditions.length) return
      if (!val) {
        val = this.conditionList.conditions[0]
        this.conditionList.flag = val.id
      }
      this.condition = this.conditionList.conditions.find(i => i.id == val)
    }
  },
  methods: {
    setInputValue(arr) {
      for (let i in arr) {
        arr[i].value = ''
      }
    },
    // mergeArr(arr1, arr2, val) {
    //   // 遍历数组 arr1
    //   arr1.forEach(a1 => {
    //     // 在数组 arr2 中查找相同 id 的对象
    //     const obj = arr2.find(a2 => a1.id === a2.id)
    //     // 如果找到匹配的对象,则进行覆盖
    //     if (obj[val] && typeof obj[val] != 'object' && obj[val] != a1[val]) {
    //       Object.assign(a1, obj)
    //     }
    //   })
    // },
    // contrastObj(obj1, obj2) {
    //   for (let i in obj1) {
    //     if (Array.isArray(obj1[i])) {
    //       this.mergeArr(obj1[i], obj2[i])
    //     }
    //   }
    // },
    setCondition(val, index) {
      this.step = val.id
      this.conditionListAll = this.$common.utils.deepClone(val)
      this.conditionList = this.$common.utils.deepClone(val)
      if (this.conditionList.conditions) this.conditionList.conditions = this.conditionList.conditions.filter(i => !i.isHide)
      this.condition_i = index
      this.condition = this.conditionList.conditions && this.conditionList.conditions[0]
      const _conditions = this.stepList.find(i => i.id == val.id).conditions
      _conditions.forEach(i => {
        if (i.id == this.condition.id && this.condition.subConditions.length == 0 && i.subConditions.length > 0) {
          this.condition.subConditions = this.$common.utils.deepClone(i.subConditions)
        }
      })
      console.log(this.condition, _conditions, 'this.conditionList')
    },
    // 保存最后一项用于新增
    delCondition(val, k) {
      if (val.length == 1) {
        this.addInfo = this.$common.utils.deepClone(val[0])
      }
      val.splice(k, 1)
    },
    // 新增条件
    addCondition(val) {
      let l = val.length,
        prop = this.$common.utils.deepClone(val[l - 1])
      if (!prop) prop = this.addInfo
      console.log(prop, 'prop')
      prop.id++
      val.push(prop)
    },
    selectStep(e) {
      const conditions = this.stepList.find(item => {
        return item.id == e
      })
      this.conditionListAll = this.$common.utils.deepClone(conditions)
      this.conditionList = this.$common.utils.deepClone(conditions)
      if (this.conditionList.conditions) this.conditionList.conditions = this.conditionList.conditions.filter(i => !i.isHide)
      this.condition = this.conditionList.conditions[0]
    }
  }
}
</script>
<style lang="scss" scoped>
.dialog-body {
  padding: 0px 50px;
}
.require {
  color: red;
}
.condition-item {
  line-height: 30px;
  font-size: 14px;
  color: #666666;
  .condition-more {
    background: #f9f9f9;
    padding: 20px;
    margin-top: 10px;
    .condition-1 {
      width: 136px;
      margin: 0 10px;
    }
    .condition-2 {
      width: 136px;
    }
    i {
      color: #999999;
      margin-left: 10px;
      cursor: pointer;
    }

    .condition-add {
      display: flex;
      align-items: center;
      margin-top: 10px;
      cursor: pointer;
      i {
        font-size: 20px;
        margin-right: 6px;
        color: #5850ff;
      }
      font-size: 12px;
      color: #5850ff;
    }
  }
  .condition-input {
    a {
      color: #5850ff;
    }
    /deep/ {
      .el-input {
        width: 136px;
        margin: 0px 4px;
      }
      .el-input__inner {
        height: 32px;
        line-height: 32px;
      }
    }
  }
  span {
    color: #333333;
  }
}
</style>