<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入护理人员的姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入护理人员的联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在公司" prop="companyId">
        <el-select v-model="queryParams.companyId" placeholder="所在公司" clearable>
          <el-option
              v-for="dict in companyList"
              :key="dict.companyId"
              :label="dict.name"
              :value="dict.companyId"
          ></el-option>
        </el-select>
      </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:nurses:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['care:nurses:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['care:nurses:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['care:nurses:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nursesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="登录名" align="center" prop="loginName" />
      <el-table-column label="性别" align="center" prop="age">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.age"/>
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="dateOfBirth" />
      <el-table-column label="所在地址" align="center" prop="address" />
      <el-table-column label="联系电话" align="center" prop="phoneNumber" />
      <el-table-column label="工作经验年限" align="center" prop="experienceYears" />
      <el-table-column label="所在公司" align="center" prop="companyName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="255">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['care:companies:edit']">修改</el-button>
          <el-button link type="warning" icon="Edit" @click="handleResetPwd(scope.row)" v-hasPermi="['care:companies:resetPassword']">重置密码</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['care:companies: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="700px" append-to-body>
      <el-form ref="nursesRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入护理人员的名字" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.age" 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-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="出生日期" prop="dateOfBirth">
              <el-date-picker clearable
                              v-model="form.dateOfBirth"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请输入出生日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phoneNumber">
              <el-input v-model="form.phoneNumber" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="登录名" prop="loginName">
              <el-input v-model="form.loginName" placeholder="请输入登录名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录密码" prop="password">
              <el-input v-model="form.password" type="password" placeholder="请输入登录密码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="所在地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所在公司" prop="companyId">
              <el-select v-model="form.companyId" placeholder="所在公司">
                <el-option
                    v-for="dict in companyList"
                    :key="dict.companyId"
                    :label="dict.name"
                    :value="dict.companyId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作年限" prop="experienceYears">
              <el-input v-model="form.experienceYears" placeholder="请输入工作经验年限" />
            </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="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="护理人员的执业许可证号码" prop="licenseNumber">-->
<!--          <el-input v-model="form.licenseNumber" 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>
  </div>
</template>

<script setup name="Nurses">
import { listNurses, getNurses, delNurses, addNurses, updateNurses,listAllCompany,resetPassword } from "@/api/care/nurses";

const { proxy } = getCurrentInstance();
const { sys_user_sex } = proxy.useDict('sys_user_sex');
const nursesList = 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 companyList=ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    dateOfBirth: null,
    address: null,
    phoneNumber: null,
    licenseNumber: null,
    experienceYears: null,
    companyId: null,
    userId: null
  },
  rules: {
    name: [
      { required: true, message: "护理人员的名字不能为空", trigger: "blur" }
    ],
    dateOfBirth: [
      { required: true, message: "护理人员的出生日期不能为空", trigger: "blur" }
    ],
    phoneNumber: [
      { required: true,pattern: /^1[3-9]\d{9}$/, message: '护理人员的联系电话不能为空', trigger: 'blur' }
    ],
    licenseNumber: [
      { required: true, message: "护理人员的执业许可证号码不能为空", trigger: "blur" }
    ],
    experienceYears: [
      { required: true, message: "护理人员的工作经验年限不能为空", trigger: "blur" }
    ],
    companyId: [
      { required: true, message: "所在公司不能为空", trigger: "blur" }
    ],
    loginName: [
      { required: true, message: "用户名不能为空", trigger: "blur" }
    ],
    password: [
      { required: true, message: "密码不能为空", trigger: "change" }
    ]

  }
});

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

/** 查询护理人员列表 */
function getList() {
  loading.value = true;
  listNurses(queryParams.value).then(response => {
    nursesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    nurseId: null,
    name: null,
    dateOfBirth: null,
    address: null,
    phoneNumber: null,
    licenseNumber: null,
    experienceYears: null,
    companyId: null,
    age:null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    userId: null,
    loginName:null,
    password:null
  };
  proxy.resetForm("nursesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 充值密码操作 */
function handleResetPwd(row) {
  const nurseId = row.nurseId || ids.value;
  proxy.$modal.confirm('是否确认重置该护理人员编号为"' + nurseId + '"的密码？').then(function() {
    return resetPassword(row.userId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("重置成功");
  }).catch(() => {});
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加护理人员";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _nurseId = row.nurseId || ids.value
  getNurses(_nurseId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改护理人员";
  });
}

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

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

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

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