<!--
 * @Description: 考核方案管理列表页面
 * @Author: 李亚光
 * @Date: 2023-09-14
 -->
<script lang="ts" setup name="RuleProgrammeList">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { delProgramme, getListPage } from '@/api/home/rule/programme'
import { getDictByCode } from '@/api/system/dict'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  planName: '', // 方案名称
  isEnable: '', // 是否禁用
  assObject: '', // 考核对象
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '考核方案名称',
    value: 'planName',
    align: 'center',
  },
  {
    text: '考核对象',
    value: 'assObjectName',
    align: 'center',
  },
  {
    text: '具体考核部门',
    value: 'assDept',
    align: 'center',
  },
  {
    text: '考核周期',
    value: 'cycleName',
    align: 'center',
  },
  {
    text: '是否启用',
    value: 'isEnableName',
    align: 'center',
  },
  {
    text: '考核等级评定',
    value: 'recoRatingName',
    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.map((item: any) => {
      return {
        ...item,
        assDept: item.depts.map((child: any) => child.fullName).join(','),
      }
    })
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  listQuery.planName = ''
  listQuery.isEnable = ''
  listQuery.assObject = ''
  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 $router = useRouter()
// 新建编辑操作
const handler = (row: any, type: string) => {
  $router.push({
    path: `/programmelist/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
    },
  })
}
// 删除
const delHandler = (row: any) => {
  ElMessageBox.confirm(
    `确定删除${row.planName}吗？一经删除，不可恢复！`,
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    delProgramme({ id: row.id }).then((res) => {
      ElMessage.success('操作成功')
      search()
    })
  })
}
// 考核对象下拉列表
const objectList = ref<{ id: string; value: string; name: string }[]>()
// 是否禁用下拉列表
const disableList = ref<{ id: string; value: string; name: string }[]>()
// 获取字典
const fetchSelectList = () => {
  getDictByCode('ass_object').then((res) => {
    objectList.value = res.data
  })
  getDictByCode('is_enable').then((res) => {
    disableList.value = res.data
  })
}
fetchSelectList()
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.planName" placeholder="考核方案名称" clearable />
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.assObject" placeholder="指标考核对象" clearable>
          <el-option v-for="item in objectList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <!-- <search-item>
        <el-input v-model.trim="listQuery.groupName" placeholder="具体考核部门" clearable />
      </search-item> -->
      <search-item>
        <el-select v-model.trim="listQuery.isEnable" placeholder="是否启用" clearable>
          <el-option v-for="item in disableList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <el-button v-if="proxy.hasPerm('/programme/add')" type="primary" @click="handler({}, 'create')">
          新增
        </el-button>
        <el-button v-if="proxy.hasPerm('/programme/import')" type="primary">
          导入
        </el-button>
        <el-button v-if="proxy.hasPerm('/programme/template')" type="primary">
          下载模板
        </el-button>
        <el-button v-if="proxy.hasPerm('/programme/delete')" type="primary">
          删除
        </el-button>
      </template>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" @change="changePage"
      >
        <template #columns>
          <el-table-column label="考核指标项目" align="center">
            <template #default="scope">
              <div class="container">
                <div v-for="(item, index) in scope.row.quotaInfos" :key="item.id" :class="`${index === 0 ? '' : 'item'}`">
                  {{ item.quotaProjectName }}
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="考核指标类型" align="center">
            <template #default="scope">
              <div class="container">
                <div v-for="(item, index) in scope.row.quotaInfos" :key="item.id" :class="`${index === 0 ? '' : 'item'}`">
                  {{ item.priIndicators }}
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="考核方案得分" align="center">
            <template #default="scope">
              {{ scope.row.score }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="handler(scope.row, 'detail')">
                详情
              </el-button>
              <el-button v-if="proxy.hasPerm('/programme/update')" link type="primary" size="small" @click="handler(scope.row, 'update')">
                编辑
              </el-button>
              <el-button v-if="proxy.hasPerm('/programme/delete')" link type="danger" size="small" @click="delHandler(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

<style lang="scss" scope>
.container {
  // display: flex;
  width: 100%;

  .item {
    float: left;
    border-top: 1px solid #ebeef5;
    text-align: center;
    width: 100%;
  }
}
</style>

