<template>
  <div>

    <div>
      <div ref="lxHeader">
        <lx-header icon="md-apps" :text="$route.meta.title" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content">
            <div class="searchwrap" style="display: flex;align-items: center;">
              <div>
                <el-input v-model="select.name" placeholder="请输入目录名称"></el-input>
              </div>
              <div>
                <el-button type="primary" size="small" @click="select.page=1,getList()">查询</el-button>
              </div>
              <div>
                <el-button type="primary" size="small" @click="editCatalog('add')">新增</el-button>
              </div>
            </div>
          </div>

        </lx-header>
      </div>
    </div>

    <div>
      <xy-table :list="list" :isPage="false" rowKey="id"
        :table-item="table_item">
        <template v-slot:btns>
          <el-table-column align='center' label="操作" width="240" header-align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="editCatalog('child',scope.row.id,scope.row.name)">子目录</el-button>
              <el-button type="primary" size="small" @click="editCatalog('editor',scope.row.id)">编辑</el-button>
              <el-popconfirm style="margin:0 10px" @confirm="deleteList(scope.row.id)" title="确定删除吗？">
                <el-button type="danger" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </xy-table>
    </div>

    <add-catalog ref="addCatalog" @refresh="getList"></add-catalog>
  </div>
</template>

<script>
  import addCatalog from './components/addCatalog.vue';
  import {
    index,
    destroy
  } from "@/api/system/baseForm.js"
  export default {
    components: {
      addCatalog
    },
    data() {
      return {
        select: {
          name: '',
          table_name: 'catalogs',
          page: 1,
          page_size: 999,
        },
        list: [],
        table_item: [{
          prop: 'name',
          label: '目录名称',
          align: 'left',
        },{
          prop: 'sort',
          label: '排序',
          align: 'center',
          width:50
        }]

      }
    },
    created() {
      this.getList()
    },
    methods: {
      editCatalog(type, id,pName) {
        if (type == 'editor') {
          this.$refs.addCatalog.id = id
        }
        if(type=='child'){
          this.$refs.addCatalog.setPid(id,pName)
        }
        this.$refs.addCatalog.type = type
        this.$refs.addCatalog.isShow = true
      },
      async getList() {
        const res = await index({
          page_size: this.select.page_size,
          page: this.select.page,
          sort_type:'ASC',
          sort_name:'sort',
          table_name: this.select.table_name,
          filter: [{
            "key": "name",
            "op": "like",
            "value": this.select.name
          }],
        })
        if(res.data.length>0){
          this.list = this.base.buildTree(res.data)

        }

      },
      deleteList(id) {
        var that = this;
        destroy({
          id: id,
          table_name: this.select.table_name
        }).then(response => {
          this.$Message.success('操作成功');
          this.getList()
        }).catch(error => {
          console.log(error)
          reject(error)
        })
      },
    }
  }
</script>

<style lang="scss" scoped>
  .searchwrap {
    display: flex;
    align-items: center;

    &>div {
      display: flex;
      align-items: center;
      margin-right: 10px;

      span {
        min-width: 70px;
      }
    }
  }
</style>
