<template>
	<el-dialog
	  v-model="showDialog"
	  title="体测列表"
	  width="80%"
	  class="diy-dialog-wrap"
	  :destroy-on-close="true"
	>
    <div class="main-container">
        <el-card class="box-card !border-none" shadow="never">

            <div class="flex justify-between items-center">
                <span class="text-lg">体测列表</span>
                <el-button type="primary" @click="addEvent">
                    {{ t('addPhysicalTest') }}
                </el-button>
            </div>

<!--            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="physicalTestTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('resourceId')" prop="resource_id">
                        <el-select class="w-[280px]" v-model="physicalTestTable.searchParam.resource_id" clearable :placeholder="t('resourceIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in resourceIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('studentId')" prop="student_id">
                        <el-select class="w-[280px]" v-model="physicalTestTable.searchParam.student_id" clearable :placeholder="t('studentIdPlaceholder')">
                           <el-option
                                       v-for="(item, index) in studentIdList"
                                       :key="index"
                                       :label="item['name']"
                                       :value="item['id']"
                                   />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('height')" prop="height">
                        <el-input v-model="physicalTestTable.searchParam.height" :placeholder="t('heightPlaceholder')" />
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="loadPhysicalTestList()">{{ t('search') }}</el-button>
                        <el-button @click="resetForm(searchFormRef)">{{ t('reset') }}</el-button>
                    </el-form-item>
                </el-form>
            </el-card> -->

            <div class="mt-[10px]">
                <el-table :data="physicalTestTable.data" size="large" v-loading="physicalTestTable.loading">
                    <template #empty>
                        <span>{{ !physicalTestTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="resource_id_name" :label="t('resourceId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="student_id_name" :label="t('studentId')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="height" :label="t('height')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="created_at" :label="t('createdAt')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="updated_at" :label="t('updatedAt')" min-width="120" :show-overflow-tooltip="true"/>
                    

                    <el-table-column :label="t('operation')" fixed="right" min-width="120">
                       <template #default="{ row }">
                           <el-button type="primary" link @click="editEvent(row)">{{ t('edit') }}</el-button>
                           <!-- <el-button type="primary" link @click="deleteEvent(row.id)">{{ t('delete') }}</el-button> -->
                       </template>
                    </el-table-column>

                </el-table>
                <div class="mt-[16px] flex justify-end">
                    <el-pagination v-model:current-page="physicalTestTable.page" v-model:page-size="physicalTestTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="physicalTestTable.total"
                        @size-change="loadPhysicalTestList()" @current-change="loadPhysicalTestList" />
                </div>
            </div>

            <edit ref="editPhysicalTestDialog" @complete="loadPhysicalTestList" />
        </el-card>
    </div>

  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import { getPhysicalTestList, deletePhysicalTest, getWithCustomerResourcesList, getWithStudentList, getWithPersonnelList } from '@/app/api/physical_test'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import Edit from '@/app/views/physical_test/components/physical-test-edit.vue'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;
let showDialog = ref(false)



let physicalTestTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "resource_id":"",
      "student_id":"",
      "height":""
    }
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据
    

/**
 * 获取体测列表
 */
const loadPhysicalTestList = (page: number = 1) => {
    physicalTestTable.loading = true
    physicalTestTable.page = page

    getPhysicalTestList({
        page: physicalTestTable.page,
        limit: physicalTestTable.limit,
         ...physicalTestTable.searchParam
    }).then(res => {
        physicalTestTable.loading = false
        physicalTestTable.data = res.data.data
        physicalTestTable.total = res.data.total
    }).catch(() => {
        physicalTestTable.loading = false
    })
}


const router = useRouter()

const editPhysicalTestDialog: Record<string, any> | null = ref(null)

/**
 * 添加体测
 */
const addEvent = () => {
    editPhysicalTestDialog.value.setFormData({'resource_id':physicalTestTable.searchParam.resource_id})
    editPhysicalTestDialog.value.showDialog = true
}


/**
 * 编辑体测
 * @param data
 */
const editEvent = (data: any) => {
    // router.push('/physical_test/physical_test_edit?id='+data.id)
	editPhysicalTestDialog.value.setFormData(data)
	editPhysicalTestDialog.value.showDialog = true
}

/**
 * 删除体测
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('physicalTestDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deletePhysicalTest(id).then(() => {
            loadPhysicalTestList()
        }).catch(() => {
        })
    })
}

    
    const resourceIdList = ref([])
    const setResourceIdList = async () => {
    resourceIdList.value = await (await getWithCustomerResourcesList({})).data
    }
    setResourceIdList()
    const studentIdList = ref([])
    const setStudentIdList = async () => {
    studentIdList.value = await (await getWithStudentList({})).data
    }
    setStudentIdList()
    const coachIdList = ref([])
    const setCoachIdList = async () => {
    coachIdList.value = await (await getWithPersonnelList({})).data
    }
    setCoachIdList()

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    loadPhysicalTestList()
}


const setFormData = async (row: any = null) => {
    // Object.assign(formData, initialFormData)
	physicalTestTable.searchParam.resource_id = row.resource_id
	
	console.log(physicalTestTable.searchParam.resource_id);
	
	loadPhysicalTestList()
}
defineExpose({
  showDialog,
  setFormData
})
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
		word-break: break-all;
		text-overflow: ellipsis;
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
</style>
