<template>
    <el-dialog
      v-model="visible"
      title="修改维修状态"
      width="500px"
      :close-on-click-modal="false"
      @close="onCancel"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="维修状态">
          <el-select v-model="form.status" placeholder="请选择维修状态">
            <el-option label="待维修" :value="1" />
            <el-option label="维修中" :value="2" />
            <el-option label="维修完成" :value="3" />
          </el-select>
        </el-form-item>
  
        <el-form-item label="维修人员" v-if="form.status === 2">
          <el-input v-model="form.repair_person" placeholder="请输入维修人员" />
        </el-form-item>
  
        <el-form-item label="维修结果" v-if="form.status === 3">
          <el-input v-model="form.repair_result" placeholder="请输入维修结果" />
        </el-form-item>
  
        <el-form-item label="维修费用" v-if="form.status === 3">
          <el-input-number v-model="form.repair_cost" :min="0" />
        </el-form-item>
  
        <!-- <el-form-item label="维修备注">
          <el-input type="textarea" v-model="form.repair_remark" :rows="2" />
        </el-form-item> -->
  
        <!-- <el-form-item label="维修附件">
          <el-input
            type="textarea"
            v-model="form.repair_attachments"
            placeholder="多个附件用换行分隔"
            :rows="2"
          />
        </el-form-item> -->
      </el-form>
  
      <template #footer>
        <el-button @click="onCancel">取消</el-button>
        <el-button type="primary" @click="onConfirm">提交</el-button>
      </template>
    </el-dialog>
  </template>
  
  <script setup lang="ts">
  import { ref, reactive,watch } from 'vue'
  import { ElMessage } from 'element-plus'
  import { request } from '/@/utils/service'
  
  const props = defineProps<{ id: number; visible: boolean }>()
  const emit = defineEmits(['update:visible', 'success'])

  console.log("props::::",props.id)
  
  const visible = ref(props.visible)
  watch(() => props.visible, val => (visible.value = val))
  watch(() => visible.value, val => emit('update:visible', val))
  
  const form = reactive({
    status: 1,
    repair_person: '',
    repair_result: '',
    repair_cost: 0,
    repair_remark: '',
    repair_attachments: ''
  })
  
  const onCancel = () => {
    visible.value = false
  }
  
  const onConfirm = async () => {
    try {
      const res = await request({
        url: `/api/system/device-damage-records/${props.id}/update_repair_status/`,
        method: 'post',
        data: form
      })
      if (res.code === 2000) {
        ElMessage.success('状态更新成功')
        emit('success')
        visible.value = false
      } else {
        ElMessage.error(res.msg || '更新失败')
      }
    } catch (err) {
      ElMessage.error('请求失败')
    }
  }
  </script>
  