<template>
  <div class="app-container">
    <el-col :span="1.5">
      <div style="font-size: 16px;margin-left: 12px;line-height: 32px;font-weight: 700;color: #606266;">公告标题：{{ noticeTitle }}</div>
    </el-col>
    <br />
<!--    <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" @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="handleAddUser"
        >添加用户
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
        >移除用户
        </el-button>
      </el-col>

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange" :row-key="getRowKey">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名字" align="center" prop="nickName"/>
<!--      <el-table-column label="类型" align="center" prop="userType"/>-->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <div v-if="scope.row.status == '0'">
            <span class="el-tag el-tag--danger el-tag--light" index="1" data-v-00e5bcbc=""><span class="el-tag__content">未读</span><!--v-if--></span>
          </div>
          <div v-else>
            <span class="el-tag el-tag--default el-tag--light" index="1" data-v-00e5bcbc=""><span class="el-tag__content">已读</span><!--v-if--></span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <div v-if="scope.row.status == '0'">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">移除</el-button>
          </div>
          <div v-else>
            ——
          </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="选择用户" v-model="userOpen" append-to-body  :style="{ width:'1200px','overflow-y': 'auto' }"  width="1200px">
      <UserList @sendUsersToNotice="getUsers"></UserList>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="addUser">添加用户</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="noticeUser">
import {listNoticeUser, addNoticeUser, removeNoticeUser, delNotice} from "@/api/system/notice";
import UserList from "@/views/system/notice/choose_user.vue";
import {delProtocol} from "@/api/care/protocol";
const router = useRoute();
const {proxy} = getCurrentInstance();
const noticeId=ref("");
const noticeTitle = ref("");
const routerType = ref("");
const userList = ref([]);
const loading = ref(true);
const userOpen = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const addUsers = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    userType: null
  },
});

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

/** 保存选中的数据编号 */
function getRowKey(row) {
  return row.studentId;
}

function getUsers(value) {
  addUsers.value = value;
}

/** 查询已选择的用户列表 */
function getList() {
  loading.value = true;
  listNoticeUser(noticeId.value, queryParams.value).then(response => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });

}

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

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

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

/** 添加学生按钮操作 */
function handleAddUser() {
  userOpen.value = true;
  title.value = "选择学生";
}

function addUser() {
  if (addUsers.value.length > 0) {
    console.info(JSON.stringify(noticeId.value)+"-----"+JSON.stringify(addUsers.value));
    addNoticeUser(noticeId.value, addUsers.value).then(res => {
      if (res.code === 200) {
        proxy.$modal.confirm("添加成功,人数总共：" + res.data + "人，是否继续添加？", "提示", {
          confirmButtonText: "继续添加",
          cancelButtonText: "关闭窗口",
          showClose: false,
          type: "success",
        }).catch(() => {
          getList();
          userOpen.value = false;
        });
      }
    })
  } else {
    proxy.$modal.msgWarning("请先选择用户后再添加");
  }

}

/** 删除按钮操作 */
function handleDelete(row) {
  const noticeUserIds = row.noticeUserId ? [row.noticeUserId] : ids.value;
  proxy.$modal.confirm('是否确认移除该用户?"' + noticeUserIds + '"').then(function() {
    return removeNoticeUser(noticeUserIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("移除成功");
  }).catch(() => {});
}


(() => {
  noticeId.value = router.params.id;
  noticeTitle.value = router.query && router.query.noticeTitle;

  if (noticeId.value) {
    loading.value = true;
    getList();
    loading.value = false;
  }
})();
</script>
