<template>
    <div class="main-container">
        <el-card class="box-card !border-none" shadow="never">

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

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="contractsTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('studentId')" prop="student_id">
                        <el-select class="w-[280px]" v-model="contractsTable.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('title')" prop="title">
                        <el-input v-model="contractsTable.searchParam.title" :placeholder="t('titlePlaceholder')" />
                    </el-form-item>
                    <el-form-item :label="t('startDate')" prop="start_date">
                        <el-date-picker v-model="contractsTable.searchParam.start_date" type="datetimerange" format="YYYY-MM-DD hh:mm:ss"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')" />
                    </el-form-item>
                    
                    <el-form-item :label="t('endDate')" prop="end_date">
                        <el-date-picker v-model="contractsTable.searchParam.end_date" type="datetimerange" format="YYYY-MM-DD hh:mm:ss"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')" />
                    </el-form-item>
                    
                    
                    <el-form-item :label="t('status')" prop="status">
                        <el-select class="w-[280px]" v-model="contractsTable.searchParam.status" clearable :placeholder="t('statusPlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in statusList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item :label="t('createTime')" prop="create_time">
                        <el-date-picker v-model="contractsTable.searchParam.create_time" type="datetimerange" format="YYYY-MM-DD hh:mm:ss"
                            :start-placeholder="t('startDate')" :end-placeholder="t('endDate')" />
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadContractsList()">{{ 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="contractsTable.data" size="large" v-loading="contractsTable.loading">
                    <template #empty>
                        <span>{{ !contractsTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="id" :label="t('id')" 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="title" :label="t('title')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="start_date" :label="t('startDate')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column prop="end_date" :label="t('endDate')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('status')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in statusList">
                                <div v-if="item.value == row.status">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    
                     <el-table-column :label="t('createTime')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            {{ row.create_time || '' }}
                        </template>
                    </el-table-column>
                    
                     <el-table-column :label="t('updateTime')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            {{ row.update_time || '' }}
                        </template>
                    </el-table-column>
                    

                    <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="contractsTable.page" v-model:page-size="contractsTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="contractsTable.total"
                        @size-change="loadContractsList()" @current-change="loadContractsList" />
                </div>
            </div>

            
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import { getContractsList, deleteContracts, getWithStudentsList } from '@/addon/zhjw/api/contracts'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let contractsTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "student_id":"",
      "title":"",
      "start_date":[],
      "end_date":[],
      "status":"",
      "create_time":[]
    }
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据
    const statusList = ref([] as any[])
    const statusDictList = async () => {
    statusList.value = await (await useDictionary('zhjw_contracts_status')).data.dictionary
    }
    statusDictList();
    const is_deletedList = ref([] as any[])
    const is_deletedDictList = async () => {
    is_deletedList.value = await (await useDictionary('is_radio')).data.dictionary
    }
    is_deletedDictList();


/**
 * 获取合同管理列表
 */
const loadContractsList = (page: number = 1) => {
    contractsTable.loading = true
    contractsTable.page = page

    getContractsList({
        page: contractsTable.page,
        limit: contractsTable.limit,
         ...contractsTable.searchParam
    }).then(res => {
        contractsTable.loading = false
        contractsTable.data = res.data.data
        contractsTable.total = res.data.total
    }).catch(() => {
        contractsTable.loading = false
    })
}
loadContractsList()

const router = useRouter()

/**
 * 添加合同管理
 */
const addEvent = () => {
    router.push('/contracts/contracts_edit')
}

/**
 * 编辑合同管理
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/contracts/contracts_edit?id='+data.id)
}

/**
 * 删除合同管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('contractsDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteContracts(id).then(() => {
            loadContractsList()
        }).catch(() => {
        })
    })
}

    
    const studentIdList = ref([])
    const setStudentIdList = async () => {
    studentIdList.value = await (await getWithStudentsList({})).data
    }
    setStudentIdList()

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    loadContractsList()
}
</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>
