<template>
  <Dialog v-model="visible" type="sm" title="任务反馈" class="box1" height="650px" hide-footer>
    <div class="dialog-content">
      <div class="dialog-body">
        <el-form ref="taskForm" :model="newTask">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="任务描述:">
                <div class="display-field">{{ newTask.task_description }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="任务类型:">
                <div class="display-field">{{ get_task_type_text(newTask.task_type) }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="执行人:">
                <div class="display-field">{{ newTask.registrar }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="执行单位:">
                <div class="display-field">{{ newTask.unit_name }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="预估完成时间:">
                <div class="display-field">{{ newTask.complete_time }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="图片:">
                <div class="display-field">{{ newTask.complete_time }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="文件:">
                <div class="display-field">{{ newTask.complete_time }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="反馈人:">
                <div class="display-field">{{ newTask.registrar }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="反馈内容:">
                <el-input v-model="newTask.feedback_content" class="custom-input2" clearable placeholder="请输入反馈内容"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="完成进度:">
                <el-select
                  v-model="newTask.processing_status"
                  class="custom-select"
                  size="large"
                  :teleported="false"
                  popper-class="custom-select-popper"
                >
                  <el-option v-for="unit in units" :key="unit" :label="unit" :value="unit"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="上传图片:">
                <!--                <el-upload-->
                <!--                  v-model="newTask.attachments"-->
                <!--                  action="/uploadAttachments"-->
                <!--                  list-type="picture-card"-->
                <!--                  :teleported="false"-->
                <!--                  auto-upload="true"-->
                <!--                  limit="9"-->
                <!--                  :teleported="false"-->
                <!--                  popper-class="custom-uploadper"-->
                <!--                  class="custom-upload"-->
                <!--                  :teleported="false"-->
                <!--                  clearable-->
                <!--                  filterable-->
                <!--                >-->
                <!--                  <el-upload-list-item v-for="file in newTask.attachments" :key="file.name">-->
                <!--                    <el-icon name="el-icon-upload" class="upload-icon"></el-icon>-->
                <!--                    <el-button type="danger" size="mini" @click="removeAttachment(file)">移除</el-button>-->
                <!--                  </el-upload-list-item>-->
                <!--                </el-upload>-->
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="footer">
        <div class="flex">
          <div class="common-btn-primary" :disabled="!isFormValid" @click="confirmRegister">确定</div>
          <div class="common-btn" @click="closeDialog">取消</div>
        </div>
      </div>
    </div>
  </Dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, defineEmits, computed, watch } from 'vue';
import { updateTaskRegistration } from '@/api/emergencyCommandMap/JointDuty.ts';

const props = defineProps({
  modelValue: { type: Boolean, required: true },
  task: { type: Object, default: () => ({ task_id: '', task_description: '', unit_name: '', registrar: '', processing_status: '' }) },
  eventId: { type: String, required: true }
});

const emit = defineEmits(['update:modelValue', 'update-success']);
const newTask = reactive({ ...props.task });
const units = ref(['待处理', '已完成']);

const opt_task_type = [
  { text: "全部", value: "" },
  { text: "事件处置", value: "0" },
  { text: "防范措施", value: "1" },
  { text: "险情处理", value: "2" },
  { text: "督办任务", value: "3" }
];

const get_task_type_text = (val) => {
  return opt_task_type.find(item => item.value == val).text
}

const visible = computed({
  get: () => props.modelValue,
  set: (val) => {
    emit('update:modelValue', val);
    if (!val) resetForm();
  }
});
const isFormValid = computed(() => newTask.registrar !== '');

const resetForm = () => {
  newTask.task_description = '';
  newTask.unit_name = '';
  newTask.registrar = '';
  newTask.processing_status = '';
};

// 任务进度更新
const confirmRegister = async () => {
  console.log('任务进度更新:', newTask);
  try {
    const response = await updateTaskRegistration({
      task_id: newTask.task_id,
      processing_status: newTask.processing_status,
      event_code: props.eventId
    });
    if (response.code === 200) {
      emit('update-success', newTask);
      closeDialog();
    } else {
      console.error('任务进度更新失败:', response.msg);
    }
  } catch (error) {
    console.error('请求任务进度更新失败:', error);
  }
};

const closeDialog = () => {
  visible.value = false;
};

watch(
  () => props.task,
  (newTaskData) => {
    Object.assign(newTask, newTaskData);
  },
  { immediate: true }
);
</script>

<style lang="scss" scoped>
.custom-select {
  //width: 500px !important; // 使用 !important 强制覆盖默认样式
}

.dialog-body {
  padding: 20px;
  :deep(.el-form-item__label) {
    font-size: 16px !important;
    color: #ffffff !important;
  }
}
/* 新增样式，用于显示字段 */
.display-field {
  padding: 0 12px;
  //border: 1px solid #dcdfe6; /* 确保边框颜色与输入框一致 */
  border-radius: 4px;
  //background-color: #f5f7fa;
  // 背景颜色透明
  background-color: transparent;
  min-height: 32px;
  line-height: 32px;
  font-size: 16px; /* 确保字体大小与输入框一致 */
  color: #ffffff; /* 确保字体颜色与输入框一致 */
  //字体加粗
  //font-weight: bold;
}
.footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;

  .flex {
    display: flex;
    align-items: center;

    .common-btn-primary,
    .common-btn {
      margin-left: 10px;
      cursor: pointer;
    }
  }
}
</style>
