<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :default-expand-all="false"
        :need-page="false"
        cache-key="systemMenuTable"
        @done=""
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit('add')"
          >
            添加
          </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:areaImg="{ row }">
          <el-button v-if="row.areaImg.length" @click="downloadFile(row.areaImg[0])" type="text">查看</el-button>
        </template>


        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('update',row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove(row)"
            v-if="row.id != 99999"
          >
            <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>
    <!-- 编辑弹窗 -->
    <area-edit
      v-if="showEdit"
      :visibleFlag.sync="showEdit"
      ref="areaEditRef"
      :data="editData"
      :parent-id="parentId"
      :organization-list="dataTree"
      @done="reload"
    />
  </div>
</template>

<script>

import areaEdit from './components/area-edit.vue';
import {
  listOrganizations,
  removeOrganization
} from '@/api/system/organization';
import {basicAreaDeleteAPI, basicAreaPageAPI} from "@/api/regionalManage";
import {getByCode} from "@/api/system/dictionary-data";
import {getFile} from "@/api/system/file";

export default {
  name: 'SystemOrganization',
  components: {areaEdit},
  data() {
    return {
      // 加载状态
      loading: false,
      // 列表数据
      data: [],
      dataTree: [],
      // 选中数据
      current: null,
      // 是否显示表单弹窗
      showEdit: false,
      // 编辑回显数据
      editData: null,
      // 上级id
      parentId: null,
      dictList:{}
    };
  },
  computed: {
    // 表格列配置
    columns() {
      return [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'name',
          label: '区域名称',
          showOverflowTooltip: true,
          align: 'center',
          minWidth: 110,
          slot: 'name'
        },
        {
          prop: 'areaCode',
          label: '区域编码',
          showOverflowTooltip: true,
          align: 'center',
          minWidth: 110,
        },
        {
          prop: 'areaImg',
          label: '地图/示意图',
          showOverflowTooltip: true,
          align: 'center',
          minWidth: 110,
          slot: 'areaImg'

        },
        {
          prop: 'areaLevel',
          label: '区域等级',
          showOverflowTooltip: true,
          align: 'center',
          formatter: (row, column) => {
            return this.getDictV('main_area_level',row.areaLevel) ;
          },
          minWidth: 110,
        },
        {
          prop: 'areaType',
          label: '区域类型',
          showOverflowTooltip: true,
          align: 'center',
          formatter: (row, column) => {
            return this.getDictV('main_area_type',row.areaType) ;
          },
          minWidth: 110,
        },
        {
          prop: 'areaSort',
          label: '排序',
          showOverflowTooltip: true,
          align: 'center',
          width: 60
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 190,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ]
    },
  },
  created() {
    //this.query();
  },
  methods: {
    getDictV(code, val) {
      if (!this.dictList[code]) return '';
      return this.dictList[code].find(item => item.value == val)?.label
    },
    async getDictList(code) {
      let {data: res} = await getByCode(code)
      this.dictList[code] = res.map(item => {
        let values = Object.keys(item)
        return {
          value: values[0],
          label: item[values[0]]
        }
      })
    },
    downloadFile(file) {
      getFile({objectName: file.storePath}, file.type);
    },
    /* 查询 */
    async datasource() {
      await this.getDictList('main_area_level')
      await this.getDictList('main_area_type')
      const data = await basicAreaPageAPI(
        {
          pageNum: 1,
          size: 9999
        }
      )
      this.data = this.$util.toTreeData({
        data: data,
        idField: 'id',
        parentIdField: 'parentId'
      });
      this.dataTree = [{
        name:'顶级区域',
        id: '0',
        children:  this.data,
      }]
      return this.data

    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload();
    },
    /* 展开全部 */
    expandAll() {
      this.$refs.table.toggleRowExpansionAll(true);
    },
    /* 折叠全部 */
    foldAll() {
      this.$refs.table.toggleRowExpansionAll(false);
    },
    /* 显示编辑 */
    openEdit(type,row) {
      this.showEdit = true;
      this.$nextTick(() => {
        this.$refs.areaEditRef.init(type,row);
      });
    },
    /* 删除 */
    remove(row) {
      this.$confirm('确定要删除选中的区域吗?', '提示', {
        type: 'warning'
      }).then(() => {
           const loading = this.$loading({lock: true});
        basicAreaDeleteAPI([row.id])
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              // this.$message.error(e.message);
            });
        })
        .catch(() => {
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.sys-organization-list {
  height: calc(100vh - 264px);
  box-sizing: border-box;
  border-width: 1px;
  border-style: solid;
  overflow: auto;
}

.sys-organization-list :deep(.el-tree-node__content) {
  height: 30px;

  & > .el-tree-node__expand-icon {
    margin-left: 10px;
  }
}
</style>
