<template>
  <!-- 保存确认弹窗（无模式时） -->
  <el-dialog :model-value="visible" @update:model-value="$emit('update:visible', $event)" title="保存流程" width="600px">
    <el-form ref="saveFormRef" :model="saveForm" :rules="saveFormRules" label-width="100px">
      <el-form-item label="流程编码" prop="code">
        <el-input v-model="saveForm.code" />
      </el-form-item>
      <el-form-item label="流程名称" prop="name">
        <el-input v-model="saveForm.name" />
      </el-form-item>
      <el-form-item label="分类">
        <el-input v-model="saveForm.category" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="saveForm.description" type="textarea" />
      </el-form-item>
    </el-form>

    <div class="form-fields-section">
      <div class="form-fields-header">
        <span class="form-fields-title">流程表单字段</span>
        <el-button type="primary" size="small" :icon="Delete" @click="$emit('addField')">添加字段</el-button>
      </div>
      <el-table :data="formFields" size="small" border style="width: 100%">
        <el-table-column label="字段名" min-width="120">
          <template #default="{ $index }">
            <el-input v-model="formFields[$index].name" placeholder="英文标识" size="small" />
          </template>
        </el-table-column>
        <el-table-column label="显示名" min-width="120">
          <template #default="{ $index }">
            <el-input v-model="formFields[$index].label" placeholder="中文显示名" size="small" />
          </template>
        </el-table-column>
        <el-table-column label="类型" width="120">
          <template #default="{ $index }">
            <el-select v-model="formFields[$index].type" placeholder="类型" size="small" style="width: 100%">
              <el-option v-for="opt in fieldTypeOptions" :key="opt.value" :label="opt.label" :value="opt.value" />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="必填" width="70" align="center">
          <template #default="{ $index }">
            <el-checkbox v-model="formFields[$index].required" />
          </template>
        </el-table-column>
        <el-table-column label="多选" width="70" align="center">
          <template #default="{ $index }">
            <el-checkbox
              v-model="formFields[$index].multiple"
              :disabled="formFields[$index].type !== 'select'"
            />
          </template>
        </el-table-column>
        <el-table-column label="选项" width="90" align="center">
          <template #default="{ $index }">
            <el-button
              link
              type="primary"
              size="small"
              :disabled="formFields[$index].type !== 'select'"
              @click="$emit('openOptionDialog', $index)"
            >
              配置选项
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="70" align="center">
          <template #default="{ $index }">
            <el-button link type="danger" size="small" @click="$emit('removeField', $index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-empty v-if="formFields.length === 0" description="暂无字段，点击上方按钮添加" :image-size="60" style="padding: 10px 0;" />
    </div>

    <template #footer>
      <el-button @click="$emit('update:visible', false)">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确认保存</el-button>
    </template>
  </el-dialog>

  <!-- 下拉选项配置弹窗 -->
  <el-dialog :model-value="optionDialogVisible" @update:model-value="$emit('update:optionDialogVisible', $event)" title="配置下拉选项" width="500px">
    <div v-if="optionEditingField" class="option-edit-tip">
      字段：{{ optionEditingField.label || optionEditingField.name }}
      <el-checkbox v-model="optionEditingField.multiple" style="margin-left: 16px;">允许多选</el-checkbox>
    </div>
    <el-table :data="optionList" size="small" border>
      <el-table-column label="显示文本" min-width="140">
        <template #default="{ $index }">
          <el-input v-model="optionList[$index].label" placeholder="显示文本" size="small" />
        </template>
      </el-table-column>
      <el-table-column label="选项值" min-width="140">
        <template #default="{ $index }">
          <el-input v-model="optionList[$index].value" placeholder="选项值" size="small" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="70" align="center">
        <template #default="{ $index }">
          <el-button link type="danger" size="small" @click="$emit('removeOption', $index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" size="small" style="margin-top: 12px;" @click="$emit('addOption')">添加选项</el-button>
    <template #footer>
      <el-button @click="$emit('update:optionDialogVisible', false)">取消</el-button>
      <el-button type="primary" @click="$emit('saveOptions')">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, toRefs } from 'vue'
import { Delete } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import type { FormField, FormFieldOption } from '@/types/flow'

const props = defineProps<{
  visible: boolean
  optionDialogVisible: boolean
  saveForm: {
    code: string
    name: string
    category: string
    description: string
  }
  saveFormRules: FormRules
  formFields: FormField[]
  fieldTypeOptions: { label: string; value: string }[]
  optionEditingField: FormField | null
  optionList: FormFieldOption[]
}>()

const emit = defineEmits<{
  'update:visible': [value: boolean]
  'update:optionDialogVisible': [value: boolean]
  submit: []
  addField: []
  removeField: [index: number]
  openOptionDialog: [index: number]
  addOption: []
  removeOption: [index: number]
  saveOptions: []
}>()

const { visible, optionDialogVisible, saveForm, formFields, optionEditingField, optionList } = toRefs(props)
const { saveFormRules, fieldTypeOptions } = props

const saveFormRef = ref<FormInstance>()

async function handleSubmit() {
  const valid = await saveFormRef.value?.validate().catch(() => false)
  if (!valid) return
  emit('submit')
}
</script>

<style scoped>
.form-fields-section {
  margin-top: 16px;
  border-top: 1px solid #ebeef5;
  padding-top: 16px;
}
.form-fields-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.form-fields-title {
  font-weight: bold;
  color: #303133;
}
</style>
