<template>
  <!-- 搜索 -->
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      <TenantSelect v-model="queryParams.tenantIds" placeholder="请选择机构名称" prop="tenantIds"/>
      <el-form-item label="长者姓名" prop="elderName">
        <el-input @keyup.enter="handleQuery" v-model="queryParams.elderName" placeholder="请输入长者姓名" class="!w-240px" />
      </el-form-item>
      <el-form-item label="合同状态" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" class="!w-240px">
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.CONTRACT_TYPE)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入住类型" prop="inStatusType">
        <el-select v-model="queryParams.inStatusType" placeholder="请选择" class="!w-240px">
          <el-option
            v-for="(dict, index) in getStrDictOptions(DICT_TYPE.IN_STATUS_TYPE)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <TabBarBtn />
    <Table2
      v-loading="loading"
      :data="list"
      :columns="ContractColumns"
      :queryParams="queryParams"
      :opWidth="240"
      @edit="(arg) => handleRenewal(arg, 1)"
      @upload="openForm"
      @check="(arg) => handleRenewal(arg, 2)"
      @detail="openDetail"
    />

    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <Form ref="formRef" @success="getList" />
  <Renewal ref="renewalRef" @success="getList" />
  <Detail ref="detailRef" />
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import { getElderlyContract } from '@/api/elderly/elder/contract'
import { ContractColumns } from '../column'
import Form from './Form.vue'
import Renewal from './Renewal.vue'
import Detail from './Detail.vue'
import { useUserStore } from '@/store/modules/user'
defineOptions({ name: 'Contract' })
const userStore = useUserStore()
const message = useMessage() // 消息弹窗
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  elderName: undefined,
  type: undefined,
  inStatusType: undefined,
  tenantIds: userStore.orgTenantId
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getElderlyContract(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = async () => {
  if (!queryFormRef.value) return
  const valid = await queryFormRef.value.validate()
  if (!valid) return
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (row: any = {}, type: number = 1) => {
  formRef.value.open(row, type)
}

// 变更/续签
const renewalRef = ref()
const handleRenewal = (row: any = {}, type: number = 1) => {
  renewalRef.value.open(row, type)
}

// 详情
const detailRef = ref()
const openDetail = (row: any = {}) => {
  detailRef.value.open(row.id)
}

const route = useRoute()
/** 初始化 **/
onMounted(() => {
  // 通知跳转过来的直接打开办理弹窗
  if(route.query && route.query.id){
    openForm({elderId: route.query.id, tenantId: route.query.tenantId}, 2)
  }
  getList()
})
</script>
