<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="就诊人类型" prop="patientType" label-width="100px" label-position="right: 100px">
        <el-select v-model="queryParams.patientType" placeholder="请选择就诊人类型" clearable style="width: 250px">
          <el-option v-for="dict in healthcare_os_patient_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="学号" prop="studentId">
        <el-input v-model="queryParams.studentId" placeholder="请输入学号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['os:healthcareOsPatientInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['os:healthcareOsPatientInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['os:healthcareOsPatientInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['os:healthcareOsPatientInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="healthcareOsPatientInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="就诊人类型" align="center" prop="patientType">
        <template #default="scope">
          <dict-tag :options="healthcare_os_patient_type" :value="scope.row.patientType" />
        </template>
      </el-table-column>
      <el-table-column label="学号/工号" align="center" prop="studentId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.gender" />
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['os:healthcareOsPatientInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['os:healthcareOsPatientInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改患者信息对话框 -->
    <el-dialog :title="title" v-model="open" :width="`${(width = '60%')}`" append-to-body>
      <el-form ref="healthcareOsPatientInfo" :model="form" :rules="rules" label-width="100px">
        <el-form-item prop="patientType" label="就诊人类型" label-width="100px">
          <el-select v-model="form.patientType" placeholder="请选择就诊人类型">
            <el-option v-for="dict in healthcare_os_patient_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学号/工号" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学号/工号" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请输入性别">
            <el-option v-for="dict in sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item v-if="form.patientType == 0" label="专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item v-if="form.patientType == 0" label="班级" prop="classInfo">
          <el-input v-model="form.classInfo" placeholder="请输入班级" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item v-if="form.patientType == 0" label="辅导员" prop="counselor">
          <el-input v-model="form.counselor" placeholder="请输入辅导员" />
        </el-form-item>
        <el-form-item v-if="form.patientType == 1 || form.patientType == 2" label="部门" prop="department">
          <el-input v-model="form.department" placeholder="请输入部门" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="importOperate">导 入</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!--连接 学生 信息 列表  -->
    <StudentInfoList ref="studentInfoListRef"></StudentInfoList>
  </div>
</template>

<script setup name="HealthcareOsPatientInfo">
import { listHealthcareOsPatientInfo, getHealthcareOsPatientInfo, delHealthcareOsPatientInfo, addHealthcareOsPatientInfo, updateHealthcareOsPatientInfo } from '@/api/healthcare/outpatientService/healthcareOsPatientInfo'

const { proxy } = getCurrentInstance()
const { healthcare_os_patient_type } = proxy.useDict('healthcare_os_patient_type')
const { sys_user_sex } = proxy.useDict('sys_user_sex')

// PatientDossier
const healthcareOsPatientInfoList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    patientType: null,
    studentId: null,
    name: null,
    gender: null,
    age: null,
    major: null,
    class: null,
    phone: null,
    counselor: null,
    department: null
  },
  rules: {
    patientType: [{ required: true, message: '就诊人类型不能为空', trigger: 'change' }],
    studentId: [
      { required: true, message: '学号/工号不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          const num = Number(value)
          if (value.length > 255) {
            callback(new Error('长度小于255个字符'))
          } else if (isNaN(num)) {
            callback(new Error('请输入数字'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    name: [
      { required: true, message: '姓名不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' }
    ],
    gender: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
    age: [
      { required: true, message: '年龄不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' },
      {
        pattern: /^(?!0\d)\d+$/,
        message: '请输入一个有效的整数',
        trigger: 'blur'
      },
      { pattern: /^-?\d+$/, message: '请输入一个有效的正数', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          const num = Number(value)
          const minValue = 0
          const maxValue = 150
          if (isNaN(num)) {
            callback(new Error('请输入一个数字'))
          } else if (!Number.isInteger(num)) {
            callback(new Error('请输入一个整数'))
          } else if (num < minValue || num > maxValue) {
            callback(new Error(`请输入一个介于 ${minValue} 和 ${maxValue} 之间的整数`))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    major: [
      { required: true, message: '专业不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value.length > 255) {
            callback(new Error('长度小于255个字符'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    classInfo: [
      { required: true, message: '班级不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value.length > 255) {
            callback(new Error('长度小于255个字符'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    phone: [
      { required: true, message: '电话不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' },
      {
        pattern: /^1[3-9]\d{9}$/,
        message: '请输入一个有效的中国大陆手机号码',
        trigger: 'blur'
      }
    ],
    counselor: [
      { required: true, message: '辅导员不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value.length > 255) {
            callback(new Error('长度小于255个字符'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    department: [
      { required: true, message: '部门不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value.length > 255) {
            callback(new Error('长度小于255个字符'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    createBy: [
      { required: false, message: '创建者不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' }
    ],
    createTime: [
      { required: false, message: '创建时间不能为空', trigger: 'blur' },
      { pattern: /^\S+.*$/, message: '开头有空格', trigger: 'blur' }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询患者信息列表 */
function getList() {
  loading.value = true
  listHealthcareOsPatientInfo(queryParams.value).then(response => {
    healthcareOsPatientInfoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    patientType: null,
    studentId: null,
    name: null,
    gender: null,
    age: null,
    major: null,
    classInfo: null,
    phone: null,
    counselor: null,
    department: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm('healthcareOsPatientInfo')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加患者信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getHealthcareOsPatientInfo(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改患者信息'
  })
}

// 校验
const usernameError = computed(() => {
  if (username.value.length < 3) {
    return '用户名至少需要3个字符'
  } else if (username.value.length > 20) {
    return '用户名不能超过20个字符'
  }
  return ''
})

/** 提交按钮 */
function submitForm() {
  proxy.$refs['healthcareOsPatientInfo'].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateHealthcareOsPatientInfo(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addHealthcareOsPatientInfo(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除患者信息编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delHealthcareOsPatientInfo(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'outpatientService/healthcareOsPatientInfo/export',
    {
      ...queryParams.value
    },
    `healthcareOsPatientInfo_${new Date().getTime()}.xlsx`
  )
}

// 组件  学生 列表
import StudentInfoList from '@/components/Healthcare/OutpatientService/StudentInfoList/index.vue'

//
const studentInfoListRef = ref(null)

// 时间 的 钩子
// console.log("填写", studentInfoListRef.value)

function importOperate() {
  console.log('填写', studentInfoListRef.value)
  studentInfoListRef.value.openDialog()
}

getList()
</script>
