<template>
  <div class="app-container">
    <el-dialog
      width="80%"
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="visible"
      top="4%"
    >
      <el-form ref="form" v-loading="loading" :model="formItem" label-width="150px">
        <ec-line :title="'应用信息'"></ec-line>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.app.name')"
              prop="appName"
              :rules="[
                { required: true, message: $t('ec.app.name') + $t('ec.table.emptyError'), trigger:'change' },
                { validator: validateAppName, trigger: 'blur' }

              ]"
            >
              <el-input v-model="formItem.appName" type="text" :placeholder="$t('ec.app.name')" maxlength="128" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.app.type')"
              prop="typeApp"
              :rules="[
                // { required: true, message: $t('ec.app.type') + $t('ec.table.emptyError'), trigger:'change' }
              ]"
            >
              <ec-select v-model="formItem.typeApp" dict-type="typeApp" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item-->
<!--              :label="$t('ec.app.authType')"-->
<!--              prop="typeAuth"-->
<!--              :rules="[-->
<!--                { required: true, message: $t('ec.app.authType') + $t('ec.table.emptyError'), trigger:'blur' }-->
<!--              ]"-->
<!--            >-->
<!--              <ec-radio-group v-model="formItem.typeAuth" dict-type="authType" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.app.ipFlag')"
              prop="flagIp"
              :rules="[
                // { required: true, message: $t('ec.app.ipFlag') + $t('ec.table.emptyError'), trigger:'blur' }
              ]"
            >
              <ec-radio-group v-model="formItem.flagIp" dict-type="enableState" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item-->
<!--              :label="$t('ec.app.lLogFlag')"-->
<!--              prop="flagLLog"-->
<!--              :rules="[-->
<!--                { required: true, message: $t('ec.app.lLogFlag') + $t('ec.table.emptyError'), trigger:'blur' }-->
<!--              ]"-->
<!--            >-->
<!--              <ec-radio-group v-model="formItem.flagLLog" dict-type="enableState" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item-->
<!--              :label="$t('ec.app.logOptFlag')"-->
<!--              prop="flagLogOpt"-->
<!--              :rules="[-->
<!--                { required: true, message: $t('ec.app.logOptFlag') + $t('ec.table.emptyError'), trigger:'blur' }-->
<!--              ]"-->
<!--            >-->
<!--              <ec-radio-group v-model="formItem.flagLogOpt" dict-type="enableState" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item-->
<!--              :label="$t('ec.table.enableStatus')"-->
<!--              prop="status"-->
<!--              :rules="[-->
<!--                { required: true, message: $t('ec.table.enableStatus') + $t('ec.table.emptyError'), trigger:'blur' }-->
<!--              ]"-->
<!--            >-->
<!--              <ec-radio-group v-model="formItem.status" dict-type="enableState" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.app.externalFlag')"
              prop="fromExternal"
              :rules="[
                // { required: true, message: $t('ec.app.externalFlag') + $t('ec.table.emptyError'), trigger:'blur' }
              ]"
            >
              <ec-radio-group v-model="formItem.fromExternal" dict-type="enableState" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.table.sort')"
              prop="sortNum"
              :rules="[
              ]"
            >
              <el-input-number v-model="formItem.sortNum" :label="$t('ec.table.sort')" maxlength="11" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item-->
<!--              :label="$t('ec.app.target')"-->
<!--              prop="target"-->
<!--              :rules="[-->
<!--              ]"-->
<!--            >-->
<!--              <el-input v-model="formItem.target" type="text" :placeholder="$t('ec.app.target')" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item-->
<!--              :label="$t('ec.table.version')"-->
<!--              prop="rsvn"-->
<!--              :rules="[-->
<!--              ]"-->
<!--            >-->
<!--              <el-input v-model="formItem.rsvn" type="text" :placeholder="$t('ec.table.version')" maxlength="64" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.app.url')"
              prop="url"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.url" type="text" :placeholder="$t('ec.app.url')" maxlength="64" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              prop="icon"
              :rules="[
              ]"
            >
              <el-switch v-model="httpIcon" active-text="HTTP图标" inactive-text="系统图标"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="!httpIcon">
            <el-form-item
              :label="$t('ec.app.icon')"
              prop="icon"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.icon" type="text" :placeholder="$t('ec.app.icon')" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="httpIcon">
            <el-form-item
              label="HTTP图标"
              prop="icon"
              :rules="[
              ]"
            >
              <ec-file-modal v-model="formItem.icon" list-type="picture" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.table.remarks')"
              prop="remark"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.remark" type="textarea" :autosize="{minRows: 3, maxRows: 5}" maxlength="255" />
            </el-form-item>
          </el-col>
        </el-row>
        <ec-line :title="'项目信息'"></ec-line>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.app.projectCode')"
              prop="projectCode"
              :rules="[
                { required: true, message: $t('ec.app.projectCode') + $t('ec.table.emptyError'), trigger:'blur' }
              ]"
            >
              <el-input :disabled="!enableEdit" v-model="formItem.projectCode" type="text" :placeholder="$t('ec.app.projectCode')" maxlength="128" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.app.projectName')"
              prop="projectName"
              :rules="[
                { required: true, message: $t('ec.app.projectName') + $t('ec.table.emptyError'), trigger:'blur' }
              ]"
            >
              <el-input :disabled="!enableEdit" v-model="formItem.projectName" type="text" :placeholder="$t('ec.app.projectName')" maxlength="128" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.app.proposer')"
              prop="proposer"
              :rules="[
                { required: true, message: $t('ec.app.proposer') + $t('ec.table.emptyError'), trigger:'blur' }
              ]"
            >
              <el-input :disabled="!enableEdit" v-model="formItem.proposer" type="text" :placeholder="$t('ec.app.proposer')" maxlength="64" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.app.proposerPhone')"
              prop="proposerPhone"
              :rules="[
                { required: true, message: $t('ec.app.proposerPhone') + $t('ec.table.emptyError'), trigger:'blur' }
              ]"
            >
              <el-input :disabled="!enableEdit" v-model="formItem.proposerPhone" type="text" :placeholder="$t('ec.app.proposerPhone')" maxlength="64" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{ $t('ec.table.cancel') }}</el-button>
        <el-button v-if="method !== 'view'" type="primary" :loading="saveLoading" @click="save">{{ $t('ec.table.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import mixin from 'ecip-web/utils/common-mixin'
import { getById, save, update, checkAppName } from 'ecip-web/api/vue/app/sysrRegisterAppApi'

export default {
  name: 'VueAppSysrRegisterAppForm',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      visible: false,
      method: '',
      title: '',
      appId: '',
      loading: false, // 页面加载遮罩
      saveLoading: false, //  保存加载
      httpIcon: false,
      formItem: {
        appName: '',
        typeApp: '1',
        typeAuth: '2',
        flagIp: '1',
        flagLLog: '1',
        flagLogOpt: '1',
        status: '1',
        fromExternal: '0',
        target: '',
        remark: '',
        sortNum: '',
        rsvn: '',
        url: '',
        icon: '',
        projectCode: '',
        projectName: '',
        proposer: '',
        proposerPhone: '',
        auditBy: '',
        auditRemarks: '',
        auditStatus: 'pending'
      },
      enableEdit: true
    }
  },
  methods: {
    init(method, id) {
      this.method = method
      this.appId = id
      if (method === 'add') {
        this.title = this.$t('ec.table.add') + this.$t('ec.app.management')
      } else if (method === 'edit') {
        this.title = this.$t('ec.table.edit') + this.$t('ec.app.management')
      } else if (method === 'view') {
        this.title = this.$t('ec.table.view') + this.$t('ec.app.management')
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        Object.assign(this.$data.formItem, this.$options.data().formItem)
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          getById(id).then(res => {
            Object.assign(this.formItem, res.data)
            this.httpIcon = !!(this.formItem.icon && this.formItem.icon.toLowerCase().startsWith('http'))
            // this.formItem = res.data
            // this.inputForm = this.recover(this.inputForm, res.data)
            this.enableEdit = true
            if (res.data.auditStatus === 'agree') {
              this.enableEdit = false
            }
            this.loading = false
          })
        }
      })
    },
    async validateAppName(rule, value, callback) {
      const { data } = await checkAppName(value, this.appId || '')
      if (data.valid) {
        callback()
      } else {
        callback(new Error(this.$t('ec.table.exist')))
      }
    },
    save() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.saveLoading = true
          try {
            if (this.method === 'edit') {
              await update(this.formItem.appId, this.formItem)
            } else {
              await save(this.formItem)
            }
            this.visible = false
            this.$notify.success(this.$t('ec.message.saveSuccess'))
            this.$emit('refreshDataList')
          } finally {
            this.saveLoading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
