<template>
  <a-card size="small" :bordered="false" class="expenseTypeList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 操作按钮 -->
      <div ref="tableSearch" class="table-operator-nobor">
        <a-button v-if="$hasPermissions('B_basicData_expenseType_add')" id="expenseTypeList-add" type="primary" class="button-error" @click="handleAdd()">新增一级类型</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :showPagination="false"
        :defaultExpandedRowKeys="defaultExpandedRowKeys"
        :defaultLoadData="false"
        bordered>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <!-- 系统内置类型不可编辑和删除  tenantId为0是内置 -->
          <a-button
            v-if="record.typeLevel<3 && $hasPermissions('B_basicData_expenseType_add')"
            type="link"
            class="button-success"
            size="small"
            icon="plus"
            id="expenseType-addSubItem"
            @click="handleAdd(record)">新增子级</a-button>
          <a-button
            v-if="record.pid != 0 && $hasPermissions('B_basicData_expenseType_edit') && record.tenantId!=0"
            type="link"
            class="button-info"
            size="small"
            icon="edit"
            id="expenseType-edit"
            @click="handleEdit(record)">编辑</a-button>
          <a-button
            v-if="record.pid != 0 && $hasPermissions('B_basicData_expenseType_del') && record.tenantId!=0"
            type="link"
            class="button-error"
            size="small"
            icon="delete"
            id="expenseType-del"
            @click="handleDel(record)">删除</a-button>
          <span v-if="!(record.typeLevel<3 && $hasPermissions('B_basicData_expenseType_add')) && !(record.pid != 0 && $hasPermissions('B_basicData_expenseType_edit') && record.tenantId!=0) && !(record.pid != 0 && $hasPermissions('B_basicData_expenseType_del') && record.tenantId!=0)">--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 新增/编辑费用类型 -->
    <expense-type-edit-modal :openModal="openModal" :nowData="nowData" :itemId="itemId" @ok="handleOk" @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { costTypeAllQuery, costTypeDel } from '@/api/costType'
import { STable } from '@/components'
import expenseTypeEditModal from './editModal.vue'
export default {
  name: 'ExpenseTypeList',
  components: { STable, expenseTypeEditModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      columns: [
        { title: '费用类型名称', dataIndex: 'name', width: '50%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '创建时间', dataIndex: 'createDate', width: '25%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '25%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        return costTypeAllQuery({}).then(res => {
          if (res.status == 200) {
            //  递归去除空children
            this.recursionFun(res.data)
            if (res.data.length > 0) {
              this.defaultExpandedRowKeys[0] = res.data[0].id
            } else {
              this.defaultExpandedRowKeys = []
            }
          }
          this.spinning = false
          return res.data
        })
      },
      defaultExpandedRowKeys: [], //  树形数据默认展开项
      openModal: false, //  新增编辑  弹框
      itemId: '', //  当前id
      nowData: null //  当前记录数据
    }
  },
  methods: {
    //  新增子级
    handleAdd (row) {
      this.itemId = null
      if (row) { //  新增子级
        const nowData = row
        nowData.psn = nowData && nowData.costTypeSn ? nowData.costTypeSn : null
        this.nowData = nowData
      } else { //  最高级
        this.nowData = null
      }
      this.openModal = true
    },
    //  编辑
    handleEdit (row) {
      this.itemId = row && row.id ? row.id : null
      const nowData = row
      nowData.psn = nowData && nowData.costTypeSn ? nowData.costTypeSn : null
      this.nowData = nowData
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
    },
    //  新增/编辑  成功
    handleOk () {
      this.$refs.table.refresh(true)
    },
    //  删除
    handleDel (item) {
      const _this = this
      _this.nowId = item.id
      _this.$confirm({
        title: '提示',
        content: '删除后原数据不可恢复，是否删除？',
        centered: true,
        onOk () {
          _this.spinning = true
          costTypeDel({ id: _this.nowId }).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)
          }
        })
      }
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 195
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.$refs.table.refresh(true)
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.$refs.table.refresh(true)
    }
  }
}
</script>

<style lang="less">
  .expenseTypeList-wrap{
    // 表格
    .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>
