<!-- 计量计划弹窗 -->
<script lang="ts" setup name="PlanListDialog">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import ApprovalDialog from './ApprovalDialog.vue'
import { cancelPlan, delPlan, editPlan, getListPage, submitPlan } from '@/api/eqpt/measurementPlan/paln'
import { getDictByCode } from '@/api/system/dict'
import { SCHEDULE } from '@/utils/scheduleDict'
const emits = defineEmits(['add'])
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  approvalStatus: '', //	申请状态(审批列表传)		false
  createDeptName: '', //	创建单位名称		false
  createTimeEnd: '', //	创建结束时间		false
  createTimeStart: '', //	创建开始时间		false
  formId: SCHEDULE.METERING_PLAN_APPROVAL, //	流程formId(待审批/已审批列表传)		false
  planCategory: '', //	计划分类（原始/追加）		false
  planName: '', //	计划名称		false
  planNo: '', //	计划编号		false
  planType: '', //	计划类型（年/季/月）		false
  offset: 1,
  limit: 5,
})
// 开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  listQuery.createTimeStart = ''
  listQuery.createTimeEnd = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.createTimeStart = `${newVal[0]} 00:00:00`
      listQuery.createTimeEnd = `${newVal[1]} 23:59:59`
    }
  }
})
const columns = ref([
  {
    text: '计划编号',
    value: 'planNo',
    align: 'center',
  },
  {
    text: '计划名称',
    value: 'planName',
    align: 'center',
  },
  {
    text: '计划分类',
    value: 'planCategoryName',
    align: 'center',
  },
  {
    text: '创建单位',
    value: 'createDeptName',
    align: 'center',
  },
  {
    text: '创建人',
    value: 'createUserName',
    align: 'center',
  },
  {
    text: '创建时间',
    value: 'createTime',
    align: 'center',
  },
  {
    text: '审批状态',
    value: 'approvalStatusName',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取列表数据
const fetchData = (isNowPage = true) => {
  list.value = []
  total.value = 0
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getListPage(listQuery, '全部').then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
// 获取计划分类列表
const planTypeList = ref()
const fetchTypeList = () => {
  getDictByCode('eqptPlanType').then((res) => {
    planTypeList.value = res.data
  })
}
fetchTypeList()
// 查询数据
const search = () => {
  fetchData(false)
}
search()
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.createTimeStart = ''
  listQuery.createTimeEnd = ''
  listQuery.offset = 1
  listQuery.limit = 20
  listQuery.planName = ''
  listQuery.planNo = ''
  listQuery.planCategory = ''
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
const select = ref(-1)
const dialogFormVisible = ref(false)
const initDialog = () => {
  select.value = -1
  dialogFormVisible.value = true
}
defineExpose({ initDialog })
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
}
// 确认
const submitForm = () => {
  if (select.value === -1) {
    ElMessage.warning('请先选择计划')
  }
  else {
    const plan = list.value[select.value]
    emits('add', plan)
    resetForm()
  }
}
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择设备" width="65%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.planNo" placeholder="计划编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.planName" placeholder="计划名称" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.planCategory" placeholder="计划分类" clearable>
          <el-option v-for="item in planTypeList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="datetimerange" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="创建开始时间" end-placeholder="创建结束时间"
        />
      </search-item>
    </search-area>
    <table-container>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="listLoading" :is-showmulti-select="false" @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="" width="55" align="center">
            <template #default="scope">
              <el-radio v-model="select" :label="scope.$index" :disabled="scope.row.isRadioDisabled" class="radio" />
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.nortable-header {
  ::v-deep(.el-table__body-wrapper) {
    display: none;
  }
}
</style>

