<template>
  <a-card size="small" :bordered="false" class="productCategoryList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 操作按钮 -->
      <div class="table-operator-nobor" v-if="$hasPermissions('B_productCategory_add')" >
        <a-button id="productCategoryList-add" type="primary"@click="handleAdd()">新增一级分类</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :showPagination="false"
        :defaultExpandedRowKeys="defaultExpandedRowKeys"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            v-if="record.productTypeLevel<3 && $hasPermissions('B_productCategory_add')"
            type="link"
            class="button-success"
            size="small"
            icon="plus"
            :id="'productCategory-addSubItem'+record.id"
            @click="handleAdd(record)">新增子级</a-button>
          <a-button
            v-if="record.pid != 0 && $hasPermissions('B_productCategory_edit')"
            type="link"
            class="button-info"
            size="small"
            icon="edit"
            :id="'productCategory-edit'+record.id"
            @click="handleEdit(record)">编辑</a-button>
          <a-button
            v-if="record.pid != 0 && $hasPermissions('B_productCategory_del')"
            type="link"
            class="button-error"
            size="small"
            icon="delete"
            :id="'productCategory-del'+record.id"
            @click="handleDel(record)">删除</a-button>
          <span v-if="!(record.productTypeLevel<3 && $hasPermissions('B_productCategory_add')) && !(record.pid != 0 && $hasPermissions('B_productCategory_edit')) && !(record.pid != 0 && $hasPermissions('B_productCategory_del'))">--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 新增/编辑产品分类 -->
    <product-category-edit-modal
      v-drag
      :openModal="openModal"
      :nowData="nowData"
      :itemSn="itemSn"
      @ok="handleOk"
      @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable } from '@/components'
import productCategoryEditModal from './editModal.vue'
// 接口
import { productTypeQuery, productTypeDel } from '@/api/productType'
export default {
  name: 'ProductCategoryList',
  mixins: [commonMixin],
  components: { STable, productCategoryEditModal },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      // label 标签布局
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      columns: [
        { title: '分类名称', dataIndex: 'productTypeName', align: 'left', width: '45%' },
        { title: '创建时间', dataIndex: 'createDate', width: '25%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '30%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 190
        }
        // 获取列表数据 无分页
        return productTypeQuery({}).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            //  递归去除空children
            this.recursionFun(data)
            if (data.length > 0) {
              this.defaultExpandedRowKeys[0] = data[0].id
            } else {
              this.defaultExpandedRowKeys = []
            }
          }
          return data
        })
      },
      defaultExpandedRowKeys: [], //  树形数据默认展开项
      openModal: false, //  新增编辑  弹框
      itemSn: '', //  当前sn
      nowData: null //  当前记录数据
    }
  },
  methods: {
    //  新增子级
    handleAdd (row) {
      this.itemSn = null
      if (row) { //  新增子级
        const nowData = row
        nowData.productTypeSn = nowData && nowData.productTypeSn ? nowData.productTypeSn : null
        this.nowData = nowData
      } else { //  最高级
        this.nowData = null
      }
      this.openModal = true
    },
    //  编辑
    handleEdit (row) {
      this.itemSn = row && row.productTypeSn ? row.productTypeSn : null
      const nowData = row
      nowData.productTypeSn = nowData && nowData.productTypeSn ? nowData.productTypeSn : null
      this.nowData = nowData
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemSn = ''
      this.openModal = false
    },
    //  新增/编辑  成功
    handleOk () {
      this.$refs.table.refresh(true)
    },
    //  删除
    handleDel (row) {
      const _this = this
      _this.$confirm({
        title: '提示',
        content: '删除后原数据不可恢复，是否删除？',
        centered: true,
        onOk () {
          _this.spinning = true
          productTypeDel({ sn: row.productTypeSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  递归函数
    recursionFun (data) {
      if (data) {
        data.map((item, index) => {
          if (item.children && item.children.length == 0) {
            delete item.children
          } else {
            this.recursionFun(item.children)
          }
        })
      }
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.$refs.table.refresh(true)
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.$refs.table.refresh(true)
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.$refs.table.refresh()
    }
  }
}
</script>

<style lang="less">
  .productCategoryList-wrap{
    height: 100%;
    // 表格
    .s-table{
      .btn-edit-s{
        color: #1891ff;
        margin: 0 5px;
      }
      .btn-del-s{
        color: #ff4d4f;
      }
      .btn-edit-s.ant-btn:focus:not(.ant-btn-primary):not(.ant-btn-danger), .btn-edit-s.ant-btn:hover:not(.ant-btn-primary):not(.ant-btn-danger){
        color: #1891ff;
        border-color: #1891ff;
      }
      .btn-del-s.ant-btn:focus:not(.ant-btn-primary):not(.ant-btn-danger), .btn-del-s.ant-btn:hover:not(.ant-btn-primary):not(.ant-btn-danger){
        color: #ff4d4f;
        border-color: #ff4d4f;
      }
    }
  }
</style>
