<template>
  <div v-if="visible">
    <div class="convert-form-dialog" @click.stop>
      <div class="convert-form-dialog-header">
        <span class="convert-form-dialog-title">转事件申请</span>
        <el-button 
          type="text" 
          icon="el-icon-close" 
          size="mini"
          @click="handleClose"
          class="convert-form-dialog-close"
        ></el-button>
      </div>
      
      <div class="convert-form-dialog-body">
        <el-form 
          ref="convertForm" 
          :model="formData" 
          :rules="rules" 
          label-width="100px"
        >
          <el-form-item label="转事件原因" prop="convertReason">
            <el-input 
              v-model="formData.convertReason" 
              type="textarea"
              :rows="4"
              placeholder="请输入转事件原因说明"
            />
          </el-form-item>
          
          <el-form-item v-if="reviewRequired !== null" label="审核要求">
            <el-tag :type="reviewRequired ? 'warning' : 'success'" size="small">
              {{ reviewRequired ? '需要审核' : '无需审核' }}
            </el-tag>
            <span class="review-hint">
              （{{ reviewRequired ? '一级、二级警情需要审核' : '三级、四级警情无需审核' }}）
            </span>
          </el-form-item>
        </el-form>
      </div>
      
      <div class="convert-form-dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button 
          type="primary" 
          :loading="submitting"
          @click="handleSubmit"
        >
          提交
        </el-button>
      </div>
    </div>
    
    <div class="convert-form-dialog-backdrop" @click="handleClose"></div>
  </div>
</template>

<script>
export default {
  name: 'ConvertFormDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    alertId: {
      type: [Number, String],
      default: null
    },
    alertInitialLevel: {
      type: String,
      default: null
    },
    submitting: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        convertReason: ''
      },
      rules: {
        convertReason: [{ required: true, message: '请输入转事件原因说明', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 根据警情等级判断是否需要审核：一级、二级需要审核，三级、四级不需要审核
    reviewRequired() {
      if (!this.alertInitialLevel) {
        return null
      }
      // level1, level2 需要审核；level3, level4 不需要审核
      return this.alertInitialLevel === 'level1' || this.alertInitialLevel === 'level2'
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.resetForm()
      }
    }
  },
  methods: {
    resetForm() {
      this.formData = {
        convertReason: ''
      }
      this.$nextTick(() => {
        if (this.$refs.convertForm) {
          this.$refs.convertForm.resetFields()
        }
      })
    },
    handleClose() {
      this.$emit('close')
    },
    handleSubmit() {
      this.$refs.convertForm.validate((valid) => {
        if (valid) {
          // 添加 reviewRequired 字段
          const submitData = {
            ...this.formData,
            reviewRequired: this.reviewRequired
          }
          this.$emit('submit', submitData)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.convert-form-dialog {
  position: fixed;
  bottom: 100px;
  right: 30px;
  width: 400px;
  height: 400px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 2001;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.convert-form-dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #ebeef5;
  background: #f5f7fa;
  flex-shrink: 0;
}

.convert-form-dialog-title {
  font-weight: 600;
  font-size: 16px;
  color: #303133;
}

.convert-form-dialog-close {
  padding: 0;
  color: #909399;
}

.convert-form-dialog-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.review-hint {
  margin-left: 8px;
  font-size: 12px;
  color: #909399;
}

.convert-form-dialog-footer {
  padding: 12px 16px;
  border-top: 1px solid #ebeef5;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-shrink: 0;
}

.convert-form-dialog-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2000;
  background: transparent;
}
</style>