<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="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="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">
      <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="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="dealStatus" >
        <template #default="scope">
          <dict-tag :options="deal_status" :value="scope.row.dealStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="168px">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="look(scope.row)" v-hasPermi="['care:applications:look']">查看</el-button>
          <el-button link type="primary" icon="Edit" @click="clock(scope.row)" v-hasPermi="['care:applications:clock']">打卡</el-button>
          <el-button link type="primary" icon="Edit" @click="finish(scope.row)" v-hasPermi="['care:applications:finish']">完成</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"
    />
  </div>

  <el-dialog :title="title" v-model="open" 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" 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-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>

  <el-dialog v-model="openClock" width="80%">
    <component :is="currentComponent" v-bind="componentProps"></component>
  </el-dialog>
</template>

<script setup name="ClocksTask">
import {
  getApplications,
  listApplications,
    updateApplications
} from "@/api/care/applications";
import clockIndex from './index.vue';

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

const applicationsList = ref([]);
const hispitalList=ref([]);
const currentComponent = ref(null); // 使用 ref 来管理组件
const componentProps = ref({});     // 使用 ref 来管理传递给组件的属性
const personList=ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const title = ref("");
const total=ref(0);
const open=ref(false);
const openClock=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: 'assigned',
    dealStatus: null,
    payStatus: null,
    auditReason: null,
  }
});

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

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

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  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 handleExport() {
  proxy.download('care/applications/export', {
    ...queryParams.value
  }, `applications_${new Date().getTime()}.xlsx`)
}


function look(row) {
  const _applyId = row.applyId || ids.value
  getApplications(_applyId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "查看";
  });
}

function clock(row){
  const _applyId = row.applyId || ids.value
  // 设置要加载的组件
  currentComponent.value = clockIndex;

  // 设置传递给组件的属性
  componentProps.value = {
    applyId: _applyId,
    // 其他需要传递的属性可以在这里添加
  };
  openClock.value=true
}

function finish(row){
  proxy.$modal.confirm('是否确认该客户："' + row.personName + '"已经完成？').then(function() {
    row.dealStatus="complete"
    row.payStatus="dis_pay"
    return updateApplications(row);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }).catch(() => {});
}
getList();

</script>
