<template>
  <div class="school-manage pg-main">
    <div class="pg-title">
      <span>用学校管理</span>
    </div>
    <div class="pg-select-infos" style="padding: 20px">
      <Button
        style="min-left: 10px"
        type="primary"
        icon="md-search"
        @click="load"
        >查询</Button
      >
      <Button
        type="primary"
        icon="md-refresh"
        :loading="loading"
        @click="toSyncSchool"
        >机构同步</Button
      >
    </div>
    <div class="pg-body">
      <Table
        ref="user_table"
        :columns="columns"
        :data="list"
        :stripe="true"
        :disabled-hover="true"
      ></Table>
    </div>

    <!-- SchoolSetDialog -->
    <school-set-dialog
      ref="SchoolSetDialog"
      :school="curRow"
    ></school-set-dialog>
  </div>
</template>

<script>
import { axios } from "../../libs/http";
import SchoolSetDialog from "./components/school-set-dialog.vue";

export default {
  name: "school-manage",
  components: { SchoolSetDialog },
  data() {
    return {
      query: {
        pageNo: 1,
        pageSize: 10,
        totalElements: 0,
        totalPages: 0,
      },
      columns: [
        {
          type: "index",
          title: "序号",
          width: 80,
        },
        {
          title: "学校名称",
          key: "name",
          minWidth: 200,
        },
        {
          title: "学校code",
          key: "code",
          minWidth: 100,
        },
        {
          title: "accessKey",
          key: "accessKey",
          minWidth: 300,
        },
        {
          title: "accessSecret",
          key: "accessSecret",
          minWidth: 300,
        },
        {
          title: "操作",
          key: "show_more",
          align: "center",
          width: 100,
          render: (h, params) => {
            return h("div", [
              h(
                "Tooltip",
                {
                  props: {
                    content: "配置",
                    placement: "top",
                  },
                },
                [
                  h(
                    "span",
                    {
                      style: {
                        cursor: "pointer",
                        padding: "0 10px",
                      },
                      on: {
                        click: () => {
                          this.toSet(params.row);
                        },
                      },
                    },
                    [
                      h(
                        "Icon",
                        {
                          props: {
                            size: "22",
                            type: "ios-settings",
                          },
                        },
                        ""
                      ),
                    ]
                  ),
                ]
              ),
            ]);
          },
        },
      ],
      list: [],
      curRow: {},
      loading: false,
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      axios.post("/system/common/school/list", {}).then((response) => {
        let data = response.data.data;
        this.list = data || [];
      });
    },
    toSet(row) {
      this.curRow = row;
      this.$refs.SchoolSetDialog.open();
    },
    async toSyncSchool() {
      if (this.loading) return;
      this.loading = true;

      const res = await axios.post("/system/auth/sync/org").catch(() => {});
      this.loading = false;

      if (!res) return;

      this.$Message.success("同步成功");
      this.load();
    },
  },
};
</script>
