<template>
  <el-dialog title="设备选择"
    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" label-width="68px">
          <el-form-item label="风机编号" prop="equipmentCode">
            <el-input
              v-model="queryParams.equipmentCode"
              placeholder="请输入风机编号"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="维保中心" prop="maintenanceCenter">
            <el-input
              v-model="queryParams.maintenanceCenter"
              placeholder="请输入维保中心"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="场站" prop="station">
            <el-input
              v-model="queryParams.station"
              placeholder="请输入场站"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </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="equipmentList" @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.equipmentId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
                </template>
            </el-table-column>          
          <el-table-column label="风机编号" align="center" key="equipmentCode" prop="equipmentCode" :show-overflow-tooltip="true" />
          <el-table-column label="维保中心" align="center" key="maintenanceCenter" prop="maintenanceCenter" :show-overflow-tooltip="true" />
          <el-table-column label="场站" align="center" key="station" prop="station" :show-overflow-tooltip="true" />
          <el-table-column label="品牌" align="center" key="brand" prop="brand" :show-overflow-tooltip="true" />
          <el-table-column label="机型" align="center" key="model" prop="model" :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 { listEquipment } from "@/api/gxt/equipment";

// 定义v-model
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

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 equipmentList = ref([])
const title = ref("")
const queryRef = ref(null)
const tableRef = ref(null)

// 监听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,
  equipmentCode: undefined,
  maintenanceCenter: undefined,
  station: undefined
})

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

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

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  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>