<template>
  <div>
    <a-modal class="modalBox" :title="titleText" v-model="isshow" @cancel="cancel">
      <a-form :form="form" @submit="handleSubmit">
        <!-- 上级菜单 -->
        <a-form-item label="上级菜单" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <div >
            <span >{{ parentTitle }}</span>
          </div>

        </a-form-item>
        <!-- 类型 -->
        <a-form-item label="类型" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-select
            @change="selectChange"
            :disabled="disabled"
            v-decorator="[
              'formData.type',
              {
                initialValue: formData.type,
                rules: [{ required: true, message: '请选择状态!' }] },
            ]"
            style="width: 100%"
            placeholder="请选择菜单类型"
          >
            <a-select-option :value="0">页面类型</a-select-option>
            <a-select-option :value="1">按钮类型</a-select-option>
          </a-select>
        </a-form-item>

        <!-- 名称 -->
        <a-form-item label="名称" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-input
            placeholder="请输入名称(最多30个字符)"
            :maxLength="30"
            v-decorator="['formData.name', {
              initialValue: formData.name,
              getValueFromEvent: $filterEmpty,
              rules: [{ required: true, message: '请输入名称!' }] }]"
          />
        </a-form-item>
        <!-- 路由英文名 -->
        <!-- <a-form-item v-if="!formData.type" label="路由英文名" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-input
            placeholder="请输入路由英文名"
            v-decorator="['formData.routeName', {
              initialValue: formData.routeName,
              rules: [{ required: true, message: '请输入路由英文名!' }] }]"
          />
        </a-form-item> -->
        <!-- 路径 -->
        <!-- <a-form-item v-if="!formData.type" label="路径" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
             <a-input placeholder="请输入路径"
                      v-decorator="['formData.routePath', {
                        initialValue: formData.routePath,
                        rules: [{ required: true, message: '请输入路径!' }] }]"
             />
           </a-form-item> -->
        <!-- 请求路径 -->
        <!-- <a-form-item v-if="formData.type==1" label="请求路径" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
             <a-input placeholder="请输入请求路径"
                      v-decorator="['formData.urlPath', {
                        initialValue: formData.urlPath,
                        rules: [{ required: true, message: '请输入请求路径!' }] }]"
             />
           </a-form-item> -->
        <!-- 前端权限编码 -->
        <a-form-item label="前端权限编码" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-input
            placeholder="请输入前端权限编码"
            v-decorator="[
              'formData.code',
              {
                initialValue: formData.code,
                rules: [{ required: true, message: '请输入前端权限编码!' }] },
            ]"
          />

        </a-form-item>
        <!-- 后台权限编码 -->
        <a-form-item label="后台权限编码" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-textarea
            placeholder="请输入后台权限编码"
            :auto-size="{ minRows: 2, maxRows: 6 }"
            v-decorator="[
              'formData.permission',
              {initialValue: formData.permission},
            ]"
          />

        </a-form-item>

        <!-- 图标 -->
        <!-- <a-form-item v-if="!formData.type" label="图标" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-input
            placeholder="请输入图标名称"
            v-decorator="['formData.icon', {
              initialValue: formData.icon,
              rules: [] }]"
          />
        </a-form-item> -->
        <!-- 前端组件 -->
        <!-- <a-form-item v-if="!formData.type" label="前端组件" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-input
            placeholder="请输入前端组件路径"
            v-decorator="['formData.componentPath', {
              initialValue: formData.componentPath,
              rules: [{ required: true, message: '请输入前端组件路径!' }] }]"
          />
        </a-form-item> -->

        <!-- 第三方链接 -->
        <!-- <a-form-item v-if="!formData.type" label="第三方链接" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-input
            placeholder="请输入第三方链接地址"
            v-decorator="['formData.urlPath', {
              initialValue: formData.urlPath,
              rules: [] }]"
          />
        </a-form-item> -->

        <!-- 排序值 -->
        <a-form-item label="排序值" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-input-number
            style="width: 100%;"
            type="number"
            placeholder="请输入排序值(0~999999)"
            v-decorator="['formData.sort', {
              initialValue: formData.sort,
              rules: [] }]"
            :max="99999999"
            :min="0"
          />
        </a-form-item>
        <!-- 连锁可用 -->
        <a-form-item v-if="parentType=='store'" label="连锁可用" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-radio-group
            name="radioGroup"
            v-decorator="[
              'formData.chainOrgUse',
              {
                initialValue: formData.chainOrgUse,
                rules: [{ required: true, message: '请选择!' }] },
            ]"
          >
            <a-radio :value="1">是</a-radio>
            <a-radio :value="0">否</a-radio>
          </a-radio-group>
        </a-form-item>
        <!-- 单店可用 -->
        <a-form-item v-if="parentType=='store'" label="单店可用" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-radio-group
            name="radioGroup"
            v-decorator="[
              'formData.singleOrgUse',
              {
                initialValue: formData.singleOrgUse,
                rules: [{ required: true, message: '请选择!' }] },
            ]"
          >
            <a-radio :value="1">是</a-radio>
            <a-radio :value="0">否</a-radio>
          </a-radio-group>
        </a-form-item>
        <!-- 状态 -->
        <a-form-item label="启用" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
          <a-radio-group
            name="radioGroup"
            v-decorator="[
              'formData.status',
              {
                initialValue: formData.status,
                rules: [{ required: true, message: '请选择状态!' }] },
            ]"
          >
            <a-radio :value="1">是</a-radio>
            <a-radio :value="0">否</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
          <a-button type="primary" @click="handleSubmit()">
            保存
          </a-button>
          <a-button :style="{ marginLeft: '8px' }" @click="handleCancel()">
            取消
          </a-button>
        </a-form-item>
      </a-form>

    </a-modal>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { saveYyMenu, saveMdMenu } from '@/api/menu.js'
export default {
  name: 'ChildModal',
  components: {
    STable, VSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    parentType: {
      type: String,
      default: ''
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      isshow: this.visible,
      titleText: '',
      disabled: false,
      parentTitle: '',
      formLayout: 'horizontal',
      form: this.$form.createForm(this, { name: 'childModal' }),
      formData: {
        parentId: '',
        name: '',
        routePath: '',
        icon: '',
        routeName: '',
        code: '',
        permission: '',
        componentPath: '',
        urlPath: '',
        sort: '',
        type: undefined,
        chainOrgUse: 1,
        singleOrgUse: 1,
        status: 1 // 活动状态
      }
    }
  },
  methods: {
    cancel (e) {
      this.clear()
      this.$emit('close')
    },

    selectChange (val) {
      this.formData.type = val
      console.log(val)
    },
    // 保存提交
    handleSubmit () {
      const _this = this
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log(values.formData, ' formData.type')
          values.formData.permission = values.formData.permission.replace(/\//g, '_')
          const saveRequest = this.parentType == 'store' ? saveMdMenu : saveYyMenu
          saveRequest(Object.assign(this.formData, values.formData)).then(res => {
            console.log(res, 'res--save')
            if (res.status + '' === '200') {
              this.$message.success(res.message ? res.message : '保存成功')
              this.$emit('refresh')
              setTimeout(function () {
                _this.cancel()
              }, 300)
            } else {
              // this.$message.warning(res.message)
            }
          })
        }
      })
    },
    // 取消
    handleCancel () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定取消吗?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          _this.clear()
          _this.cancel()
        }
      })
    },
    clear () {
      this.form.resetFields()
      this.parentTitle = ''
      this.formData.parentId = ''
      this.formData.name = ''
      this.formData.routePath = ''
      this.formData.icon = ''
      this.formData.routeName = ''
      this.formData.componentPath = ''
      this.formData.urlPath = ''
      this.formData.sort = ''
      this.formData.type = ''
      this.formData.status = 1
      this.formData.chainOrgUse = 1
      this.formData.singleOrgUse = 1
      this.formData.code = ''
      this.formData.permission = ''
    }

  },
  mounted () {
  },
  beforeCreate () {
    this.form = this.$form.createForm(this, { name: 'childModal' })
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (newValue) {
        if (this.data.id) { // 添加子菜单
          this.titleText = '添加子菜单'
          this.disabled = false
          console.log(this.data, 'this.data')
          this.formData.parentId = this.data.id
          this.parentTitle = this.data.name
        } else { // 添加顶菜单
          this.titleText = '添加顶菜单'
          this.formData.type = 0
          this.disabled = true
        }
      } else {
        this.cancel()
      }
    }
  }
}
</script>

<style >
  .modalBox{
  }
  .ant-modal-footer {
    display: none;
  }
</style>
