<template>
  <el-dialog :title="titleValue" width="1200px" append-to-body :model-value="modelValue" :before-close="closed">
    <div>
      <div class="tabBox" style="margin-top: 0px">
        <el-table ref="multipleUserRef" stripe @selection-change="handleSelectionChange" :data="tableData" border
          :row-key="keyid" style="width: 100%" height="450">
          <el-table-column width="55" type="selection" #default="{ row }">
            <el-checkbox v-model="row.isChoose" :label="row.id" @click='chooesry(row)'
              :disabled='row.signState == "W" ? true : false'></el-checkbox>
          </el-table-column>
          <el-table-column prop="name" align="center" label="学员姓名"></el-table-column>
          <el-table-column prop="orgName" align="center" label="所属单位"></el-table-column>
          <el-table-column prop="signState" align="center" label="签到状态">
            <template #default="{ row }">
              <span> {{ row.signState == 'W' ? "未签到" : "签到" }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <!-- <el-button @click="restab">清空</el-button> -->

        <el-button type="primary" @click="onComfirm">确认</el-button>
        <el-button @click="closed">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import * as rule from "@/utils/rules.js";
import * as MOSTY from "@/components/MyComponents/index";
import { ElMessage } from "element-plus";
import { defineProps, watch, ref, onMounted, nextTick } from "vue";
import { selectUserDeptPage } from "@/api/user-manage";
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  titleValue: {
    type: String,
    default: "选择用户"
  },
  roleId: {
    type: String,
    default: ""
  },
  data: {
    type: Array,
    default: []
  },
  tableData: {
    type: Array,
    default: []
  },
  //是否单选
  Single: {
    type: Boolean,
    default: true
  },
});
const total = ref(0);
const listQuery = ref({
  current: 1,
  size: 20
});
const form = ref({});
const tableData = ref([]);
const ridioIndex = ref(null);
const emits = defineEmits(["update:modelValue", "choosedUsers"]);
const multipleUserRef = ref(null);
const multipleSelectionUser = ref([]);
watch(
  () => props.modelValue,
  (val) => {
    if (val === true) {
      tableData.value = props.tableData
    } else {
      tableData.value = []
    }
  }
);
const handleSelectionChange = (val) => {
  multipleSelectionUser.value = val;
};
const closed = () => {
  emits("update:modelValue", false);
};
const reset = () => {
  listQuery.value = {
    current: 1,
    size: 20,
    loginName: "",
    phone: ""
  };
  tableData.value = []
};

// 为用户分配角色
const onComfirm = () => {
  // const userList = JSON.parse(JSON.stringify(tableData.value));
  const userList = tableData.value.filter(item => {
    return item.isChoose
  })
  let list = {
    userList: userList
  };
  if (props.Single) {
    if (userList.length > 1) {
      ElMessage({
        type: "warning",
        message: "请每次选择一人!"
      });
    } else {
      emits("choosedUsers", userList);
      emits("choosedUsersLeader", list);
      closed();
    }
  } else {
    emits("choosedUsers", userList);
    emits("choosedUsersLeader", list);
    closed();
  }
};
// 列表回显
function multipleUser(row) {
  if (row) {
    tableData.value.forEach((item) => {
      row.forEach((val) => {
        if (val.id == item.id) {
          multipleUserRef.value.toggleRowSelection(item, undefined);
        }
      });
    });
  }
}
const keyid = (row) => {
  return row.id;
};

function chooesry(val) {
  // var list=tableData.value.filter(item=>{
  //   return item.isChoose
  // })
}
//清空
function restab() {
  tableData.value = tableData.value.forEach(item => {
    if (item.isChoose) {
      item.isChoose = false
    }

  })
}
</script>

<style lang="scss" scoped>
@import "@/assets/css/layout.scss";
@import "@/assets/css/element-plus.scss";

::v-deep .el-checkbox__label {
  display: none !important;
}
</style>
