<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="label_className" placeholder="请输入分类名称" class="filter-item"/>
      <el-input v-model="label_classShortName" placeholder="请输入分类简写" class="filter-item"/>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search">搜索</el-button>
      <el-button v-permission="'HY_add'" type="primary" class="filter-item" icon="el-icon-plus" @click="btn_add">添加</el-button>
    </div>
    <el-table :data="dataLists" style="width: 100%;margin-bottom: 20px;" row-key="id" border :default-expand-all="defaultExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="text" label="分类名称" align="center"></el-table-column>
      <el-table-column prop="code" label="分类简写" align="center"></el-table-column>
      <el-table-column prop="iconcls" label="排序" align="center"></el-table-column>
        <el-table-column label="操作" align="center" class-name="oparate_btn" fixed="right">
          <template slot-scope="scope">
            <el-button v-permission="'HY_edit'" type="text" @click="btn_edit(scope.row.id)">编辑</el-button>
            <el-button v-permission="'HY_delete'" type="text" @click="btn_delete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
    </el-table>
    <pagination
      v-show="pageParams.total > 0"
      :total="pageParams.total"
      :pageindex.sync="pageParams.pageindex"
      :pagesize.sync="pageParams.pagesize"
      class="pagination"
      @pagination="getList" />
  </div>
</template>

<script>

import { getCommodityType, deleteCommodityType } from '@/api/commodityManagement/commodityType'
import addOrEdit from './components/addOrEdit'
import { formatterContent } from '@/utils'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装

export default {
  name: 'CommodityType',
  components: {
    Pagination
  },
  data() {
    return {
      loading: false,
      defaultExpandAll: false,
      treeclickId: 0, //选中树id
      label_className: '', //分类名称
      label_classShortName: '', //分类简称
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      dataLists: [] // 列表数据
    }
  },
  created() {
    this.getList()
    document.onkeyup = (e) => {
      if (e.keyCode === 13) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      return new Promise(resolve => {
        const data = {
          pageindex: this.pageParams.pageindex, // int 第几页
          pagesize: this.pageParams.pagesize, // int 每页几条信息
          ClassId: '',
          ClassName: this.label_className,
          ClassShortName: this.label_classShortName,
          ParentId: -1,
        }
        getCommodityType(data).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.dataLists = response.data
          }
        })
        resolve()
      })
    },
    btn_search() {
      this.pageParams.pageindex = 1
      this.getList()
    },
    btn_add() {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': '' }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['80%', '90%'],
        title: '添加商品分类'
      })
    },
    btn_edit(id) {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': id }// props
        },
        area: ['80%', '90%'],
        title: '编辑商品分类'
      })
    },
    btn_delete(id) {
      this.$confirm('您确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          ids: `,${id},` 
        }
        deleteCommodityType(data).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
