<template>
  <ai-detail>
    <template slot="title">
      <ai-title :title="params.id ? '编辑模块' : '添加模块'" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <el-form class="ai-form" :model="form" label-width="120px" ref="form">
            <el-form-item label="模块名称" style="width: 100%;" prop="moduleName" :rules="[{required: true, message: '请输入模块名称', trigger: 'blur'}]">
              <el-input type="input" style="width: 300px" size="small" v-model="form.moduleName" clearable placeholder="请输入模块名称" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item prop="menuId" label="菜单名称" :rules="[{required: true, message: '请选择菜单', trigger: 'change'}]">
               <el-select size="small" style="width: 300px" v-model="form.menuId" placeholder="请选择菜单">
                  <el-option
                    v-for="item in memuList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-form>
        </template>
      </ai-card>
    </template>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="confirm">提交</el-button>
    </template>
  </ai-detail>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'Add',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },

    data () {
      return {
        info: {},
        form: {
          moduleName: '',
          menuId: ''
        },
        id: '',
        memuList: []
      }
    },

    computed: {
      ...mapState(['user'])
    },

    created () {
      this.getMenuList()
      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/appcontentmoduleinfo/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.form = res.data
          }
        })
      },

      getMenuList () {
        this.instance.post(`/admin/menu/menuTree?containPermission=0`).then(res => {
          if (res.code === 0) {
            this.memuList = res.data
          }
        })
      },

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appcontentmoduleinfo/addOrUpdate`, {
              ...this.form,
              menuName: this.memuList.filter(v => v.id === this.form.menuId)[0].name,
              id: this.params.id || ''
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('提交成功')
                setTimeout(() => {
                  this.cancel(true)
                }, 600)
              }
            })
          }
        })
      },

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'list',
          isRefresh: !!isRefresh
        })
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
