<!--
 * @Description: 考核指标弹窗
 * @Author: 李亚光
 * @Date: 2023-09-15
 -->
<script lang="ts" setup name="SubpackageDialog">
import { ElMessage } from 'element-plus'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import { getListPage } from '@/api/home/rule/proxy'
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
const listQuery = reactive({
  assDept: '', // 责任考核单位
  priIndicators: '', // 一级考核指标
  quotaNo: '', // 指标编码
  quotaProject: '', // 指标项目
  quotaType: '', // 指标类型
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '考核指标编号',
    value: 'quotaNo',
    align: 'center',
  },
  {
    text: '考核指标项目',
    value: 'quotaProjectName',
    align: 'center',
  },
  {
    text: '考核指标类型',
    value: 'quotaTypeName',
    align: 'center',
  },
  {
    text: '一级考核指标',
    value: 'priIndicators',
    align: 'center',
  },
  {
    text: '指标数据来源',
    value: 'dataSourceName',
    align: 'center',
  },
  {
    text: '指标责任考核单位',
    value: 'assDeptName',
    align: 'center',
  },
  {
    text: '指标计算规则',
    value: 'ruleDesc',
    align: 'center',
  },
  {
    text: '考核分值',
    value: 'score',
    align: 'center',
  },
  {
    text: '备注',
    value: 'remarks',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取数据
const fetchData = (isNowPage = true) => {
  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
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  listQuery.assDept = ''
  listQuery.priIndicators = ''
  listQuery.quotaNo = ''
  listQuery.quotaProject = ''
  listQuery.quotaType = ''
  listQuery.offset = 1
  listQuery.limit = 20
  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 projectList = ref<{ id: string; value: string; name: string }[]>()
// 指标类型下拉列表
const typeList = ref<{ id: string; value: string; name: string }[]>()
// 获取字典
const fetchSelectList = () => {
  getDictByCode('quota_project').then((res) => {
    projectList.value = res.data
  })
  getDictByCode('quota_type').then((res) => {
    typeList.value = res.data
  })
}
fetchSelectList()

// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 点击保存
const submitForm = () => {
  if (selectList.value.length) {
    emits('add', selectList.value)
    dialogFormVisible.value = false
    selectList.value = []
    reset()
  }
  else {
    ElMessage.warning('请先选择考核指标')
  }
}
// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
  fetchData()
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择考核指标" width="75%">
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.quotaNo" placeholder="考核指标编号" clearable />
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.quotaProject" placeholder="考核指标项目" clearable>
          <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.quotaType" placeholder="考核指标类型" clearable>
          <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.priIndicators" placeholder="一级考核指标" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.assDept" placeholder="责任考核单位" clearable />
      </search-item>
    </search-area>
    <table-container>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" :is-multi="true" @change="changePage" @multi-select="multiSelect"
      />
    </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>
:deep(.el-radio__label) {
  display: none;
}
</style>
