<template>
  <el-dialog :title="props.titleValue" width="1200px" :model-value="props.modelValue" :before-close="closed">
    <div>
      <el-form :model="listQuery" class="mosty-from-wrap" :inline="true">
        <el-form-item label="试卷名称：">
          <el-input v-model="listQuery.name" clearable style="width:227px"></el-input>
        </el-form-item>
        <el-form-item label="业务类型：">
          <el-select v-model="listQuery.type" clearable placeholder="请选择业务类型：">
            <el-option v-for="item in D_BZ_KTKYWLX" :key="item" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="handleFilter">查询</el-button>
          <el-button type="info" @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
      <div class="tabBox" :class="props.Single ? 'table-box-radio' : ''" style="margin-top: 0px;height:90%">
        <el-table stripe ref="multipleUserRef" @selection-change="handleSelectionChange" :data="tableData" border
          :row-key="keyid" style="width: 100%" height="450">
          <el-table-column type="selection" width="55" :reserve-selection="true" v-if="!props.Single" />
          <el-table-column width="55" #default="{ row }" v-else>
            <el-radio v-model="ridioIndex" :label="row.id"></el-radio>
          </el-table-column>
          <el-table-column label="试卷姓名" property="name"> </el-table-column>
          <el-table-column label="业务类型" property="type">
            <template #default="{ row }">
              <dict-tag v-for="item in (row.type && row.type.split(',') || [])" :options="D_BZ_KTKYWLX" :value="item"
                :tag="true" />
            </template>
          </el-table-column>

          <el-table-column label="试题数量" property="count"> </el-table-column>
        </el-table>
      </div>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="listQuery.pageCurrent" :page-sizes="[2, 5, 10, 20]" :page-size="listQuery.pageSize"
          layout="total, prev, pager, next, sizes, jumper" :total="total"></el-pagination>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">

        <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, getCurrentInstance } from "vue";
import { getApi, postApi, putApi } from "@/api/tobAcco_api.js";
const { proxy } = getCurrentInstance();
const { D_BZ_KTKYWLX } = proxy.$dict("D_BZ_KTKYWLX");
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  titleValue: {
    type: String,
    default: "选择试卷"
  },
  roleId: {
    type: String,
    default: ""
  },
  // 选择成员
  data: {
    type: Array,
    default: []
  },
  LeaderType: {
    type: String,
    default: ""
  },
  //是否单选
  Single: {
    type: Boolean,
    default: true
  },

});
const total = ref(0);
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 20
});
const ridioIndex = ref(null);
const tableData = ref([]);
const emits = defineEmits(["update:modelValue", "listdata"]);
const closed = () => {
  emits("update:modelValue", false);
  multipleUserRef.value.clearSelection();
};
const reset = () => {
  listQuery.value = {
    pageCurrent: 1,
    pageSize: 20,
    loginName: "",
  };
  getListData();
};
const keyid = (row) => {
  return row.id;
};

watch(
  () => props.modelValue,
  (val) => {
    if (val === true) {
      handleFilter();
    } else {
    }
  }
);
watch(
  () => props.Single,
  (val) => {
  }
);
onMounted(() => {
})

// 为用户分配角色
const onComfirm = () => {
  const userList = multipleSelectionUser.value;
  let list = {
    userList: userList
  };
  if (props.Single) {
    const info = tableData.value.find((item) => {
      return item.id === ridioIndex.value;
    });
    emits("listdata", [info]);
    closed();
  } else {
    emits("listdata", userList);
    closed();
  }
  multipleUserRef.value.clearSelection();
};

// pageSize改变触发
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

// 页码改变触发
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};
const getListData = async () => {
  tableData.value = []
  const params = listQuery.value;
  if (props.roleId) {
    params.roleId = props.roleId;
  }
  await postApi(params, '/mosty-jcgl/exampaper/getPageList').then((res) => {
    total.value = res.total
    tableData.value = res.records
  })
  multipleUser(props.data);

};

const handleFilter = () => {
  listQuery.value.pageCurrent = 1;
  getListData();
};

const multipleUserRef = ref(null);
const multipleSelectionUser = ref([]);
const handleSelectionChange = (val) => {
  multipleSelectionUser.value = val;
};

// 列表回显
function multipleUser(row) {
  if (row) {
    if (props.Single) {
      ridioIndex.value = row;
    } else {
      row.forEach((item) => {
        tableData.value.forEach((val) => {
          if (item == val.id) {
            multipleUserRef.value.toggleRowSelection(val, true);
          }
        });
      });
    }
  }
}

</script>

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

// .table-box-radio {
//   ::v-deep .el-checkbox__inner {
//     border-radius: 50% !important;
//   }
// }
::v-deep .el-form--inline {
  padding-left: 0 !important;
}

::v-deep .el-radio__label {
  display: none !important;
}

.table-box-radio .el-table1__header-wrapper .el-checkbox {
  display: none !important;
}
</style>
