<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="visible"
    width="800px"
    :before-close="handleClose"
    :close-on-click-modal="false"
  >
    <el-form
      ref="projectForm"
      :model="form"
      :rules="rules"
      label-width="120px"
      size="small"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="项目名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入项目名称"
              :disabled="mode === 'view'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预算年度" prop="budget_year_id">
            <el-select
              v-model="form.budget_year_id"
              placeholder="选择预算年度"
              :disabled="mode === 'view'"
              style="width: 100%"
            >
              <el-option
                v-for="item in yearOptions"
                :key="item.value"
                :label="item.year + '年'"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="项目类型" prop="project_type">
            <el-select
              v-model="form.project_type"
              placeholder="选择项目类型"
              :disabled="mode === 'view'"
              style="width: 100%"
            >
              <el-option
                v-for="item in projectTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目状态" prop="package_status">
            <el-select
              v-model="form.package_status"
              placeholder="选择项目状态"
              :disabled="mode === 'view'"
              style="width: 100%"
            >
              <el-option
                v-for="item in packageStatusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="项目描述" prop="description">
            <el-input
              v-model="form.description"
              type="textarea"
              :rows="3"
              placeholder="请输入项目描述"
              :disabled="mode === 'view'"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="预算金额" prop="budget_amount">
            <el-input-number
              v-model="form.budget_amount"
              :min="0"
              :precision="1"
              :step="0.1"
              style="width: 100%"
              :disabled="mode === 'view'"
              placeholder="请输入预算金额"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="manager">
            <el-input
              v-model="form.manager"
              placeholder="请输入负责人姓名"
              :disabled="mode === 'view'"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        v-if="mode !== 'view'"
        type="primary"
        @click="handleSubmit"
        :loading="submitting"
      >
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'ProjectForm',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    mode: {
      type: String,
      default: 'create', // create, edit, view
      validator: value => ['create', 'edit', 'view'].includes(value)
    },
    projectData: {
      type: Object,
      default: () => ({})
    },
    yearOptions: {
      type: Array,
      default: () => []
    },
    projectTypeOptions: {
      type: Array,
      default: () => []
    },
    packageStatusOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      submitting: false,
      form: {
        name: '',
        budget_year_id: '',
        project_type: '',
        package_status: '',
        description: '',
        budget_amount: 0,
        manager: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        budget_year_id: [
          { required: true, message: '请选择预算年度', trigger: 'change' }
        ],
        project_type: [
          { required: true, message: '请选择项目类型', trigger: 'change' }
        ],
        package_status: [
          { required: true, message: '请选择项目状态', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    dialogTitle() {
      const titles = {
        create: '新建项目',
        edit: '编辑项目',
        view: '查看项目'
      }
      return titles[this.mode] || '项目管理'
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.initForm()
      }
    },
    projectData: {
      handler(val) {
        if (val && Object.keys(val).length > 0) {
          this.initForm()
        }
      },
      deep: true
    }
  },
  methods: {
    initForm() {
      if (this.mode === 'create') {
        this.form = {
          name: '',
          budget_year_id: '',
          project_type: '',
          package_status: '',
          description: '',
          budget_amount: 0,
          manager: ''
        }
      } else {
        this.form = { ...this.projectData }
      }
      this.$nextTick(() => {
        this.$refs.projectForm && this.$refs.projectForm.clearValidate()
      })
    },
    handleClose() {
      this.$emit('update:visible', false)
      this.$emit('close')
    },
    async handleSubmit() {
      try {
        await this.$refs.projectForm.validate()
        this.submitting = true
        
        this.$emit('submit', {
          mode: this.mode,
          data: this.form
        })
        
        this.handleClose()
      } catch (error) {
        console.error('表单验证失败:', error)
      } finally {
        this.submitting = false
      }
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
