<template>
  <el-dialog
    :title="titleValue"
    width="1400px"
    :model-value="modelValue"
    append-to-body
    @close="closed"
  >
    <div>
      <el-form :model="listQuery" class="mosty-from-wrap" :inline="true">
        <el-form-item label="报告名称">
          <el-input
            placeholder="请输入报告名称"
            v-model="listQuery.bgmc"
            clearable
          ></el-input>
        </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 ? 'tabBoxRadio' : ''"
        :key="keyVal"
        style="margin-top: 0px"
      >
        <el-table
          ref="multipleUserRef"
          @selection-change="handleSelectionChange"
          :data="tableData"
          v-loading="loading"
          border
          :row-key="keyid"
          style="width: 100%"
          height="450"
        >
          <!-- <el-table-column
            type="selection"
            width="55"
            :reserve-selection="true"
          /> -->
          <el-table-column prop="bgmc" align="center" label="报告名称" />
          <el-table-column prop="scsj" align="center" label="保存时间" />
          <el-table-column align="center" label="操作">
            <template #default="{ row }">
              <el-button type="primary" size="mini" @click="deletBg(row)">删除</el-button>
              <el-button type="primary" size="mini" @click="onComfirm(row.bgnr)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @pageSize-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.pageCurrent"
          :page-sizes="[10, 20, 50, 100]"
          :page-pageSize="listQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closed">取消</el-button>
        <!-- <el-button type="primary" @click="onComfirm">确认</el-button> -->
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { qcckGet ,qcckDelete} from "@/api/qcckApi.js";
import { defineProps, ref, getCurrentInstance, watch } from "vue";
const { proxy } = getCurrentInstance();
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  titleValue: {
    type: String,
    default: "查看报告"
  },
  LeaderType: {
    type: String,
    default: ""
  },
  //是否单选
  Single: {
    type: Boolean,
    default: true
  },
  roleIds: {
    type: Array,
    default: []
  }, chageData: {
    type: String,
    default: ""
  }
});
const loading = ref(false);
const total = ref(0);
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 20
});

const keyVal = ref();
const multipleUserRef = ref(null);
const multipleSelectionUser = ref([]);
const tableData = ref([]);
const emits = defineEmits(["update:modelValue", "choosed"]);
const keyid = (row) => {
  return row.id;
};

const closed = () => {
  emits("update:modelValue", false);
};
const reset = () => {
  listQuery.value = { pageCurrent: 1, pageSize: 20 };
  getListData();
};

// 为用户分配角色
const onComfirm = (val) => {
  // const userList = multipleSelectionUser.value;
  // let list = [];
  // let listId = [];
  // userList.forEach((val) => {
  //   if (listId.indexOf(val.id) == -1) {
  //     list.push(val);
  //     listId.push(val.id);
  //   }
  // });
  emits("choosed", val);
  closed();
};
/**
 * pageSize 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};
/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};
const chageDatas=ref()
// watch(() => props.chageData, (val) => {
//   chageDatas.value = val;
//   getListData()
// },{deep:true,immediate:true})
const getListData = () => {
  keyVal.value++;
  loading.value = true;
  const params = { ...listQuery.value,ypid:chageDatas.value};
  qcckGet(params, "/mosty-gsxt/gsxtYpbg/getPageList")
    .then((res) => {
      loading.value = false;
      tableData.value = res.records || [];
      total.value = res.total;
      multipleUser();
    })
    .catch(() => {
      loading.value = false;
    });
};

//列表回显
function multipleUser() {
  tableData.value.forEach((item) => {
    multipleUserRef.value.toggleRowSelection(item, false);
    if (props.roleIds.some((id) => id == item.id)) {
      multipleUserRef.value.toggleRowSelection(item, true);
    }
  });
}

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

const handleSelectionChange = (val) => {
  if (props.Single) {
    if (val.length > 1) {
      let del_row = val.shift();
      multipleUserRef.value.toggleRowSelection(del_row, false);
    }
    multipleSelectionUser.value = val;
  } else {
    multipleSelectionUser.value = val;
  }
};
// 删除
const deletBg = (val) => {
  const id = { ids: [val.id] }
  proxy.$confirm('确定删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    qcckDelete(id, `/mosty-gsxt/gsxtYpbg/deleteEntity`).then((res) => {
      getListData();
      ElMessage.success("删除成功");
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    });
  });
}

watch(
  () => props.modelValue,
  (val) => {

    if (val) {


      chageDatas.value = props.chageData;
      console.log(chageDatas.value );
      handleFilter();
    }
  },
  { immediate: true }
);
</script>

<style lang="scss" scoped>
@import "@/assets/css/layout.scss";
@import "@/assets/css/element-plus.scss";
</style>
<style>
.tabBoxRadio .el-checkbox__inner {
  border-radius: 50% !important;
}
.tabBoxRadio .el-table__header-wrapper .el-checkbox {
  display: none;
}
</style>
