<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="128px">
      <el-form-item label="申请人姓名" prop="personName">
        <el-input
            v-model="queryParams.personName"
            placeholder="请输入申请人姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="首选护理人员性别" prop="nurseGender">
        <el-select v-model="queryParams.nurseGender" placeholder="请选择首选护理人员性别" clearable>
          <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所在医院" prop="hospital">
        <el-select v-model="queryParams.hospital" placeholder="请选择所在医院" clearable>
          <el-option
              v-for="dict in hispitalList"
              :key="dict.name"
              :label="dict.name"
              :value="dict.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input
            v-model="queryParams.address"
            placeholder="请输入科室具体楼层房间床号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预计天数" prop="careDays">
        <el-input
            v-model="queryParams.careDays"
            placeholder="请输入预计天数"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请日期" prop="applyDate">
        <el-input
            v-model="queryParams.applyDate"
            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-table v-loading="loading" :data="applicationsAssignList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="申请人姓名" align="center" prop="personName" />
          <el-table-column label="护理需求详情" align="center" prop="careNeeds" />
          <el-table-column label="首选护理人员性别" align="center" prop="nurseGender">
            <template #default="scope">
              <dict-tag :options="sys_user_sex" :value="scope.row.nurseGender"/>
            </template>
          </el-table-column>
          <el-table-column label="所在医院" align="center" prop="hospital" />
          <el-table-column label="科室具体楼层房间床号" align="center" prop="address" />
          <el-table-column label="预计天数" align="center" prop="careDays" />
          <el-table-column label="申请日期" align="center" prop="applyDate" />
          <el-table-column label="申请状态" align="center" prop="status" >
            <template #default="scope">
              <dict-tag :options="care_apply_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="128px">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleAssign(scope.row)" v-hasPermi="['care:assignments:add']">指派</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="assignTotal>0"
            :total="assignTotal"
            v-model:page="queryParams.pageNumAssign"
            v-model:limit="queryParams.pageSizeAssign"
            @pagination="getAssignList"
        />


    <!-- 指派对话框 -->
    <el-dialog :title="title" v-model="openAssign" width="500px" append-to-body>
      <el-form ref="applicationsRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="申请人" prop="personId">
              <el-select v-model="form.personId" placeholder="请选择特困人员" disabled>
                <el-option
                    v-for="dict in personList"
                    :key="dict.personId"
                    :label="dict.name"
                    :value="dict.personId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首选性别" prop="nurseGender">
              <el-select v-model="form.nurseGender" placeholder="请选择首选护理人员性别" disabled>
                <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-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="护理需求" prop="careNeeds">
              <el-input v-model="form.careNeeds" type="textarea" placeholder="请输入详细护理需求" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所在医院" prop="hospital">
              <el-select v-model="form.hospital" placeholder="所在公司" disabled>
                <el-option
                    v-for="dict in hispitalList"
                    :key="dict.name"
                    :label="dict.name"
                    :value="dict.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入科室具体楼层房间床号" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="预计天数" prop="careDays">
              <el-input v-model="form.careDays" placeholder="请输入预计天数" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请日期" prop="applyDate">
              <el-input v-model="form.applyDate" placeholder="请输入申请日期" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="护理备注" prop="assignRemark">
              <el-input v-model="form.assignRemark" type="textarea" placeholder="请输入内容" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider/>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="选择护理人员" prop="nurseId">
              <el-select v-model="form.nurseId" placeholder="护理人员">
                <el-option
                    v-for="dict in nursesList"
                    :key="dict.name"
                    :label="dict.name"
                    :value="dict.nurseId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm()">确定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="assignNurseApplications">
import {
  listApplications,
  getApplications,
  delApplications,
  addApplications,
  updateApplications,
  assignApplications
} from "@/api/care/applications";
import { listAllHospital } from "@/api/care/hospital";
import {listAllPersons} from "@/api/care/persons";
import {listCompanyNurses} from "@/api/care/nurses";
const { proxy } = getCurrentInstance();
const { sys_user_sex,care_apply_status } = proxy.useDict('sys_user_sex','care_apply_status');

const applicationsAssignList = ref([]);
const hispitalList=ref([]);
const personList=ref([]);
const nursesList=ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const title = ref("");
const assignTotal=ref(0);

const openAssign=ref(false);
const data = reactive({
  form: {},
  queryParams: {
    pageNumAssign: 1,
    pageSizeAssign: 10,
    personId: null,
    personName: null,
    careNeeds: null,
    nurseGender: null,
    hospital: null,
    address: null,
    careDays: null,
    applyDate: null,
    status: 'wait_assign_nurse',
    dealStatus: null,
    payStatus: null,
    auditReason: null,
  }
});

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

/** 查询护理指派列表 */
function getAssignList(){
  loading.value = true;
  listApplications(queryParams.value).then(response => {
    applicationsAssignList.value = response.rows;
    assignTotal.value = response.total;
    loading.value = false;
  });
}
// 取消按钮
function cancel() {
  openAssign.value=false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    applyId: null,
    personId: null,
    personName: null,
    careNeeds: null,
    nurseGender: null,
    hospital: null,
    address: null,
    careDays: null,
    applyDate: null,
    status: null,
    dealStatus: null,
    payStatus: null,
    auditReason: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("applicationsRef");
}

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

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

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


/** 指派操作 */
function handleAssign(row) {
  reset();
  const _applyId = row.applyId || ids.value
  getApplications(_applyId).then(response => {
    form.value = response.data;
    openAssign.value = true;
    title.value = "指派";
  });
}

/** 提交按钮 */
function submitForm() {
  console.info(form.value);
  proxy.$refs["applicationsRef"].validate(valid => {
    if (valid) {
      form.value.status='assigned'
      form.value.dealStatus='no_start'
      assignApplications(form.value).then(response => {
        proxy.$modal.msgSuccess("指派成功");
        openAssign.value = false;
        getAssignList();
      });
    }
  });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('care/applications/export', {
    ...queryParams.value
  }, `applications_${new Date().getTime()}.xlsx`)
}

function handleSelectChange(value) {
  // 找到选中的选项
  const person =personList.value.find(option => option.personId === value);
  if (person) {
    form.value.personId=person.personId
    form.value.personName=person.name
  }
}

/** 查询当前公司的护理人员 */
function companyNurses() {
  listCompanyNurses().then(response => {
    nursesList.value = response;
  });
}

/** 查询医院列表 */
function listHispital() {
  listAllHospital().then(response => {
    hispitalList.value = response;
  });
}
getAssignList();
companyNurses();
listHispital();
</script>
