<template>
  <el-dialog title="MIS工单选择"
    v-model="showFlag"
    :modal= false
    width="80%"
    center
  >
    <el-row :gutter="20">
      <!--设备数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" >
          <el-form-item label="MIS工单编码" prop="misNo">
            <el-input
                v-model="queryParams.misNo"
                placeholder="请输入MIS系统编码"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="风机编号" prop="pcsDeviceName">
            <el-input
              v-model="queryParams.pcsDeviceName"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
              disabled
            />
          </el-form-item>
          <el-form-item label="场站" prop="pcsStationName">
            <el-input
              v-model="queryParams.pcsStationName"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
              disabled
            />
          </el-form-item>
          <el-form-item label="结束时间" prop="realEndTime">
            <el-date-picker
              v-model="realEndTimeRange"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD"
              style="width: 240px"
              @change="handleDateChange"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="misNoList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick" ref="tableRef">
            <el-table-column  width="55" align="center" >
                <template #default="scope">
                    <el-radio v-model="selectedId" :label="scope.row.misNo" @change="handleRowChange(scope.row)">{{""}}</el-radio>
                </template>
            </el-table-column>
          <el-table-column label="MIS工单编码" align="center" key="misNo" prop="misNo" :show-overflow-tooltip="true" min-width="100" />
          <el-table-column label="风机编号" align="center" key="pcsDeviceName" prop="pcsDeviceName" :show-overflow-tooltip="true" />
          <el-table-column label="场站" align="center" key="pcsStationName" prop="pcsStationName" :show-overflow-tooltip="true" />
          <el-table-column label="检修内容" align="center" key="content" prop="content" :show-overflow-tooltip="true" />
          <el-table-column label="工作票号" align="center" key="workPermitNum" prop="workPermitNum" :show-overflow-tooltip="true" min-width="120" />
          <el-table-column label="状态" align="center" key="workOrderStatus" prop="workOrderStatus" :show-overflow-tooltip="true" />
<!--          <el-table-column label="计划开始时间" align="center" key="planStartTime" prop="planStartTime" :show-overflow-tooltip="true" />-->
<!--          <el-table-column label="计划结束时间" align="center" key="planEndTime" prop="planEndTime" :show-overflow-tooltip="true" />-->
          <el-table-column label="开始时间" align="center" key="realStartTime" prop="realStartTime" :show-overflow-tooltip="true" />
          <el-table-column label="结束时间" align="center" key="realEndTime" prop="realEndTime" :show-overflow-tooltip="true" />
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <template #footer>
        <div class="dialog-footer">
            <el-button type="primary" @click="confirmSelect">确 定</el-button>
            <el-button @click="handleCancel">取 消</el-button>
        </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, watch, onMounted, nextTick } from 'vue'
import { listMisInfo } from "@/api/gxt/misInfo.js";

// 定义v-model
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  pcsStationName: {
    type: String,
    default: ''
  },
  pcsDeviceName: {
    type: String,
    default: ''
  },
  pauseTime: {
    type: String,
    default: ''
  },
  restartTime: {
    type: String,
    default: ''
  },
  workOrderStatus: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update:modelValue', 'onSelected'])

// 获取组件实例
const { proxy } = getCurrentInstance()

// 定义变量
const showFlag = ref(false)
const loading = ref(true)
const selectedId = ref(null)
const selectedRow = ref(null)
const single = ref(true)
const multiple = ref(true)
const showSearch = ref(true)
const total = ref(0)
const misNoList = ref([])
const title = ref("")
const queryRef = ref(null)
const tableRef = ref(null)
const realEndTimeRange = ref([]) // 用于存储日期范围选择的值

// 监听modelValue变化
watch(() => props.modelValue, (newVal) => {
  showFlag.value = newVal
  // 无论对话框打开还是关闭，都重置选择
  selectedId.value = null
  selectedRow.value = null
})

// 监听showFlag变化
watch(showFlag, (newVal) => {
  emit('update:modelValue', newVal)
  // 无论对话框打开还是关闭，都重置选择
  selectedId.value = null
  selectedRow.value = null
})

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  misNo: undefined,
  pcsDeviceName: props.pcsDeviceName, // 使用 props 的初始值
  pcsStationName: props.pcsStationName, // 使用 props 的初始值
  workOrderStatus: '结束,作业结束,工作票终结许可人确认,作业中,签发人签发,编辑,互保人签字,运行调控中心',
  beginRealEndTime: undefined, // 添加开始时间参数
  endRealEndTime: undefined,    // 添加结束时间参数
  pauseTime: undefined, // 使用 props 的初始值
  restartTime: undefined, // 使用 props 的初始值
})

// 组件挂载时查询数据
onMounted(() => {
  getList();
})

/** 处理日期范围变化 */
function handleDateChange(val) {
  if (val && val.length === 2) {
    queryParams.beginRealEndTime = val[0]
    queryParams.endRealEndTime = val[1]
  } else {
    queryParams.beginRealEndTime = undefined
    queryParams.endRealEndTime = undefined
  }
}

/** 查询设备列表 */
function getList() {
  loading.value = true
  listMisInfo(queryParams).then(response => {
      misNoList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  realEndTimeRange.value = []
  handleQuery();
}

function handleCurrent(row){
    if(row){
        selectedRow.value = row;
    }
}

//行双击选中
function handleRowDbClick(row){
  if(row){
    selectedRow.value = row;
    emit('onSelected', selectedRow.value);
    showFlag.value = false;
  }
}

// 单选选中数据
function handleRowChange(row) {
  if(row){
    selectedRow.value = row;
  }
}

//确定选中
function confirmSelect(){
    if(selectedId.value == null || selectedId.value == 0){
        proxy.$notify({
            title:'提示',
            type:'warning',
            message: '请至少选择一条数据!'
        });
        return;
    }
    emit('onSelected', selectedRow.value);
    showFlag.value = false;
}

// 取消按钮
function handleCancel() {
  selectedId.value = null
  selectedRow.value = null
  showFlag.value = false
}
</script>
