<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :default-expand-all="false"
        :need-page="false"
        :parse-data="parseData"
        cache-key="systemOrganizationTable"
        @done="onDone"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            添加
          </el-button>
          <el-button class="ele-btn-icon" size="small" @click="expandAll">
            展开全部
          </el-button>
          <el-button class="ele-btn-icon" size="small" @click="foldAll">
            折叠全部
          </el-button>
        </template>
        <!-- 标题列 -->
        <template v-slot:title="{ row }">
          <i :class="row.icon"></i> {{ row.title }}
        </template>
        <template v-slot:logo="{ row }">
          <template v-if="row.logo && row.logo[0]">
            <el-image
              style="width: 80px; height: 80px"
              :src="row.logo[0]['url']"
              :preview-src-list="[row.logo[0]['url']]"
            />
          </template>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            v-if="row.level === 1"
            type="primary"
            :underline="false"
            @click="openEditCmmssn(row)"
          >
            佣金配置
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-plus"
            @click="openEdit(null, row.id)"
          >
            添加
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit
      :data="current"
      :parent-id="parentId"
      :organization-list="organizationList"
      :visible.sync="showEdit"
      @done="reload"
    />
    <edit-cmmssn
      :data="current"
      :visible.sync="showEditCmmssn"
      @done="reload"
    />
  </div>
</template>

<script>
  import Edit from './components/edit.vue';
  import EditCmmssn from './components/edit-cmmssn.vue';
  import {
    listOrganizations,
    removeOrganization
  } from '@/api/organization/manage';

  export default {
    name: 'SystemOrganization',
    components: { Edit, EditCmmssn },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            type: 'index',
            width: 45,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'title',
            label: '名称',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'logo',
            label: 'logo',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'logo'
          },
          {
            prop: 'remark',
            label: '备注',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createTime',
            label: '创建时间',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 250,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 全部菜单数据
        organizationList: [],
        // 上级菜单id
        parentId: null,
        showEditCmmssn: false
      };
    },
    methods: {
      /* 表格数据源 */
      datasource({ where }) {
        return listOrganizations({ ...where });
      },
      /* 数据转为树形结构 */
      parseData(data) {
        return this.$util.toTreeData({
          data: data,
          idField: 'id',
          parentIdField: 'parentId'
        });
      },
      /* 表格渲染完成回调 */
      onDone({ data }) {
        //if (!this.organizationList.length) {
        this.organizationList = data;
        //}
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ where: where });
      },
      /* 显示编辑 */
      openEdit(row, parentId) {
        this.current = row;
        this.parentId = parentId;
        this.showEdit = true;
      },
      openEditCmmssn(row) {
        this.current = row;
        this.showEditCmmssn = true;
      },
      /* 删除 */
      remove(row) {
        if (row.children?.length) {
          this.$message.error('请先删除子节点');
          return;
        }
        const loading = this.$loading({ lock: true });
        removeOrganization(row.id)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      },
      /* 展开全部 */
      expandAll() {
        this.$refs.table.toggleRowExpansionAll(true);
      },
      /* 折叠全部 */
      foldAll() {
        this.$refs.table.toggleRowExpansionAll(false);
      }
    }
  };
</script>
