<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
            v-for="dict in sys_clause_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.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>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
      </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';
import { useDict } from '@/utils/dict';

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

const { sys_clause_type } = useDict('sys_clause_type');

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

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

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

onMounted(() => {
  if (props.initialData) {
    Object.assign(form, props.initialData);
    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, 
        price: form.price.toString() 
      });
    }
  });
};
</script>

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