<template>
  <div class="percentage-content">
    <div class="content">
      <div class="h-layout space-between h-center">
        <div>
          <div class="h-layout" style="display: flex;align-items: center;">
            <div class="tool-item" @click="getList">
              <svg-icon name="refurbish" class="icon" />
              <span>刷新</span>
            </div>
            <div
              class="tool-item"
              @click="(dialogVisible = true), (form = {}), (readonly = false)"
            >
              <svg-icon name="add" class="icon" />
              <span>新增</span>
            </div>
            <div class="label-select filter-tab">
              <label>岗位</label>
              <el-tree-select
              v-model="listperformid"
              :data="postTree"
              :render-after-expand="false"
              check-strictly
              :filter-node-method="filterPostMethod"
              filterable
              clearable
              @change="getList"
            />
            </div>
          </div>
        </div>
        <div style="width: 30%">
          <div>
            <el-input
              v-model="search"
              @keydown.enter.native="getList"
              placeholder="请输入岗位名称"
              class="search-input"
            >
              <template #append>
                <span @click="getList" class="search-bottom">
                  <svg-icon name="search" class="icon_class" />
                  <span>搜索</span>
                </span>
              </template>
            </el-input>
          </div>
        </div>
      </div>
      <el-table
        ref="loading"
        class="table"
        v-loading="tableDataloading"
        :data="tableData"
        header-row-class-name="el-one-header"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column
          align="center"
          :resizable="true"
          width="100"
          label="序列"
          type="index"
        >
          <template #default="scope">
            {{ scope.$index + 1 + (currentPage - 1) * pageSize }}
          </template>
        </el-table-column>
        <el-table-column
          prop="performclassname"
          :resizable="true"
          label="岗位名称"
        >
        </el-table-column>
        <el-table-column
          prop="performname"
          :resizable="true"
          align="center"
          label="负责人"
        >
        </el-table-column>
        <el-table-column prop="areaPathName" :resizable="true" label="所属地区">
        </el-table-column>
        <el-table-column
          :resizable="true"
          align="center"
          width="180"
          label="操作"
        >
          <template #default="scope">
            <span
              class="operate"
              @click="
                (dialogVisible = true), (form = scope.row), (readonly = false)
              "
            >
              <svg-icon name="edit" class="icon"></svg-icon>
              <span class="edit">编辑</span>
            </span>
            <span class="operate" @click="toDetail(scope.row)">
              <svg-icon name="detail" class="icon"></svg-icon>
              <span class="detail">详情</span>
            </span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="getList"
        @current-change="getList"
        v-model:current-page.sync="currentPage"
        :page-sizes="[10, 15, 20, 30]"
        v-model:page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </div>
    <!-- 新增/修改 -->
    <AddOrEdit
      :visible="dialogVisible"
      :form="form"
      :readonly="readonly"
      @close="onclone"
    />
  </div>
</template>

<script lang='ts' setup>
import { onMounted, reactive, ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();

import { getListperformPage, performDel } from "@/api/Sys";
import { userListperformTree } from "@/api/Sys";
import AddOrEdit from "./add-or-edit-post.vue";

import useUserStore from "@/store/modules/user";
const userStore = useUserStore();

const user = ref(JSON.parse(userStore.userInfo));
const userId = ref(user.value.userId);

const search = ref([]);
const tableData = ref([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

const dialogVisible = ref(false);
const form = ref({});
const readonly = ref(false);

const listperformid = ref('')
//组织list
const postTree = ref([]);
/**筛选组织*/
const filterPostMethod = (value, data) => data.label.includes(value);
/**
 * 获取组织
 */
const getPostTree = () => {
  userListperformTree().then((res: any) => {
    if (res.code == 1) postTree.value = handlePostData(res.data);
  });
};
/**处理组织数据 */
let handlePostData = (list) => {
  let data = list.map((item) => {
    return {
      label: item.chinaName ? item.performclassname + '-' + item.chinaName :item.performclassname,
      value: item.listperformid,
      children: item.children ? handlePostData(item.children) : [],
    };
  });
  return data;
};

//获取列表
const tableDataloading = ref(false);
const getList = () => {
  tableDataloading.value = true;
  let params = {
    condition: search.value,
    limit: pageSize.value,
    page: currentPage.value,
    state: null,
    unitId: null,
    listperformid:listperformid.value
  };
  getListperformPage(params).then((res: any) => {
    tableData.value = res.data.list;
    total.value = res.data.total;
    tableDataloading.value = false;
  });
};

//关闭新增弹窗
const onclone = () => {
  dialogVisible.value = false;
  form.value = {};
  getList();
};

/**跳转岗位详情 */
const toDetail = (item: any) => {
  router.push({
    name: "post-detail",
    query: { id: item.listperformid },
  });
};

onMounted(() => {
  getPostTree();
  getList();
});
</script>

<style lang="scss" scoped>
.content {
  width: 100%;
  background: #fff;
  border-radius: 0.26042vw;
  box-shadow: 0 0 0.41667vw 0 rgba(8, 33, 85, 0.1);
  height: 100%;
  padding: 0.83333vw;
  overflow-y: auto;
  overflow-x: hidden !important;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -o-box-sizing: border-box;
  -ms-box-sizing: border-box;
  box-sizing: border-box;

  .table {
    margin-top: 16px;
  }
}
</style>