<template>
  <div v-if="visible">
    <div class="transfer-form-dialog" @click.stop>
      <div class="transfer-form-dialog-header">
        <span class="transfer-form-dialog-title">转警申请</span>
        <el-button 
          type="text" 
          icon="el-icon-close" 
          size="mini"
          @click="handleClose"
          class="transfer-form-dialog-close"
        ></el-button>
      </div>
      
      <div class="transfer-form-dialog-body">
        <el-form 
          ref="transferForm" 
          :model="formData" 
          :rules="rules" 
          label-width="100px"
        >
          <el-form-item label="目标单位" prop="targetOrg">
            <el-input 
              v-model="formData.targetOrg" 
              placeholder="请输入目标单位"
            />
          </el-form-item>
          
          <el-form-item label="联系人" prop="contact">
            <el-input 
              v-model="formData.contact" 
              placeholder="请输入联系人"
            />
          </el-form-item>
          
          <el-form-item label="联系电话" prop="mobile">
            <el-input 
              v-model="formData.mobile" 
              placeholder="请输入联系电话"
            />
          </el-form-item>
          
          <el-form-item label="转警说明" prop="transferDesc">
            <el-input 
              v-model="formData.transferDesc" 
              type="textarea"
              :rows="3"
              placeholder="请输入转警说明/原因"
            />
          </el-form-item>
        </el-form>
      </div>
      
      <div class="transfer-form-dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button 
          type="primary" 
          :loading="submitting"
          @click="handleSubmit"
        >
          提交
        </el-button>
      </div>
    </div>
    
    <div class="transfer-form-dialog-backdrop" @click="handleClose"></div>
  </div>
</template>

<script>
export default {
  name: 'TransferFormDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    alertId: {
      type: [Number, String],
      default: null
    },
    submitting: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        targetOrg: '',
        contact: '',
        mobile: '',
        transferDesc: ''
      },
      rules: {
        targetOrg: [{ required: true, message: '请输入目标单位', trigger: 'blur' }],
        contact: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        mobile: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$|^0\d{2,3}-?\d{7,8}$/, message: '请输入正确的手机号或固定电话', trigger: 'blur' }
        ],
        transferDesc: [{ required: true, message: '请输入转警说明', trigger: 'blur' }]
      }
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.resetForm()
      }
    }
  },
  methods: {
    resetForm() {
      this.formData = {
        targetOrg: '',
        contact: '',
        mobile: '',
        transferDesc: ''
      }
      this.$nextTick(() => {
        if (this.$refs.transferForm) {
          this.$refs.transferForm.resetFields()
        }
      })
    },
    handleClose() {
      this.$emit('close')
    },
    handleSubmit() {
      this.$refs.transferForm.validate((valid) => {
        if (valid) {
          this.$emit('submit', this.formData)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.transfer-form-dialog {
  position: fixed;
  bottom: 100px;
  right: 30px;
  width: 400px;
  height: 500px;
  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;
}

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

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

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

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

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

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