<template>
  <div class="app-content" style="height:420px;overflow:auto">
    <a-spin :spinning="loading" style="width: 100%;height: 100%;">
      <a-form-model ref="form" :model="formData" :rules="rules">
        <a-form-model-item label="菜单Id">
          <a-input v-model="formData.id" :disabled="disabled" style="width:180px;" />
        </a-form-model-item>
        <a-form-model-item ref="menuName" label="菜单名称" prop="menuName">
          <a-input v-model="formData.menuName" style="width:180px;" />
        </a-form-model-item>
        <a-form-model-item ref="actionUrl" label="地址" prop="actionurl">
          <a-input v-model="formData.actionUrl" style="width:180px;" />
        </a-form-model-item>
        <a-form-model-item ref="icon" label="icon图标" prop="icon">
          <a-input v-model="formData.icon" style="width:180px;" />
        </a-form-model-item>
        <a-form-model-item ref="windowPara" label="窗口参数" prop="windowPara">
          <a-input v-model="formData.windowPara" style="width:180px;" />
        </a-form-model-item>
        <a-form-model-item ref="showIndex" label="排序" prop="showindex">
          <a-input-number v-model="formData.showIndex" :min="10" :max="9999" @change="onSNumberChange" style="width:180px;" />
        </a-form-model-item>
        <a-form-model-item ref="parentId" label="父Id" prop="parentId">
          <a-input-number v-model="formData.parentId" :min="10" :max="9999" @change="onPNumberChange" style="width:180px;" />
        </a-form-model-item>
        <a-form-model-item ref="keepAlive" label="是否缓存" prop="keepAlive">
          <a-switch default-checked :checked="Boolean(formData.keepAlive)" checked-children="是" un-checked-children="否" @change="switchChange" />
        </a-form-model-item>
        <a-row>
          <a-col :span="24" style="text-align: center">
            <a-button type="primary" size="small" @click="submit">提交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </div>
</template>

<script>

import { isEmptyParams } from '@/views/utils/common'

export default {
  name: 'menuEdit',
  props: {
    value: {
      type: String,
      default: () => {
        return null
      },
    },
  },
  data () {
    return {
      formData: {
        id: null, menuName: null, actionUrl: null, icon: '', windowPara: null, showIndex: null, parentId: null, keepAlive: 0
      },
      rules: {
        id: { required: true, message: '请填写菜单id', trigger: 'blur' },
        menuName: { required: true, message: '请填写菜单名称', trigger: 'blur' },
        actionUrl: { required: true, message: '请填写访问url', trigger: 'blur' },
        icon: { required: false, message: '请填写icon图标' },
        windowPara: { required: true, message: '请填写窗口参数', trigger: 'blur' },
        showIndex: { required: true, message: '请填写排序号', trigger: 'blur' },
        keepAlive: { required: true, message: '请选择是否缓存', trigger: 'blur' }
      },
      disabled: false,
      loading: false
    }
  },
  computed: {

  },
  created () {
    if (!!this.value) {
      this.getSystemMenuById()
      this.disabled = true
    }
  },
  methods: {
    getSystemMenuById () {
      this.loading = true
      let par = { id: this.value }
      this.$api.systemManage.getSystemMenuById(par).then(({ data = {} }) => {
        if (data) {
          this.formData = data
        }
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    onSNumberChange (value) {
      this.formData.showIndex = value
    },
    onPNumberChange (value) {
      this.formData.parentId = value
    },
    switchChange (checked) {
      if (checked)
        this.formData.keepAlive = 1
      else
        this.formData.keepAlive = 0
    },
    submit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          if (!!!this.value) {
            let pars = isEmptyParams(this.formData)
            let par = { ...pars }
            this.$api.systemManage.insertSystemMenu(par).then(({ data = {} }) => {
              if (data) {
                this.$message.success("添加成功！")
                this.$emit('close')
              }
              this.loading = false
            }).catch(() => { this.loading = false })
          } else {
            let pars = isEmptyParams(this.formData)
            let par = { ...pars }
            this.$api.systemManage.updateSystemMenu(par).then(({ data = {} }) => {
              if (data) {
                this.$message.success("修改成功！")
                this.$emit('close')
              }
              this.loading = false
            }).catch(() => { this.loading = false })
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.app-content {
  .ant-form-item {
    margin-bottom: 6px;
  }
  ::v-deep .ant-row {
    .ant-col {
      display: inline-block;
    }
    .ant-form-item-label {
      width: 90px;
    }
    .ant-form-item-control-wrapper {
      width: calc(100% - 90px);
    }
    .ant-form-explain {
      margin-left: 5px;
      display: inline-block;
    }
  }
}
</style>

