<template>
  <div class="base-site-nav-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.nav_title"
              clearable
              :placeholder="__('请输入导航标题')"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('导航标题')"
        prop="nav_title"
        show-overflow-tooltip
        width="200"
      />
      <el-table-column
        align="center"
        :label="__('导航链接')"
        prop="nav_url"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatPosition"
        :label="__('导航位置')"
        prop="nav_position"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatBlank"
        :label="__('新窗口打开')"
        prop="nav_target_blank"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="nav_order"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="nav_enable"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatBuildin"
        :label="__('系统内置')"
        prop="nav_buildin"
        show-overflow-tooltip
        width="100"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doDelete, getList } from '@/api/base/site/nav'
  import Edit from './components/BaseSiteNavEdit'

  export default {
    name: 'BaseSiteNav',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          nav_title: "",
          pageNum: 1,
          pageSize: 20,
        },
        nav_position_option: [
          {
            value: 0,
            label: this.__('头部'),
          },
          {
            value: 1,
            label: this.__('中部'),
          },
          {
            value: 2,
            label: this.__('底部'),
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatBlank(row) {
        let navTargetBlank = row.nav_target_blank
        return navTargetBlank === 1 ? '是' : '否'
      },
      formatEnable(row) {
        let nav_enable = row.nav_enable
        return nav_enable === 1 ? '是' : '否'
      },
      formatBuildin(row) {
        let nav_buildin = row.nav_buildin
        return nav_buildin === 1 ? '是' : '否'
      },
      formatPosition(row) {
        let obj = this.nav_position_option.find(
          (item) => item.value === row.nav_position
        )
        return obj ? obj.label : ''
      },
      __,
      handleEdit(row) {
        if (row.nav_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.nav_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({ nav_id: row.nav_id })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
