<template>
  <div class="clause-form bg-white p-6 rounded h-full overflow-y-auto">
    <div class="form-header flex items-center mb-8">
      <el-button link @click="$emit('cancel')">
        <el-icon><i-ep-arrow-left /></el-icon>
        返回
      </el-button>
      <span class="ml-4 font-bold text-lg">{{ initialData ? '编辑条款' : '创建条款' }}</span>
    </div>

    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
      class="max-w-4xl mx-auto"
    >
      <el-form-item label="条款名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入" />
      </el-form-item>

      <el-form-item label="条款类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择" class="w-full">
          <el-option label="身份风险" value="身份风险" />
          <el-option label="职业风险" value="职业风险" />
          <el-option label="能力评估" value="能力评估" />
        </el-select>
      </el-form-item>

      <el-form-item label="条款设置" prop="setting1">
        <el-select v-model="form.setting1" placeholder="请选择" class="w-full">
          <el-option label="必选" value="必选" />
          <el-option label="可选" value="可选" />
        </el-select>
      </el-form-item>

      <el-form-item label="条款设置" prop="setting2">
        <el-select v-model="form.setting2" placeholder="请选择" class="w-full">
          <el-option label="必选" value="必选" />
          <el-option label="可选" value="可选" />
        </el-select>
      </el-form-item>

      <el-form-item label="条款设置" prop="setting3">
        <el-select v-model="form.setting3" placeholder="请选择" class="w-full">
          <el-option label="必选" value="必选" />
          <el-option label="可选" value="可选" />
        </el-select>
      </el-form-item>

      <el-form-item label="条款描述" prop="description">
        <Editor v-model="form.description" :min-height="200" />
      </el-form-item>

      <el-form-item label="价格" prop="price">
        <div class="flex items-center gap-4">
          <el-input-number
            v-model="form.price"
            :precision="2"
            :step="0.1"
            :min="0"
            :disabled="form.isFree"
            placeholder="0.00"
          >
            <template #suffix>元</template>
          </el-input-number>
          <el-checkbox v-model="form.isFree" @change="handleFreeChange">免费</el-checkbox>
        </div>
      </el-form-item>

      <div class="flex justify-center gap-4 mt-12">
        <el-button @click="$emit('cancel')">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import Editor from '@/components/Editor/index.vue';
import type { FormInstance, FormRules } from 'element-plus';

const props = defineProps<{
  initialData?: any;
}>();

const emit = defineEmits(['cancel', 'submit']);

const formRef = ref<FormInstance>();
const form = reactive({
  id: '',
  name: '',
  type: '',
  setting1: '可选',
  setting2: '可选',
  setting3: '可选',
  description: '',
  price: 0,
  isFree: false,
  status: 1
});

const rules = reactive<FormRules>({
  name: [{ required: true, message: '请输入条款名称', trigger: 'blur' }],
  type: [{ required: true, message: '请选择条款类型', trigger: 'change' }],
  setting1: [{ required: true, message: '请选择条款设置', trigger: 'change' }],
  price: [{ required: true, message: '请输入价格', trigger: 'blur' }]
});

onMounted(() => {
  if (props.initialData) {
    Object.assign(form, props.initialData);
    // Assuming setting maps to setting1 for simplicity in this mock
    form.setting1 = props.initialData.setting || '可选';
    form.price = parseFloat(props.initialData.price) || 0;
    form.isFree = form.price === 0;
  }
});

const handleFreeChange = (val: boolean) => {
  if (val) {
    form.price = 0;
  }
};

const handleSubmit = async () => {
  if (!formRef.value) return;
  await formRef.value.validate((valid) => {
    if (valid) {
      emit('submit', { 
        ...form, 
        setting: form.setting1, // Use setting1 as the main setting for the list
        price: form.price.toString() 
      });
    }
  });
};
</script>

<style scoped lang="scss">
.clause-form {
  :deep(.el-input-number) {
    width: 180px;
  }
}
</style>
