<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="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-tabs v-model="activeName">
      <el-tab-pane label="未支付" name="waitPay">
        <el-table v-loading="loading" :data="applicationsPayList" @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="careDays" />
          <el-table-column label="申请日期" align="center" prop="applyDate" />
          <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" class-name="small-padding fixed-width" width="128px">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['care:applications:pay']">确定支付</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-tab-pane>
      <el-tab-pane label="已支付" name="isPay">
        <el-table v-loading="loading" :data="applicationsIsPayList" @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="careDays" />
          <el-table-column label="申请日期" align="center" prop="applyDate" />
          <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" class-name="small-padding fixed-width" width="128px">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleCancel(scope.row)" v-hasPermi="['care:applications:cancelPay']">取消支付</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="getIsPayList"
        />
      </el-tab-pane>
    </el-tabs>


    <!-- 添加或修改护理申请对话框 -->
    <el-dialog :title="title" v-model="open" 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-form>
    </el-dialog>
  </div>
</template>

<script setup name="payApplications">
import { listApplications, updateApplications } from "@/api/care/applications";
import { listAllHospital } from "@/api/care/hospital";
import {listAllPersons} from "@/api/care/persons";
import {listAllCompany} from "@/api/care/nurses";
const { proxy } = getCurrentInstance();
const { sys_user_sex,pay_status } = proxy.useDict('sys_user_sex','pay_status');

const applicationsPayList = ref([]);
const applicationsIsPayList = ref([]);
const hispitalList=ref([]);
const personList=ref([]);
const companyList=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 assignTotal=ref(0);
const activeName = ref("waitPay");

const openAssign=ref(false);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    pageNumAssign: 1,
    pageSizeAssign: 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,
  }
});

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

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

function getIsPayList() {
  loading.value = true;
  queryParams.value.payStatus = 'is_pay';
  listApplications(queryParams.value).then(response => {
    applicationsIsPayList.value = response.rows;
    assignTotal.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  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,
    companyId: null,
    assignRemark: null,
    assignId: null
  };
  proxy.resetForm("applicationsRef");
  proxy.resetForm("applicationAssignRef");
}

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

/** 重置按钮操作 */
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 handleUpdate(row) {
  proxy.$modal.confirm('是否支付"' + row.personName + '"的护理？').then(function() {
    row.payStatus = 'is_pay';
    updateApplications(row).then(response => {
      proxy.$modal.msgSuccess("支付成功");
      getList();
      getIsPayList();
    });
  })
}

/** 指派操作 */
function handleCancel(row) {
  proxy.$modal.confirm('是否取消支付"' + row.personName + '"的护理？').then(function() {
    row.payStatus = 'dis_pay';
    updateApplications(row).then(response => {
      proxy.$modal.msgSuccess("取消支付成功");
      open.value = false;
      getList();
      getIsPayList();
    });
  })
}

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

/** 查询所有特困人员 */
function listPersons() {
  listAllPersons().then(response => {
    personList.value = response;
  });
}

/** 查询公司列表 */
function listCompany() {
  listAllCompany().then(response => {
    companyList.value = response;
  });
}

listPersons();
listHispital();
getList();
getIsPayList();
listCompany();
</script>
