<template>
  <el-dialog v-dialogDrag width="40%" class="moveMen" :title="title" :visible.sync="addStatus"
    :close-on-click-modal="false" :before-close="cancel" top="30px">
    <el-form :model="form" ref="ruleForm" label-width="100px" :rules='rules' style="padding:0 2px">
      <el-row :gutter="20">
        <el-col :span="19">
          <el-form-item label="菜单名称：" prop="name">
            <el-input size="small" v-model="form.name" clearable placeholder="请输入菜单名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="19">
          <el-form-item label="url地址：" prop="url">
            <el-input size="small" v-model="form.url" clearable placeholder="请输入url地址"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="19">
          <el-form-item label="排序：" prop="order">
            <el-input size="small" v-model="form.order" clearable placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button size="small" type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button size="small" type="info" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
  import {
    message,
    dateFormats,
  } from '@/utils/common'
  export default {
    props: {
      addStatus: {
        type: Boolean,
        default: () => false,
      },
      title: {
        type: String,
        default: () => '新增',
      },
      form: {
        type: Object,
        default: () => {},
      },

    },
    data() {
      return {
        rules: {
          name: [{
            required: true,
            message: '请输入菜单名称',
            trigger: 'blur'
          }],
          url: [{
            required: true,
            message: '请输入url地址',
            trigger: 'blur'
          }],
          order: [{
            required: true,
            message: '请输入',
            trigger: 'blur'
          }],
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.title === '新增') {
              this.$store.dispatch('mune/saveTree', this.form).then(res => {
                this.$emit('getData')
                this.cancel()
              })
            } else {
              this.$store.dispatch('mune/updateTree', this.form).then(res => {
                this.$emit('getData')
                this.cancel()
              })
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      cancel() {
        this.$refs['ruleForm'].resetFields();
        this.$emit('cancel')
      }
    }
  }

</script>
<style lang='scss' scoped>
  .el-form-item {
    margin-bottom: 22px;
  }

  .ren {
    text-align: right;
    width: 100px;
    font-size: 14px;
    color: #606266;
    line-height: 40px;
    padding: 0 12px 0 0;
    font-weight: 700;

  }

</style>
