<script setup lang="ts" name="AddRules">
import { onMounted, reactive, ref, toRefs, watch } from "vue";
import { Switch } from "view-ui-plus";

const props = defineProps<{
  modelValue: boolean;
  type: string;
}>();

const emit = defineEmits(["update:modelValue", "cancel", "confirm"]);

const data = reactive({
  rules: {},
});

const { rules } = toRefs(data);

const readOnly = ref(true);
const radio = ref(1);
const radioOptions = ref([
  {
    label: "正则表达式",
    value: 1,
  },
  {
    label: "自定义方法",
    value: 2,
  },
  {
    label: "字典发现",
    value: 3,
  },
]);

watch(
  () => props.type,
  (newVal) => {
    readOnly.value = props.type === "edit" ? true : false;
  },
  { deep: true, immediate: true }
);
// 取消
const cancel = () => {
  if (props.type === "edit") {
    readOnly.value = true;
  } else if (props.type === "add") {
    emit("cancel");
  }
};

const confirm = () => {
  if (props.type === "edit") {
    readOnly.value = true;
  }
  emit("confirm");
};
</script>

<template>
  <div>
    <el-form ref="formRef" :model="modelValue" label-width="110px" :disabled="readOnly">
      <el-form-item label="发现规则名" required>
        <el-input v-model="modelValue.name"></el-input>
      </el-form-item>
      <el-form-item label="数据域">
        <div>{{ modelValue.name }}</div>
      </el-form-item>
      <el-form-item label="发现方式" required>
        <div style="width: 100%;background: #F3F5FA;">
          <el-input v-model="modelValue.name" v-if="readOnly"></el-input>
          <el-select v-model="modelValue.name" v-if="!readOnly">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="发现规则类型">
        <el-radio-group v-model="radio">
          <el-radio :label="item.value" v-for="(item,index) in radioOptions" :key="index">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="正则表达式" required v-if="radio === 1">
        <el-input v-model="modelValue.name"></el-input>
      </el-form-item>
      <el-form-item label="JAVA方法" required v-if="radio === 2">
        <el-input v-model="modelValue.name"></el-input>
      </el-form-item>
      <el-form-item label="字典分类" required v-if="radio === 3">
        <div style="width: 100%;background: #F3F5FA;">
          <el-input v-model="modelValue.name" v-if="readOnly"></el-input>
          <el-select v-model="modelValue.name" v-if="!readOnly">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="匹配方式" required v-if="radio === 3">
        <div style="width: 100%;background: #F3F5FA;">
          <el-input v-model="modelValue.name" v-if="readOnly"></el-input>
          <el-select v-model="modelValue.name" v-if="!readOnly">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="样例" required>
        <el-input v-model="modelValue.name"></el-input>
      </el-form-item>
      <el-form-item label="测试">
        <div style="width: 100%;display: flex;align-items: center;">
          <el-input v-model="modelValue.name" style="flex: 1;"></el-input>
          <el-button type="primary">测试</el-button>
        </div>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="modelValue.name"></el-input>
      </el-form-item>
      <el-form-item label="默认规则">
        <Switch :disabled="readOnly">
          <template #open>
            <span>是</span>
          </template>
          <template #close>
            <span>否</span>
          </template>
        </Switch>
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button type="primary" style="width: 150px;" @click="readOnly = false" v-if="readOnly">编辑</el-button>
      <el-button type="info" style="width: 150px;" @click="cancel" v-if="!readOnly">取消</el-button>
      <el-button type="primary" style="width: 150px;" @click="confirm" v-if="!readOnly">确认</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 8px;
}
.btn {
  padding: 20px;
  display: flex;
  justify-content: center;
}
</style>