<template>
  <div class="" style="padding: 16px;height: 100vh;overflow-y: auto;margin-bottom: 50px">
    <!--    <YMTableSearch :loading="loading" :search-form="searchForm" @onValueChange="onValueChange" @submit="onSubmit" />-->
    <div>
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <el-table
      align="left"
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        :align="item.align||'center'"
        :prop="item.prop"
        :label="item.label"
        v-for="item in columns" :key="item.id"
      >
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button @click="edit(row)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 自定义弹窗 -->
    <Dialog ref="dialog" :loading="loading" :dialogActions="{width:'560px',title:'菜单'}" @confirm="confirm">
      <template slot="content">
        <el-form label-width="80px" :inline="true">
          <el-form-item label="上级菜单" :rules="[{ required: false, message: '请选择上级菜单' }]">
            <el-cascader
              style="width: 300px"
              v-model="form.parent_id"
              placeholder="请选择上级菜单"
              :options="mineMenuList"
              :props="{ checkStrictly: true,value:'id',label:'title' }"
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item label="菜单名称" :rules="[{ required: true, message: '请输入菜单名称' }]">
            <el-input v-model="form.title" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="路由" :rules="[{ required: true, message: '请输入路由' }]">
            <el-input v-model="form.uri" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="类型" :rules="[{ required: true, message: '请选择类型' }]">
            <el-select style="width: 300px" v-model="form.type" placeholder="请选择类型">
              <el-option label="菜单" :value="1"></el-option>
              <el-option label="按钮" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" :rules="[{ required: true, message: '请选择状态' }]">
            <el-select style="width: 300px" v-model="form.status" placeholder="请选择状态">
              <el-option label="显示" :value="1"></el-option>
              <el-option label="隐藏" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="图标">
            <el-input v-model="form.icon" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="form.sort" autocomplete="off" style="width: 300px"></el-input>
          </el-form-item>
        </el-form>
      </template>
    </Dialog>
  </div>
</template>
<script>
import Dialog from '@/components/Dialog/index.vue'
import { mapGetters } from 'vuex'
import { menuall, menusubmit } from '@/api/data'

export default {
  components: {
    Dialog
  },
  data() {
    return {
      loading: false,
      visibleBasicDrawer: false,
      renderContent: '',
      searchForm: [],
      mineMenuList: [],
      tableData: [],
      form: {},
      columns: [
        {
          label: '菜单名称',
          prop: 'title',
          align: 'left'
        },
        {
          label: '路由',
          prop: 'uri'
        },
        {
          label: '类型',
          prop: 'type_name',
          method: (row) => row.type === 1 ? '菜单' : '按钮'
        },
        {
          label: '状态',
          prop: 'status_name',
          method: (row) => row.status === 1 ? '显示' : '隐藏'
        },
        {
          label: '排序',
          html: true,
          prop: 'sort'
        }
      ],
    }
  },
  computed: {
    ...mapGetters(['rolesTree'])
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      menuall()
        .then((res) => {
          this.tableData = this.processTreeData(res.data)
          this.$set(this, 'mineMenuList', res.data)
          // this.mineMenuList.unshift({
          //   title: '顶级菜单',
          //   parent_id: 0
          // })
        })
    },
    processTreeData(nodes) {
      if (!nodes || !Array.isArray(nodes)) return []
      return nodes.map(node => {
        const processedNode = { ...node }
        processedNode.status_name = node.status === 1 ? '显示' : '隐藏'
        processedNode.type_name = node.type === 1 ? '菜单' : '按钮'

        if (node.children && node.children.length > 0) {
          processedNode.children = this.processTreeData(node.children)
        }

        return processedNode
      })
    },
    confirm() {
      this.loading = true
      const parent_id = Array.isArray(this.form.parent_id)
        ? (this.form.parent_id.length ? this.form.parent_id[this.form.parent_id.length - 1] : null)
        : (this.form.parent_id == null ? null : this.form.parent_id)
      menusubmit({
        ...this.form,
        parent_id: parent_id
      })
        .then((res) => {
          this.$message.success('保存成功')
          this.$refs.dialog.close()
          this.getList()
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    add() {
      this.form = {
        parent_id: [0]
      }
      this.$refs.dialog.open()
    },
    edit(row) {
      const data = JSON.parse(JSON.stringify(row))
      this.form = {
        icon: data.icon,
        id: data.id,
        parent_id: data.parent_id,
        sort: data.sort,
        status: data.status,
        title: data.title,
        type: data.type,
        uri: data.uri
      }
      this.$refs.dialog.open()
    },
    onValueChange(val) {

    },
    onSubmit(params) {
      this.$refs.table.getList(params)
    }
  }
}
</script>
