<template>
  <a-card class="cust-list-cart">
    <div>
      <a-table
        size="middle"
        :columns="columns"
        :dataSource="businessList"
        :pagination="false"
        rowKey="id"
        bordered
      >
        <div slot="title" slot-scope="data">
          <span class="cust-title">
            <a-icon type="hdd" />业务列表{{ data.none }}
          </span>
          <div class="cust-table-operator">
            <a-button-group>
              <a-button type="primary" @click="addNew">新建</a-button>
            </a-button-group>
          </div>
        </div>
        <template #action="text, record">
          <span >
            <a @click="addChild(record)">添加子业务</a>
            <a-divider type="vertical" />
            <a @click="editRow(record)">编辑</a>
            <a-divider type="vertical" />
            <span
              v-show="record.children == null"
            >
              <a @click="gotoDetail(record)">操作管理</a>
              <a-divider type="vertical" />
            </span>
            <a @click="onDelete(record)">删除</a>
          </span>
        </template>
      </a-table>
    </div>
    <sys-business-detail @modifyItem="modifyItem" ref="sysBusinessItemDetail" />
  </a-card>
</template>

<script>
import { deleteOne, save, update, getList } from '@/api/system/sysBusiness'
import SysBusinessDetail from './SysBusinessDetail'

const columns = [
  { title: '业务名称', width: 230, dataIndex: 'name' },
  { title: '业务编码', width: 100, dataIndex: 'code' },
  { title: '图标', width: 80, dataIndex: 'icon' },
  { title: '请求路由', dataIndex: 'vrPath' },
  { title: '排序', width: 60, dataIndex: 'showOrder' },
  {
    title: '操作',
    key: 'operation',
    width: 250,
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'SysBusinessList',
  components: { SysBusinessDetail },
  data() {
    return {
      isChildBusiness: false, //是否是子业务
      rootId: 'SYS000', //根节点id
      modalTitle: '新建业务',
      modalWidth: 800,
      columns: columns,
      businessList: [],
      selectedRows: [],
      needTotalList: [],
      businessForm: null,
      sysBusiness: {},
      apiUrl: '/sys/business/',
      detailPathName: 'sysBusinessOperateDetail'
    }
  },
  methods: {
    loadData() {
      getList(this.apiUrl).then((response) => {
        if (response && response.code == SYS_CONST.REQUEST.SUCCEED) {
          let rootBusiness = response.data[0]
          const list = rootBusiness.children
          this.checkChildren(list)
          this.businessList = list
        }
      })
    },
    checkChildren(list) {
      list.map((m) => {
        if (m.children.length == 0) {
          m.children = null
        } else {
          this.checkChildren(m.children)
        }
      })
    },
    modifyItem(item) {
      //执行保存操作
      this.$valueCopy(this.sysBusiness, item)
      if (this.sysBusiness.id) {
        update(this.apiUrl, this.sysBusiness).then((response) => {
          if (response && response.code == SYS_CONST.REQUEST.SUCCEED) {
            this.loadData()
          }
        })
      } else {
        save(this.apiUrl, this.sysBusiness).then((response) => {
          if (response && response.code == SYS_CONST.REQUEST.SUCCEED) {
            this.loadData()
          }
        })
      }
    },
    addChild(row) {
      this.sysBusiness = {}
      this.isChildBusiness = true
      this.sysBusiness.parentCode = row.code
      this.$refs.sysBusinessItemDetail.show(
        SYS_CONST.OPT_TYPE.ADD,
        this.isChildBusiness,
        row
      )
    },
    addNew() {
      this.sysBusiness = {}
      this.isChildBusiness = false
      this.sysBusiness.parentCode = this.rootCode
      this.$refs.sysBusinessItemDetail.show(
        SYS_CONST.OPT_TYPE.ADD,
        this.isChildBusiness
      )
    },
    editRow(row) {
      if (row.parentId == this.rootCode) {
        //编辑一级菜单
        this.isChildBusiness = false
      } else {
        this.isChildBusiness = true
      }
      this.sysBusiness = row
      this.$refs.sysBusinessItemDetail.show(
        SYS_CONST.OPT_TYPE.MODIFY,
        this.isChildBusiness,
        row
      )
    },
    gotoDetail(row) {
      this.$router.push({
        name: this.detailPathName,
        query: { id: row.id, name: row.name, pageType: 'sys' }
      })
    },
    deleteRow(id) {
      deleteOne(this.apiUrl, id).then((response) => {
        if (response && response.code == SYS_CONST.REQUEST.SUCCEED) {
          this.loadData()
        }
      })
    },
    onDelete(row) {
      let _this = this
      if (row.children != null) {
        this.$error({
          title: '错误提示',
          content: '该业务存在子业务，请先删除子业务'
        })
        return
      }
      this.$confirm({
        title: '提示',
        content: '业务下面的操作和资源也会一起删除，确定删除选中的业务吗？',
        onOk() {
          _this.deleteRow(row.id)
        },
        onCancel() {}
      })
    }
  },
  mounted() {
    this.loadData()
  }
}
</script>
