<template>
  <div class="pay-channel-edit-form">
    <el-dialog
      :title="title"
      :visible="true"
      :modal-append-to-body="true"
      :append-to-body="true"
      :before-close="handleClose"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      width="550px"
      top="50px"
    >
      <el-form
        ref="form"
        v-loading="submitLoading"
        style="width: 400px; margin-left:50px;"
        :model="form"
        :rules="localizedRules"
        label-width="60px"
      >

        <el-form-item label="Code" prop="factoryCode">
          <el-input v-model.trim="form.factoryCode" :disabled="isEdit" type="text" :placeholder="$t('pages.payFactory.placeholder.enterFactoryCode')" />
        </el-form-item>
        <el-form-item :label="$t('pages.payFactory.form.name')" prop="factoryName">
          <el-input v-model.trim="form.factoryName" type="text" :placeholder="$t('pages.payFactory.placeholder.enterFactoryName')" />
        </el-form-item>
        <el-form-item :label="$t('pages.payFactory.form.icon')" prop="factoryIcon">
          <el-upload
            :disabled="iconUploadLoading"
            :file-list="iconFileList"
            :class="{'upload-but-hide': formFactoryIconNotEmpty}"
            action=""
            list-type="picture-card"
            :http-request="iconFileUpload"
            :show-file-list="formFactoryIconNotEmpty"
            :on-remove="handleIconFileRemove"
            accept="image/*"
          >
            <i slot="default" v-loading="iconUploadLoading" class="el-icon-plus" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">{{ $t('pages.payFactory.action.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm()">{{ $t('pages.payFactory.action.save') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone, getElementUiUploadFile } from '@/utils'
import { payChannelGroups } from '@/constant/type'
import { addPayFactory, updatePayFactory } from '@/api/sys-pay'
export default {
  name: 'PayApplFactoryEditForm',
  props: {
    row: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      payChannelGroups,
      form: {
        id: '',
        factoryCode: '',
        factoryName: '',
        factoryIcon: ''
      },
      submitLoading: false,
      iconUploadLoading: false,
      iconFileList: []
    }
  },
  computed: {
    isEdit() {
      return !!(this.form && this.form.id)
    },
    title() {
      return this.form && this.form.id ? this.$t('pages.payFactory.dialog.editTitle') : this.$t('pages.payFactory.dialog.addTitle')
    },
    formFactoryIconNotEmpty() {
      return !!(this.form.factoryIcon && this.form.factoryIcon.length > 0)
    },
    localizedRules() {
      const commonRequiredParams = [{ required: true, message: this.$t('pages.payFactory.validation.requiredField'), trigger: 'blur' }]
      return {
        factoryCode: commonRequiredParams,
        factoryName: commonRequiredParams,
        factoryIcon: commonRequiredParams
      }
    }
  },
  watch: {
    row: {
      handler(newVal) {
        if (newVal && newVal.id) {
          this.form = deepClone(newVal)
          this.iconFileList = getElementUiUploadFile(newVal.factoryIcon)
        }
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    submitForm() {
      const that = this
      that.$refs.form.validate(valid => {
        if (!valid) {
          console.error('submit error!')
          return
        }
        that.submitLoading = true
        if (that.isEdit) {
          updatePayFactory(that.form).then(res => {
            that.submitLoading = false
            that.handleClose()
            that.$emit('success', { event: 'UPDATE', form: that.form })
          }).catch(er => {
            that.submitLoading = false
            console.error(er)
            that.$emit('fail', { event: 'UPDATE', form: that.form })
          })
          return
        }

        addPayFactory(that.form).then(res => {
          that.submitLoading = false
          that.handleClose()
          that.$emit('success', { event: 'ADD', form: that.form })
        }).catch(er => {
          that.submitLoading = false
          console.error(er)
          that.$emit('fail', { event: 'ADD', form: that.form })
        })
      })
    },
    handleIconFileRemove(file, fileList) {
      this.form.factoryIcon = ''
      this.iconUploadLoading = false
    },
    iconFileUpload(file) {
      const that = this
      that.iconUploadLoading = true
      that.$simpleUploadFlie(file, that.$application.fileBucket.back).then(res => {
        that.iconUploadLoading = false
        that.form.factoryIcon = that.$getAccessImgUrl(res.name)
      }).catch(er => {
        that.iconUploadLoading = false
      })
    }
  }
}
</script>
