<template>
  <el-dialog
    title="Select Vehicle Attribute"
    :visible.sync="dialogVisible"
    width="600px">
    <el-form
      :model="form"
      ref="ruleForm"
      label-width="130px"
      :rules="rules"
    >
      <el-form-item label="Vehicle Attributes: " prop="vehicleAttrs">
        <el-select v-model="form.vehicleAttrs" placeholder=" " multiple no-match-text="No match data" no-data-text="No data" filterable clearable>
          <el-option
            v-for="(ele, index) in attributeList"
            :key="index"
            :label="ele.mappingName"
            :value="ele.attrName">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">Cancel</el-button>
      <el-button type="primary" size="small" @click="confirm">Confirm</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getAttributeKV } from '@/api/group';
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        vehicleAttrs: [],
      },
      rules: {
        vehicleAttrs: [
          { required: false, message: "Please select vehicle attribute", trigger: "change" },
        ],
      },
      attributeList: [],
    };
  },
  created() {
    this.getAttributeList()
  },
  methods: {
    getAttributeList(){
      getAttributeKV().then(res => {
        this.attributeList = res.map(item => {
          return {
            ...item,
            values: item.values && typeof item.values === 'object' ? item.values : []
          }
        });
        this.$emit('getAdsVersion', this.attributeList.filter(item => item.mappingName === 'ADS Version')[0])
        this.attributeList = this.attributeList.filter(item => item.mappingName !== 'ADS Version')
      })
    },
    show() {
      this.dialogVisible = true;
    },
    confirm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.dialogVisible = false;
          this.$emit("getVehicleAttr", this.attributeList.filter(item => this.form.vehicleAttrs.includes(item.attrName)));
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.tags {
  li {
    display: flex;
    align-items: center;
    margin-bottom: 5px;
    i {
      font-size: 18px;
      font-weight: 600;
      color: #0B2C55;
      cursor: pointer;
    }
  }
}
</style>
