<!--
  @description: 分类管理
  @author: dijia
  @routerPath: /goodsClassManage
  @date:  2021/06/29
-->
<template>
  <div>
    <div class="shopmall-page-title">
      分类管理列表
    </div>
    <div class="search-content clearfix">
      <section class="fl">
        <el-form ref="searchForm" :model="searchForm" :inline="true">
          <el-form-item label="分类名称" prop="classifyName">
            <el-input v-model.trim="searchForm.classifyName" placeholder="请输入分类名称" />
          </el-form-item>
          <el-form-item label="分类编码" prop="classifyNo">
            <el-input v-model.trim="searchForm.classifyNo" placeholder="请输入分类编码" />
          </el-form-item>
        </el-form>
      </section>
      <div class="fr">
        <el-button type="purple" @click.native="getList">
          搜索
        </el-button>
        <el-button type="gray" @click.native="resetList">
          重置
        </el-button>
      </div>
    </div>
    <div class="shopmall-page-add">
      <el-button type="purple" @click="editGoodsClass('','add')">
        新 增
      </el-button>
      <el-button @click="handleExpand()">
        全部{{ isExpand?'收起':'展开' }}
      </el-button>
    </div>
    <div class="shopmall-view">
      <el-table
        ref="smallTable"
        v-loading="loading"
        :data="allList"
        row-key="id"
        :default-expand-all="isExpand"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        size="small"
      >
        <el-table-column label="分类名称" min-width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.classifyName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分类编码" min-width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.classifyNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建日期" align="center" min-width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" min-width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.useStatus==='01'" style="color:#00AACA">启用</span>
            <span v-if="scope.row.useStatus==='02'" style="color:#F77C46">禁用</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="300"
          align="center"
        >
          <template slot-scope="scope">
            <el-link v-if="scope.row.classifyCategory!=='3'" type="primary" @click="editGoodsClass(scope.row,'rowAdd')">
              [新增子级]
            </el-link>
            <el-link type="primary" @click="editGoodsClass(scope.row,'edit')">
              [编辑]
            </el-link>
            <el-link type="primary" @click="setEnablingClickBtn(scope.row)">
              <span v-if="scope.row.useStatus==='01'" style="color:#F77C46">[禁用]</span>
              <span v-if="scope.row.useStatus==='02'">[启用]</span>
            </el-link>
            <el-link type="primary" @click="deleteGoodsClass(scope.row.id)">
              [删除]
            </el-link>
            <el-link type="primary" @click="goodsClassParam(scope.row,scope.$index)">
              [参数设置]
            </el-link>
            <!-- <el-link type="primary" @click="managingGoods(scope.row,scope.$index)">
              [管理商品]
            </el-link> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--  新增/编辑菜单弹框 -->
    <goodsClassDialog ref="goodsClassAdd" :call-back="getList" :menu-list="allList" :second-menu-list="secondList" />
    <!-- 参数设置弹框 -->
    <goodsParamManageDialog ref="goodParamDialog" />
  </div>
</template>

<script>
/* api */
import {
  getGoodsClassListTree,
  doDeleteGoodsClass,
  updateGoodClassUseStatus
} from '@/api/shoppingmall/products'
/* 组件 */
import goodsClassDialog from './goodsClassAdd-dialog'// 新增/编辑菜单弹框
import goodsParamManageDialog from './goodsParamManageList-dialog'// 参数设置弹框
export default {
  name: 'GetGoodsClassList',
  components: {
    goodsClassDialog, // 新增/编辑菜单弹框
    goodsParamManageDialog // 参数设置弹框
  },

  data() {
    return {
      loading: false,
      isExpand: false, // 是否展开树 false 否 true 是
      allList: [], // 全部列表数据
      // 搜索参数
      searchForm: {
        classifyName: '', // 分类名称
        classifyNo: '' // 分类编码
      },
      secondList: [] // 二级分类数据
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 重置
    resetList() {
      this.$refs.searchForm.resetFields()
      this.getList()
    },
    getList() {
      this.getGoodsClassList(this.searchForm)
    },
    // 是否展开table(展开与折叠切换)
    handleExpand() {
      this.isExpand = !this.isExpand
      this.$nextTick(() => {
        this.forArr(this.allList, this.isExpand)
      })
    },
    // 遍历
    forArr(arr, isExpand) {
      this.isExpand = isExpand
      arr.forEach(i => {
        // toggleRowExpansion(i, isExpand)用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
        this.$refs.smallTable.toggleRowExpansion(i, isExpand)
        if (i.children) {
          this.forArr(i.children, isExpand)
        }
      })
    },
    // 列表数据展示
    getGoodsClassList(params) {
      this.loading = true
      getGoodsClassListTree(params).then(response => {
        this.loading = false
        this.allList = response.data
      }).catch(() => {
        this.loading = false
      })
    },
    // 新增菜单/编辑菜单
    editGoodsClass(row, type) {
      // secondList 二级分类数组
      if (type === 'rowAdd' && row.classifyCategory === '2') {
        this.allList.forEach(item => {
          if (item.id === row.classifyParentId) {
            this.secondList = item.children
          }
        })
      }
      this.$refs.goodsClassAdd.open({
        type: type, // add新增 edit 编辑 rowAdd 列表内的新增
        row: row // 菜单信息
      })
    },
    // 删除分类
    deleteGoodsClass(id) {
      this.$confirm('确认要删除分类吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        doDeleteGoodsClass(id).then(res => {
          if (res.header.ret === '0') {
            this.$message({
              message: ' 删除成功！',
              type: 'success'
            })
            this.getGoodsClassList(this.searchForm)
          }
        })
      })
    },
    // 列表参数设置btn
    goodsClassParam(row, index) {
      this.$refs.goodParamDialog.open({
        id: row.id,
        classifyName: row.classifyName,
        classifyNo: row.classifyNo
      })
    },
    // 管理商品
    managingGoods(row, index) {
      this.$router.push('/personalCenter/commodityManageList')
    },
    /**
     * 禁用and启用
     */
    setEnablingClickBtn(item) {
      const useStatus = item.useStatus === '01' ? '02' : '01'
      const dto = { id: item.id, useStatus }
      this.$confirm('确认要修改分类状态吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        updateGoodClassUseStatus(dto).then((res) => {
          if (res.header.ret === '0') {
            this.getList()
            this.$message({
              message: ' 修改状态成功！',
              type: 'success'
            })
          }
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import '~@assets/style/common/public.less';
.functionIconStyle_icon{
  color: @hyacinthine;
  margin-right: 10px;
}
.scrollbar {
  height: 80vh;
  margin-top: 10px;
}

.filter-container {
  margin: 30px 20px !important;
}
.shopmall-page-add {
  width: 100%;
  margin-top:  12px;
  padding-left: 12px;
  display: flex;
  align-items: center;
}
</style>
