<template>
  <div>
    <CardContainer>
      <vxe-toolbar>
        <template #buttons>
          <el-button icon="el-icon-plus" type="primary" size="small" @click="isShowAdd = true">新增</el-button>
          <el-button icon="el-icon-search" type="primary" plain size="small" @click="getList">搜索</el-button>
        </template>
      </vxe-toolbar>
      <vxe-table
        style="margin-top: 10px;"
        ref="table"
        :loading="loading"
        keep-source
        :row-config="{ useKey: 'id', isHover: true }"
        :column-config="{ resizable: true }"
        :edit-rules="validRules"
        :edit-config="{ trigger: 'manual', mode: 'row', showStatus: true }"
        :tree-config="{ rowField: 'id', parentField: 'pid' }"
        :data="tableData">
        <vxe-column type="seq" width="58" align="center"></vxe-column>
        <vxe-column field="name" tree-node width="160" title="菜单" :edit-render="{ name: 'input', attrs: { type: 'text'} }"></vxe-column>
        <vxe-column field="path" title="路由地址" min-width="140" :edit-render="{ name: 'input', attrs: { type: 'text'} }">
          <template #default="{ row }">
            <template v-if="/#/g.test(row.path)">
              <SvgIcon icon-class="folder" class-name="icon-folder"></SvgIcon>
            </template>
            <template v-else>
              <span>{{ row.path }}</span>
            </template>
          </template>
        </vxe-column>
        <vxe-column field="api_profix" min-width="140" title="api前缀" :edit-render="{ name: 'input', attrs: { type: 'text'} }">
          <template #default="{ row }">
            <template v-if="/#/g.test(row.path)">
              <SvgIcon icon-class="folder" class-name="icon-folder"></SvgIcon>
            </template>
            <template v-else>
              <span>{{ row.path }}</span>
            </template>
          </template>
        </vxe-column>
        <vxe-column field="visible" width="100" align="center" title="是否显示" :edit-render="{ name: 'select', options: [{ value: 1, label: '显示' },{ value: 0, label: '隐藏' }] }"></vxe-column>
        <vxe-column field="icon" width="160" align="center" title="图标" :edit-render="{ name: 'input', attrs: { type: 'text'} }">
          <template #default="{ row }">
            <Icon :icon="row.icon"></Icon>
          </template>
        </vxe-column>
        <vxe-column field="sortnumber" width="80" title="排序" align="center" :edit-render="{ name: 'input', attrs: { type: 'number' } }"></vxe-column>
        <vxe-column field="operate" title="操作" min-width="240">
          <template #default="{ row }">
            <template v-if="isActiveStatus(row)">
              <el-button size="small" type="primary" @click="saveRowEvent(row)">保存</el-button>
              <el-button size="small" type="primary" plain @click="cancelRowEvent(row)">取消</el-button>
            </template>
            <template v-else>
              <el-button size="small" type="success" @click="$refs['AddMenu'].setPid(row.id),isShowAdd = true">子菜单</el-button>
              <el-button size="small" type="warning" @click="editRowEvent(row)">编辑</el-button>
              <el-button size="small" type="danger" @click="destroyRowEvent(row)">删除</el-button>
            </template>
          </template>
        </vxe-column>
      </vxe-table>
    </CardContainer>

    <add-menu :is-show.sync="isShowAdd" :list="tableData" ref="AddMenu" @refresh="getList"></add-menu>
  </div>
</template>

<script>
import addMenu from "./components/AddMenu.vue"
import Icon from "@/layout/components/Navbar/Icon.vue"
import SvgIcon from "@/components/SvgIcon"
import { index, save, destroy } from "@/api/menu"
import { deepCopy } from '@/utils'
export default {
  components: {
    SvgIcon,
    Icon,
    addMenu
  },
  data() {
    return {
      isShowAdd: false,

      loading: false,
      allAlign: null,
      tableData: [],
      validRules: {

      },
      form: {
        id: "",
        pid: "",
        name: "",
        url: "",
        path: "",
        api_profix: "",
        icon: "",
        visible: 1,
        sortnumber: 0
      },
    }
  },
  methods: {
    editRowEvent (row) {
      if (this.$refs['table']) {
        this.$refs['table'].setEditRow(row)
      }
    },
    cancelRowEvent (row) {
      if (this.$refs['table']) {
        this.$refs['table'].clearEdit().then(() => {
          // 还原行数据
          this.$refs['table'].revertData(row)
        })
      }
    },

    async getList () {
      this.loading = true;
      try {
        const res = await index()
        this.tableData = res;
        this.loading = false;
      } catch (err) {
        console.error(err)
        this.loading = false;
      }
    },

    async saveRowEvent (row) {
      try {
        await this.$confirm("确认保存？","提示",{
          confirmButtonText: "确认",
          cancelButtonText: "取消"
        })
        let form = deepCopy(this.form)
        for (let key in form) {
          form[key] = row[key]
        }
        this.loading = true;
        await save(form)
        await this.getList();
        this.loading = false;
      } catch (err) {
        this.loading = false;
      }
    },
    async destroyRowEvent (row) {
      try {
        await this.$confirm("确认删除？","提示",{
          confirmButtonText: "确认",
          cancelButtonText: "取消"
        })
        this.loading = true;
        if (row.id) {
          await destroy({
            id: row.id
          })
          await this.getList();
        } else {
          this.tableData.splice(this.tableData.findIndex(i => i._X_ROW_KEY === row._X_ROW_KEY),1)
        }
        this.loading = false;
      } catch (err) {
        this.loading = false;
      }
    },
  },
  computed: {
    isActiveStatus () {
      return function (row) {
        if (this.$refs['table']) {
          return  this.$refs['table'].isEditByRow(row)
        }
      }
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped lang="scss">
.icon-folder {
  width: 2em;
  height: 2em;
}
</style>
