<!--
 * @Author: 曹捷
 * @Date: 2019-08-19 15:24:34
 * @LastEditors: 曹捷
 * @LastEditTime: 2021-05-17 15:14:26
 * @Description: file content
 -->
<template>
  <a-modal class="obit-modal-default" v-if="showModal" :maskClosable="false" :title="showTitle" v-model:visible="showModal"
    @cancel="closeDialog" @ok="saveInfo" cancelText="取消" okText="确认" width="40%">
    <a-form :inline="true" :model="formData" :rules="rules" ref="formRef" :label-col="{span: 4}"
      :wrapper-col="{span:18}">
      <a-form-item label="应用名称" name="appName">
        <a-input :maxlength="20" placeholder="请输入应用名称" show-word-limit v-model:value="formData.appName"></a-input>
      </a-form-item>
      <a-form-item label="应用编码" name="appCode">
        <a-input :maxlength="20" placeholder="请输入应用编码" :disabled="addType ==='edit'" show-word-limit
          v-model:value="formData.appCode"></a-input>
      </a-form-item>
      <a-form-item label="应用图标" name="appIcon">
        <a-input placeholder="请输入应用图标" readonly v-model:value="formData.appIcon">
          <template #prefix>
            <i class="iconfont " :class="'icon-'+formData.appIcon"></i>
          </template>
          <template #suffix>
            <obitIconShow v-model:icon="formData.appIcon"></obitIconShow>
          </template>
        </a-input>
      </a-form-item>
      <a-form-item label="应用类型" name="appType">
        <obit-dict dictCode="AppType" placeholder="请选择应用类型" v-model:value="formData.appType"></obit-dict>
      </a-form-item>
      <a-form-item label="登录路径" name="linkUrl">
        <a-input placeholder="应用单点登录路径" v-model:value="formData.linkUrl"></a-input>
      </a-form-item>
      <a-form-item label="应用描述" name="appDesc">
        <a-textarea :maxlength="100" placeholder="请输入应用描述" show-word-limit v-model:value="formData.appDesc">
        </a-textarea>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { computed, inject, reactive, ref, toRefs, watch } from 'vue'
import obitIconShow from '../../../components/obit-icon-show/obit-icon-show.vue'
export default {
  components: { obitIconShow },
  // inheritAttrs: false,
  props: {
    open: {
      type: Boolean
    },
    addType: {
      type: String,
    },
    detailsInfo: {
      type: Object,
    },
  },
  setup (props, context) {
    let http = inject('http')
    let util = inject('util')
    let _data = reactive({
      showModal: props.open,
      formData: {},
      // form组件  ref绑定  需要内部定义才可以调用  没有$refs
      formRef: null,
      rules: {
        appName: [
          { required: true, message: '请输入应用名称', trigger: 'blur' },
        ],
        appCode: [
          { required: true, message: '请输入应用编码', trigger: 'blur' },
        ],
        appIcon: [
          { required: true, message: '请输入应用图标', trigger: 'blur' },
        ],
        appType: [
          { required: true, message: '请选择应用类型', trigger: 'change' },
        ],
        linkUrl: [
          { required: true, message: '请输入应用单点登录路径', trigger: 'blur' },
        ],
      },
    })
    let _computed = {
      showTitle: computed(() => props.addType === 'add' ? '新增应用' : '修改应用')
    }
    let _methods = {
      saveInfo () {
        _data.formRef.validate().then(() => {
          let param = util.util.cloneObj(_data.formData)
          if (param.id) {
            http.AddSystemAppInfo(param).then((res) => {
              context.emit('reload')
              _methods.closeDialog()
            })
          } else {
            http.EditSystemAppInfo(param).then((res) => {
              context.emit('reload')
              _methods.closeDialog()
            })
          }
        })
      },
      closeDialog () {
        util.util.cleanObj(_data.formData)
        context.emit('update:open', false)
      },
    }
    watch(() => props.open, (value) => {
      if (value && props.addType === 'edit') {
        _data.formData = util.util.cloneObj(props.detailsInfo)
      } else {
        util.util.cleanObj(_data.formData)
      }
    })
    return {
      ..._computed,
      ...toRefs(_data),
      ..._methods
    }
  },
}
</script>
<style lang="less">
</style>