<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <span class="cameratxt">角色管理</span>
    </div>
    <div class="middle">
      <div class="filter">
        <div class="condition">
          <span>角色名称 </span>
          <el-input
            clearable
            v-model="searchInput.name"
            @clear="searchBtn"
            class="w-50 m-2"
            placeholder="请输入角色名称"
            style="width: 250px"
          />
        </div>
        <div class="condition">
          <span>创建时间 </span>
          <el-date-picker
            v-model="searchInput.createTime"
            type="daterange"
            unlink-panels
            range-separator="-"
            start-placeholder="起始时间"
            end-placeholder="截止时间"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            :prefix-icon="Calendar"
            placeholder="请选择日期"
            style="width: 280px"
            @change="searchBtn"
          />
        </div>
        <el-button
          style="margin-left: 20px"
          type="primary"
          class="search"
          @click="searchBtn"
          >查询</el-button
        >
        <el-button
          style="margin-left: 20px"
          class="reset"
          @click="resetSearchBtn"
          >重置</el-button
        >
      </div>
      <!-- 按钮列表 -->
      <div class="gongneng">
        <el-button @click="addProjectbtn" class="add"
          ><el-icon> <CirclePlus /> </el-icon><span>创建角色</span></el-button
        >
        <el-button @click="projectImportBtn" class="imp"
          ><el-icon><Upload /></el-icon><span>批量导入</span></el-button
        >
        <!-- <el-button @click="projectExportBtn" class="add"
          ><el-icon><Download /></el-icon><span>项目导出</span></el-button
        > -->
      </div>
    </div>
    <div class="scroll">
      <div class="footer" v-loading="loading">
        <el-table
          :row-class-name="tableRowClassName"
          :data="tableData.list"
          @selection-change="handleSelectionChange"
          style="width: 100%"
          :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
            height: '50px',
            border: 0,
          }"
        >
          <!-- <el-table-column type="selection" width="55" /> -->
          <el-table-column align="center" prop="createTime" label="序号" />
          <el-table-column align="center" prop="createTime" label="角色级别" />
          <el-table-column align="center" prop="createUser" label="父级角色" />
          <el-table-column align="center" prop="createUser" label="角色名称" />
          <el-table-column align="center" prop="createTime" label="创建时间" />
          <el-table-column align="center" label="操作" width="220">
            <template #default="scope">
              <div class="options">
                <div class="reset" @click="reset(scope.row)">添加</div>
                <div class="look" @click="edit(scope.row)">编辑</div>
                <el-popconfirm
                  width="220"
                  confirm-button-text="确认"
                  cancel-button-text="取消"
                  :icon="InfoFilled"
                  icon-color="#f89626"
                  title="是否删除此数据?"
                  @confirm="del(scope.row)"
                  @cancel="cancelEvent"
                >
                  <template #reference>
                    <div class="del">删除</div>
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 添加角色 -->
        <el-dialog
          class="addRoles"
          v-model="addRolesVisible"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :title="addRolesTitle"
          align-center
          width="900"
          :before-close="cancelAddRoles"
        >
          <div class="rolesNames">
            <span>角色名称: &nbsp;&nbsp;</span>
            <el-input
              :clearable="true"
              v-model.trim="addRoleName"
              class="w-50 m-2"
              style="width: 450px"
              placeholder="请输入角色名称"
            />
          </div>
          <div class="rolesName">
            <div class="rolePermission">角色权限: &nbsp;&nbsp;</div>
            <div>
              <!-- <el-table
              :row-class-name="tableRowClassName"
              :data="addRolesData.list"
              style="width: 100%"
              :span-method="arraySpanMethod"
              :header-cell-style="{
                background: 'rgba(240, 243, 247, 1)',
                height: '50px',
              }"
            >
              <el-table-column align="center" prop="one" label="一级" />
              <el-table-column align="center" prop="two" label="二级" />
              <el-table-column width="600" label="功能">
                <template #default="{ row }">
                  <div class="checkBoxSel">
                    <el-tree
                      :ref="`addRolesData${row.four}`"
                      style="max-width: 600px"
                      :data="row.three"
                      show-checkbox
                      node-key="id"
                      :props="defaultProps"
                      default-expand-all
                      @check="
                        (data, nodes) => editMemberCheckChange(data, nodes, row)
                      "
                    />
                  </div>
                </template></el-table-column
              >
            </el-table> -->

              <table cellspacing="0" border-collapse="collapse">
                <tr align="center">
                  <td>一级</td>
                  <td>二级</td>
                  <td>功能</td>
                </tr>
                <!-- 房态管理 1 -->
                <tr align="center">
                  <td>房态管理</td>
                  <td>/</td>
                  <td>
                    <el-tree
                      ref="addRolesData1"
                      style="max-width: 600px"
                      :data="addRolesList1.three"
                      show-checkbox
                      node-key="id"
                      :props="defaultProps"
                      default-expand-all
                      @check="
                        (data, nodes) =>
                          editMemberCheckChange(data, nodes, addRolesList1.one)
                      "
                    />
                  </td>
                </tr>
                <!-- 楼栋单元管理 2 -->
                <tr align="center">
                  <td :rowspan="4">房间管理</td>
                  <td>楼栋单元管理</td>
                  <td>
                    <el-tree
                      ref="addRolesData2"
                      style="max-width: 600px"
                      :data="addRolesList2.three"
                      show-checkbox
                      node-key="id"
                      :props="defaultProps"
                      default-expand-all
                      @check="
                        (data, nodes) =>
                          editMemberCheckChange(data, nodes, addRolesList2.two)
                      "
                    />
                  </td>
                </tr>
              </table>
            </div>
          </div>
          <div class="options">
            <div class="btn">
              <el-button
                color="rgba(41, 109, 227, 1)"
                class="queding"
                type="primary"
                @click="confirmEdit"
              >
                确定
              </el-button>
              <el-button @click="cancelAddRoles">取消</el-button>
            </div>
          </div>
        </el-dialog>

        <!-- 项目导入 -->
        <el-dialog
          class="projectImport"
          v-model="projectImportVisible"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          title="项目导入"
          align-center
          width="600"
          :before-close="cancelProjectImport"
        >
          <p class="title">当前只支持项目类型为“非装配式建筑项目”的项目导入</p>
          <p class="down">
            <span>非装配式建筑项目导入模板</span
            ><span @click="templateDown">模板下载</span>
          </p>
          <el-upload
            class="avatar-uploader"
            action="#"
            :auto-upload="false"
            :on-remove="projectImportRemove"
            :on-change="projectImportChange"
            :before-upload="beforeAvatarProImport"
            ref="projectRef"
            :limit="1"
            :on-exceed="projectImportExceed"
          >
            <template #trigger>
              <el-button class="queding" type="primary">
                &nbsp;&nbsp;导入项目&nbsp;&nbsp;
              </el-button>
            </template>
          </el-upload>
          <div class="options">
            <el-button
              color="rgba(9, 101, 98, 1)"
              class="queding"
              type="primary"
              @click="projectImportConfirm(projectRef)"
            >
              &nbsp;&nbsp;确认导入&nbsp;&nbsp;
            </el-button>
            <el-button @click="cancelProjectImport"
              >&nbsp;&nbsp;取消导入&nbsp;&nbsp;</el-button
            >
          </div>
        </el-dialog>
      </div>

      <!-- 分页组件 -->
      <el-pagination
        background
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total,sizes, prev, pager, next, jumper, slot"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  nextTick,
  onMounted,
  watch,
  onBeforeMount,
  onUnmounted,
} from "vue";
import COS from "cos-js-sdk-v5";
import { useRouter } from "vue-router";
import { genFileId, ElMessage, ElMessageBox } from "element-plus";
import { Calendar } from "@element-plus/icons-vue";
import { dayjs } from "element-plus";
import lodash, { reduce } from "lodash";
import axios from "axios";
import { useStore } from "vuex";

const store = useStore();
const router = useRouter();

const tableData = reactive({
  list: [{}],
});

const searchInput = reactive({
  name: "", // 项目名称
  createTime: "", // 创建时间
}); // 搜索按钮数据

const currentPage = ref(1); // 当前页
const pageSize = ref(10);
const total = ref(40); // 当前总数
const selectData = reactive({
  list: [],
}); // 多选框选择的数据
const api = ref("");

// 新增账号 (--------------------------------------------------------)
const addRolesVisible = ref(false);
const addRolesTitle = ref();
const addRolesData = ref();
const defaultProps = ref({
  children: "children",
  label: "name",
});

const addRoleName=ref()
const addRolesData1 = ref(); // 房态管理
const addRolesList1 = reactive({
  one: "房态管理",
  two: "/",
  three: [
    {
      name: "全部",
      id: 0,
      children: [
        { name: "预定", id: 1 },
        { name: "退房", id: 2 },
        { name: "维修", id: 3 },
        { name: "关房", id: 4 },
        { name: "置脏", id: 5 },
        { name: "置净", id: 6 },
        { name: "退款", id: 7 },
        { name: "入住", id: 8 },
        { name: "锁房", id: 9 },
        // { name: "日志", id: 10 },
        { name: "换房", id: 11 },
        { name: "开房", id: 12 },
        // { name: "门锁管理", id: 13 },
        { name: "查看界面", id: 14 },
        { name: "开电", id: 15 },
        { name: "关电", id: 16 },
        { name: "取消预定", id: 17 },
        { name: "取消维修", id: 18 },
        { name: "查看密码", id: 19 },
        { name: "退款", id: 20 },
      ],
    },
  ],
  four: "1",
  checkedSel: [],
});

const addRolesData2 = ref(); // 楼栋单元管理
const addRolesList2 = reactive({
  one: "房间管理",
  two: "楼栋单元管理",
  three: [
    {
      name: "全部",
      id: 0,
      children: [
        { name: "添加", id: 1 },
        { name: "编辑", id: 2 },
        { name: "删除", id: 3 },
        { name: "查看界面", id: 4 },
      ],
    },
  ],
  four: "2",
  checkedSel: [],
});

// 项目导入
const projectImportVisible = ref(false);
const projectRef = ref();
const projectFile = ref();

// 获取账户列表
const getList = async () => {
  loading.value = true;
  let data = {
    currentPage: currentPage.value, // 当前页
    pageCount: pageSize.value, // 一页数据条数
    projectName: searchInput.name,
    pageType: 1,
    permitNum: searchInput.LicenseCode, // 施工许可证编码
    projectUnit: searchInput.unit, // 建设单位
    projectPurpose: searchInput.purpose, // 项目用途
    structureType: searchInput.structureType, // 结构类型
    projectType: searchInput.projectType, // 项目类型
  };

  if (searchInput.createTime) {
    data.startDate = searchInput.createTime[0];
    data.endDate = searchInput.createTime[1];
  }
  let res = await axios({
    method: "get",
    url: api.value + "/jgcloudProject/queryPageJgcloudProject",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: data,
  });
  console.log(res, "项目分页数据");
  if (res.data.code == 200) {
    tableData.list = res.data.data.list;
    total.value = res.data.data.totalCount;
    loading.value = false;
  } else {
    loading.value = false;
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};

// 搜索功能
const searchBtn = lodash.debounce(async () => {
  getList();
}, 300);

// 角色新增
const addProjectbtn = () => {
  addRolesVisible.value = true;
  addRolesTitle.value = "角色新增";
};
// 取消角色新增
const cancelAddRoles = () => {
  addRolesVisible.value = false;
  accountRef.value.resetFields();
};

//编辑项目
const edit = (row) => {
  addRolesVisible.value = true;
  addRolesTitle.value = "角色编辑";
  console.log(row);
};
// 删除pdf文件 （装配式相关附件）
const pdfDel = (value, uid) => {};

// 导入项目
const projectImportConfirm = async () => {
  let data = new FormData();
  data.set("file", projectFile.value);
  data.set("pageType", 1);
  let res = await axios({
    method: "POST",
    url: api.value + "/jgcloudProject/importExcelProject",
    headers: {
      token: sessionStorage.getItem("token"),
      user_head: sessionStorage.getItem("userhead"),
    },
    data: data,
  });
  console.log(res, "导入项目");
  if (res.data.code == 200) {
    projectImportVisible.value = false;
    getList();
    if (projectRef.value) {
      projectRef.value.clearFiles();
    }
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
const cancelProjectImport = () => {
  projectRef.value.clearFiles();
  projectImportVisible.value = false;
};

// (--------------------------------------------------)
// cos桶中地址栏中中文地址乱码转换为中文
const pdfName = (value) => {
  // console.log(decodeURIComponent(value));
  // console.log(decodeURIComponent(value).length);
  let index = decodeURIComponent(value).lastIndexOf("/") + 1;
  let name = decodeURIComponent(value).slice(index, value.length);
  console.log(name);
  return name;
};

// 多选框功能
const handleSelectionChange = (val) => {
  console.log(val);
  selectData.list = val;
};

// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};
// 每页显示条数
const handleSizeChange = (value) => {
  console.log(value, "每页显示条数");
  pageSize.value = value;
  getList();
};
// 分页
const handleCurrentChange = (value) => {
  console.log(value, "分页");
  currentPage.value = value;
  getList();
};

onBeforeMount(() => {
  api.value = store.state.user.api;
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.content-box {
  min-width: 1000px;
  width: calc(100vw - 260px);
  height: calc(100vh - 105px);
  margin: 20px auto;
  background-color: #fff;
  color: #fff;
  display: flex;
  flex-direction: column;
  box-shadow: 0px 3px 10px rgba(0, 97, 255, 0.2);

  .left {
    // width: calc(100wh - 40px);
    display: flex;
    align-items: center;
    height: 60px;
    margin: 0 30px;
    border-bottom: 1px solid #ccc;
    color: #000;
    font-size: 18px;
    font-weight: 600;

    .cameratxt {
      display: block;
      height: 60px;
      line-height: 60px;
    }
  }
  .middle {
    width: calc(100% - 60px);
    margin: 0 auto;
    color: #000;

    // border-bottom: 1px solid rgb(231, 231, 231);
    .filter {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      margin: 10px 0 0 0;

      .search {
        margin-left: 0 !important;
        color: #fff;
        background: linear-gradient(
          90deg,
          rgba(33, 107, 255, 1) 0%,
          rgba(102, 182, 255, 1) 100%
        );
        border: none;
      }

      .reset {
        border: 1px solid rgba(38, 151, 255, 1);
        color: rgba(38, 151, 255, 1);
      }

      .condition {
        display: flex;
        align-items: center;
        margin: 10px 30px 10px 0;

        :deep(.el-input .el-input__inner) {
          font-size: 14px;
        }

        span {
          margin: 0 10px 0 0;
        }
      }
    }

    .gongneng {
      margin: 10px 0;

      // .el-button {
      //   color: #fff;
      //   border: none;
      // }
      .add {
        color: #fff;
        border: none;
        background: linear-gradient(
          90deg,
          rgba(33, 107, 255, 1) 0%,
          rgba(102, 182, 255, 1) 100%
        );
      }
      .imp {
        border: 1px solid rgba(33, 107, 255, 1);
        color: rgba(33, 107, 255, 1);
      }
    }
  }
  .scroll {
    flex: 1;
    overflow: auto;
  }
  .footer {
    width: 96%;
    height: calc(100% - 82px);
    margin: 10px auto 10px;

    .el-table--fit {
      height: 100%;
      :deep(.el-table__header-wrapper) {
        background-color: #000;
        font-size: 14px;
        color: #000;

        .cell {
          color: #000;
        }
      }

      :deep(.el-table__row) {
        height: 50px;
        font-size: 14px;
        color: #000;
      }

      :deep(.el-table__row td) {
        padding: 0;
        border: 0;
      }

      .el-button--primary {
        margin-left: 5px;
      }

      :deep(.el-table__body .even) {
        background-color: #fff;
      }

      :deep(.el-table__body .odd) {
        background-color: rgba(240, 243, 247, 1);
      }

      :deep(.options) {
        display: flex;
        justify-content: center;
        align-items: center;

        .reset {
          margin-right: 15px;
          color: rgba(30, 125, 251, 1);
          cursor: pointer;
        }

        .look {
          margin-right: 15px;
          color: rgba(30, 125, 251, 1);
          cursor: pointer;
        }

        .del {
          color: rgba(212, 48, 48, 1);
          cursor: pointer;
        }
      }
    }

    // 添加角色
    :deep(.addRoles) {
      //   height: 420px;
      border-radius: 11px;
      .el-dialog__header {
        border-radius: 11px 11px 0 0;
        background: rgba(237, 241, 245, 1);
        font-weight: 600;
        margin: 0;
        .el-dialog__headerbtn {
          outline: none;
        }
      }
      .el-dialog__body {
        padding: 20px 30px 10px 30px;

        .rolesName {
          margin: 15px 0;
          color: #000;
          height: 600px;
          overflow: auto;
          .rolePermission {
            margin: 10px 0;
          }
        }
        .el-table--fit {
          height: 600px;

          .el-table__header-wrapper {
            background-color: #000;
            font-size: 16px;
            tr {
              color: #000;
              th {
                border-left: 1px solid #dcdfe6 !important;
                &:last-child {
                  border-right: 1px solid #dcdfe6 !important;
                }
                .cell {
                  text-align: center;
                }
              }
            }
          }

          .el-table__row {
            height: 50px;
            font-size: 16px;
            color: #000;
            .el-tree-node__children {
              display: flex;
              flex-wrap: wrap;
              .el-tree-node {
                width: 140px;
                .el-tree-node__content {
                  padding-left: 0 !important;
                }
              }
              // border: 1px solid red;
            }
          }

          .el-table__row td {
            padding: 0;
            // border: 0;
          }
          td {
            border-left: 1px solid #dcdfe6;
            .checkBoxSel {
              padding-left: 20px;
              .el-checkbox {
                .el-checkbox__label {
                  color: #000;
                }
              }
            }
            &:last-child {
              border-right: 1px solid #dcdfe6;
            }
          }
          .el-table__row:hover {
            td {
              background-color: #ffffff !important;
            }
          }

          .el-button--primary {
            margin-left: 5px;
          }

          // .el-table__body .even {
          //   background-color: #fff;
          // }

          // .el-table__body .odd {
          //   background-color: rgba(240, 243, 247, 1);
          // }
        }
        table {
          width: 840px;
          // height: 600px;
          tr {
            td {
              border-top: 1px solid #7c7c7c;
              border-left: 1px solid #7c7c7c;
            }
            td:nth-child(1) {
              width: 120px;
            }
            td:last-child {
              border-right: 1px solid #7c7c7c;
              padding: 10px 0;
              .el-tree {
                .el-tree-node__children {
                  display: flex;
                  flex-wrap: wrap;
                  .el-tree-node {
                    width: 140px;
                    .el-tree-node__content {
                      padding-left: 0 !important;
                    }
                  }
                  // border: 1px solid red;
                }
              }
            }
          }
          tr:nth-child(1) {
            td {
              height: 40px;
            }
          }
          tr:last-child {
            td {
              border-bottom: 1px solid #7c7c7c;
            }
          }
        }

        .options {
          margin: 10px 20px 10px 0;
          width: 100%;
          .btn {
            display: flex;
            flex-direction: row-reverse;
          }
          .queding {
            color: #fff;
            margin-left: 15px;
          }
        }
      }
    }
  }

  .el-pagination {
    width: calc(100vw - 300px);
    margin: 20px auto;
    justify-content: flex-end;
    :deep(.el-pagination__total) {
      color: #000;
    }

    :deep(.el-pagination__goto) {
      color: #000;
    }

    :deep(.el-pagination__classifier) {
      color: #000;
    }

    :deep(.el-input__wrapper) {
      // border: 1px solid rgba(0, 0, 0, 1);
      // border-radius: 5px;
      // box-shadow: none;
      width: 100px;
    }

    :deep(.el-pager li) {
      margin: 0 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }

    :deep(.el-pager li.is-active) {
      // background-color: rgba(111, 182, 184, 1);
      color: rgba(0, 149, 255, 1);
      border: 1px solid rgba(0, 149, 255, 1);
    }

    :deep(.btn-prev) {
      margin-right: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }

    :deep(.btn-next) {
      margin-left: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }
  }
}

.el-input {
  width: 192px;
}
</style>
<style lang="scss">
.usePrefabricate {
  height: 488px;
  .el-select-dropdown__wrap {
    max-height: 100%;
  }
}
</style>
