<template>
  <div class="page">
    <LayoutTable :data='tableData' :config='tableConfig' notPagination />
  </div>
</template>

<script>
import table from '@/assets/mixins/table';

export default {
  mixins: [table],
  methods: {
    beforeRender(data) {
      this.allMenu = this.sortByGroup(this.util_copy(data.result));
      // 存在查询条件，展开全部
      if(Object.keys(this.$route.query).length) {
        this.select = this.allMenu.filter(i=>!i.parentId).map(i=>i.id);
      }
      return data;
    },
    afterRender() {
      this.showChild();
    },
    // 按parentId排序分组
    sortByGroup(data) {
      return data.filter(i=>!i.parentId).reduce((prev, item)=>{
        return prev.concat([item], data.filter(i=>i.parentId == item.id));
      }, [])
    },
    // 查看下级菜单
    selectHandler({id}) {
      // 如果已展开，则关闭
      const index = this.select.indexOf(id);
      if(index > -1) {
        this.select.splice(index, 1);
      }else{
        this.select.push(id);
      }
      this.showChild();
    },
    showChild() {
      this.tableData.result = this.allMenu.filter(item=>{
        return !item.parentId || this.select.indexOf(item.parentId) > -1
      });
    },
    // 展示菜单图标
    showIcon(row, column) {
      return <i class={'el-icon-'+row.imgPath}></i>
    },
    // 操作菜单状态
    changeStatus(row, column) {
      return (
        <my-switch 
          confirm 
          url='/menu/save'
          row={row} 
          onChange={this.statusChange}
          value={this.tableData.result} 
          onInput={(data)=>{
            this.tableData.result = data;
          }}/>
      )
    },
    afterDel(id) {
      this.$store.dispatch('login');
    },
    statusChange() {
      this.$store.dispatch('login');
    },
     handleUp(data) {
      let type = 0;
      let url = "/menu/upOrDown";
      this.switchSort(url, data.id, type);
    },

    handleDown(data) {
      let type = 1;
       let url = "/menu/upOrDown";
      this.switchSort(url, data.id, type);
    },

    switchSort(url, id, type) {
      this.loading = true;
      this.$post(url, {
        id: id,
        type: type,
      })
        .then((res) => {
          if (res && res.code && res.code == 1) {
            this.getData()
            this.loading = false;

            this.$message.success("更新排序成功！");
          }
        })
        .catch((error) => {
          this.loading = false;
          this.$message.error(error.message);
        });
    },
  },
  data() {
    return {
      allMenu: [], // 经过分组排序的表格数据
      select: [], // 当前展开的树节点
      query: {
        'pageInfo.prePageResult': -1
      },
      config: {
        search: [
          {
            name: 'name',
            type: 'text',
            label: '菜单名称',
          },
        ],
        columns: [
          {
            type: 'selection',
            width: 60,
          },
          {
            width: 60,
            formatter: row => {
              const icon = this.select.indexOf(row.id) === -1 ? 'right' : 'down';
              return (
                !row.parentId
                ? <div onClick={()=>{this.selectHandler(row)}}>
                  <i class={'el-icon-arrow-'+icon}></i>
                </div>
                : ''
              )
            }
          },
          {
            prop: 'id',
            label: 'ID',
            width: 60,
          },
          {
            prop: 'name',
            label: '名称',
            width: 160,
          },
          {
            prop: 'imgPath',
            label: '图标',
            width: 50,
            formatter: this.showIcon,
          },
          {
            prop: 'authType',
            label: '权限类型',
            formatter: this.formatter,
          },
          {
            prop: 'parentId',
            label: '父ID',
          },
          {
            prop: 'url',
            label: '地址',
          },
          {
            prop: 'status',
            label: '状态',
            width: 100,
            formatter: this.changeStatus,
          },
          {
            width: 280,
            label: '操作',
            formatter: (row)=> {
              return (

               <div>
                   <el-link
              style="margin-right:5px;margin-left:5px"
              icon="el-icon-top"
              onClick={() => {
                this.handleUp(row);
              }}
            ></el-link>

            <el-link
              style="margin-right:5px;margin-left:5px"
              icon="el-icon-bottom"
              onClick={() => {
                this.handleDown(row);
              }}
               
  ></el-link>

                <table-buttons row={row} onEdit={this.toEdit} onDel={this.toDel} />

                </div>
              )
            },
          },
        ],
      },
    }
  }
}
</script>


