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

    <el-table v-loading="loading" :data="applicationsList" @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">
          <div v-if="scope.row.status == 'disagree'">
            <el-popover effect="light" trigger="hover" placement="top" width="auto">
              <template #default>
                <div>审核原因: {{ scope.row.auditReason }}</div>
              </template>
              <template #reference>
                <dict-tag :options="care_apply_status" :value="scope.row.status"/>
              </template>
            </el-popover>
          </div>
          <div v-if="scope.row.status !== 'disagree'">
            <dict-tag :options="care_apply_status" :value="scope.row.status"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="dealStatus" >
        <template #default="scope">
          <dict-tag :options="deal_status" :value="scope.row.dealStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template #default="scope">
          <dict-tag :options="pay_status" :value="scope.row.payStatus"/>
        </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="158px">
        <template #default="scope">
          <div v-if="scope.row.status == 'no_check' || scope.row.status == 'disagree'">
            <el-button link type="primary" icon="Check" @click="handleCheck(scope.row)" v-hasPermi="['care:applications:check']">送审</el-button>
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['care:applications:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['care:applications:remove']">删除</el-button>
          </div>
          <div v-else>
            <el-button link type="success" icon="View" @click="handleView(scope.row)" v-hasPermi="['care:applications:edit']">查看</el-button>
          </div>
        </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="500px" append-to-body>
      <el-form ref="applicationsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请人" prop="personId">
          <el-select v-model="form.personId"
                     filterable
                     remote
                     reserve-keyword
                     placeholder="请输入特困人员名字进行过滤"
                     :remote-method="personSelectorData"
                     :loading="personLoading"
                     style="width: 200px"  @change="handleSelectChange"
          >
            <el-option
                v-for="item in personList"
                :key="item.personId"
                :label="item.name"
                :value="item.personId"
            />
          </el-select>
          <el-input v-model="form.personName" placeholder="personName" type="hidden"></el-input>
        </el-form-item>
        <el-form-item label="护理需求" prop="careNeeds">
          <el-input v-model="form.careNeeds" type="textarea" placeholder="请输入详细护理需求" />
        </el-form-item>
        <el-form-item label="首选性别" prop="nurseGender">
          <el-select v-model="form.nurseGender" 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="hospital">
          <el-select v-model="form.hospital" placeholder="所在医院">
            <el-option
                v-for="dict in hispitalList"
                :key="dict.name"
                :label="dict.name"
                :value="dict.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入科室具体楼层房间床号" />
        </el-form-item>
        <el-form-item label="预计天数" prop="careDays">
          <el-input v-model="form.careDays" placeholder="请输入预计天数" />
        </el-form-item>
        <el-form-item label="申请日期" prop="applyDate">
          <el-date-picker clearable
                          v-model="form.applyDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择申请日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </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>

    <el-dialog :title="title" v-model="openDetail" width="500px" append-to-body>
      <el-form ref="applicationsRef" :model="form" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="申请人" prop="personId">
              <el-select v-model="form.personId"
                         filterable
                         remote
                         reserve-keyword
                         placeholder="请输入特困人员名字进行过滤"
                         :remote-method="personSelectorData"
                         :loading="personLoading"
                         style="width: 200px"
              >
                <el-option
                    v-for="item in personList"
                    :key="item.personId"
                    :label="item.name"
                    :value="item.personId"
                />
              </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-input v-model="form.hospital" type="textarea" placeholder="请输入详细护理需求" disabled />
            </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-form>
    </el-dialog>
  </div>
</template>

<script setup name="Applications">
import { listApplications, getApplications, delApplications, addApplications, updateApplications } from "@/api/care/applications";
import { listAllHospital } from "@/api/care/hospital";
import {listAllPersons} from "@/api/care/persons";

const { proxy } = getCurrentInstance();
const { sys_user_sex,care_apply_status,deal_status,pay_status } = proxy.useDict('sys_user_sex','care_apply_status','deal_status','pay_status');

const applicationsList = ref([]);
const hispitalList=ref([]);
const personList=ref([]);
const personLoading=ref(true);
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 openDetail=ref(false);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    personId: null,
    personName: null,
    careNeeds: null,
    nurseGender: null,
    hospital: null,
    address: null,
    careDays: null,
    applyDate: null,
    status: null,
    dealStatus: null,
    payStatus: null,
    auditReason: null,
  },
  rules: {
    personId: [
      { required: true, message: "申请人id不能为空", trigger: "blur" }
    ],
    personName: [
      { required: true, message: "申请人姓名不能为空", trigger: "blur" }
    ],
    applyDate: [
      { required: true, message: "申请日期不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "申请状态(待审核、同意、不同意、待指派、已指派)不能为空", trigger: "change" }
    ],
  }
});

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

/** 查询护理申请列表 */
function getList() {
  loading.value = true;
  listApplications(queryParams.value).then(response => {
    applicationsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.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;
  getList();
}

/** 重置按钮操作 */
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 handleAdd() {
  reset();
  open.value = true;
  title.value = "添加护理申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _applyId = row.applyId || ids.value
  getApplications(_applyId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改护理申请";
  });
}

//查看按钮
function handleView(row) {
  reset();
  const _applyId = row.applyId || ids.value
  getApplications(_applyId).then(response => {
    form.value = response.data;
    openDetail.value = true;
    title.value = "查看护理申请";
  });
}

// 送审
function handleCheck(row) {
  proxy.$modal.confirm('是否确认送审"' + row.personName + '"的护理申请？').then(function() {
    row.status='wait_check';
    updateApplications(row).then(response => {
      proxy.$modal.msgSuccess("送审成功");
      open.value = false;
      getList();
    });
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["applicationsRef"].validate(valid => {
    if (valid) {
      if (form.value.applyId != null) {
        updateApplications(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addApplications(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _applyIds = row.applyId || ids.value;
  proxy.$modal.confirm('是否确认删除护理申请编号为"' + _applyIds + '"的数据项？').then(function() {
    return delApplications(_applyIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
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 listHispital() {
  listAllHospital().then(response => {
    hispitalList.value = response;
  });
}
/** 查询所有特困人员 */
function personSelectorData(qstr){
  personLoading.value = true;
  listAllPersons({
    name: qstr
  }).then(response=>{
    personLoading.value = false;
    personList.value = response;
  });
}
listHispital();
getList();
</script>
