<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="60%"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item
        :label="__('导航标题')"
        label-width="140px"
        prop="nav_title"
      >
        <el-input
          v-model="form.nav_title"
          clearable
          :placeholder="__('请输入导航标题')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('导航链接')" label-width="140px" prop="nav_url">
        <el-input
          v-model="form.nav_url"
          clearable
          :placeholder="__('请输入导航链接')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('导航位置')"
        label-width="140px"
        prop="nav_position"
      >
        <el-radio-group v-model="form.nav_position" size="medium">
          <el-radio
            v-for="(item, index) in nav_positionOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="__('是否以新窗口打开')"
        label-width="140px"
        prop="nav_target_blank"
        required
      >
        <el-switch
          v-model="form.nav_target_blank"
          :active-text="__('新窗口打开')"
          :active-value="1"
          :inactive-text="__('当前窗口打开')"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item
        :label="__('导航下拉内容')"
        label-width="140px"
        prop="nav_dropdown_menu"
      >
        <el-input
          v-model="form.nav_dropdown_menu"
          :autosize="{ minRows: 4, maxRows: 4 }"
          :placeholder="__('导航下拉内容导航下拉内容')"
          :style="{ width: '100%' }"
          type="textarea"
        />
      </el-form-item>
      <el-form-item :label="__('排序')" label-width="140px" prop="nav_order">
        <el-input
          v-model="form.nav_order"
          clearable
          :placeholder="__('请输入排序')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('是否启用')"
        label-width="140px"
        prop="nav_enable"
        required
      >
        <el-switch
          v-model="form.nav_enable"
          :active-text="__('启用')"
          :active-value="1"
          :inactive-text="__('停用')"
          :inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

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

  import { doEdit } from '@/api/base/site/nav'

  export default {
    name: 'BaseSiteNavEdit',
    data() {
      return {
        form: {
          nav_position: 0,
          nav_target_blank: 0,
          nav_dropdown_menu: undefined,
          nav_enable: 1,
        },
        rules: {
          nav_title: [
            {
              required: true,
              message: this.__('请输入导航标题'),
              trigger: 'blur',
            },
          ],
          nav_url: [
            {
              required: true,
              message: this.__('请输入导航链接'),
              trigger: 'blur',
            },
          ],
          nav_position: [
            {
              required: true,
              message: this.__('导航位置不能为空'),
              trigger: 'change',
            },
          ],
          nav_order: [
            {
              required: true,
              message: this.__('请输入排序'),
              trigger: 'blur',
            },
          ],
        },
        nav_positionOptions: [
          {
            label: this.__('头部显示'),
            value: 0,
          },
          {
            label: this.__('中部显示'),
            value: 1,
          },
          {
            label: this.__('底部显示'),
            value: 2,
          },
        ],
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          nav_position: 0,
          nav_target_blank: 0,
          nav_dropdown_menu: undefined,
          nav_enable: 1,
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
