<template>
  <el-dialog :visible.sync="visible"
             :title="'修改定制功能'"
             :close-on-click-modal="false"
             :before-close="handleClose"
             :close-on-press-escape="false">
    <el-form :inline="true"
             :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             :label-width="'120px'">
      <el-form-item label="功能名称"
                    prop="functionName">
        <el-input class="item_width_1"
                  v-model="dataForm.functionName"
                  placeholder="功能名称"></el-input>
      </el-form-item>

      <el-form-item prop="iconLargeImg"
                    label="大图标">
        <el-upload class="item_width_1 avatar-uploader"
                   :action="uploadUlr"
                   :show-file-list="false"
                   :on-success="function (res, file) { return handleImgSuccess(res, file, 'large')}"
                   :before-upload="beforeImgUpload">
          <img v-if="dataForm.iconLargeImg"
               :src="dataForm.iconLargeImg"
               style="width:70px;height:70px"
               class="function-icon">
          <i v-else
             class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item prop="iconSmallImg"
                    label="小图标">
        <el-upload class="item_width_1 avatar-uploader"
                   :action="uploadUlr"
                   :show-file-list="false"
                   :on-success="function (res, file) { return handleImgSuccess(res, file, 'small')}"
                   :before-upload="beforeImgUpload">
          <img v-if="dataForm.iconSmallImg"
               :src="dataForm.iconSmallImg"
               style="width:70px;height:70px"
               class="function-icon">
          <i v-else
             class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary"
                 @click="saveForm()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>

export default {
  data () {
    return {
      visible: false,

      dataForm: {
        customerId: '',
        functionId: '',
        functionName: '',
        iconLargeImg: '',
        iconSmallImg: ''

      },
      uploadUlr: window.SITE_CONFIG['apiURL'] + '/oss/file/function/upload'
    }
  },
  created () {
    // this.queryFunctionList()
  },
  computed: {
    dataRule () {
      return {
        functionName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        iconLargeImg: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        iconSmallImg: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]

      }
    }
  },
  methods: {
    init (dataForm) {
      console.log(dataForm)
      this.visible = true
      // this.dataForm=dataForm

      this.$nextTick(() => {
        Object.assign(this.dataForm, dataForm)
      })
    },

    // 上传大图标成功
    handleImgSuccess (res, file, type) {
      if (res.code === 0 && res.msg === 'success') {
        if (type === 'large') {
          this.dataForm.iconLargeImg = res.data.url
        } else if (type === 'small') {
          this.dataForm.iconSmallImg = res.data.url
        }
      } else {
        this.$message.error(res.msg)
      }
    },

    beforeImgUpload (file) {
      // const isPNG = file.type === 'image/png'
      const isLt1M = file.size / 1024 / 1024 < 1

      // if (!isPNG) {
      //   this.$message.error('上传图片只能是 PNG 格式!')
      // }
      if (!isLt1M) {
        this.$message.error('上传图片大小不能超过 1MB!')
      }
      // return isPNG && isLt1M
      return isLt1M
    },

    saveForm () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          this.$message.error('表单验证失败!')
        } else {
          console.log(this.dataForm)
          const url = 'https://nei.netease.com/api/apimock-v2/e3b1d0eb88e905f6c7ee559b2d6bb7ad/oper/customize/customerfunctiondetail/updatecustomerfunction'
          // const url = '/oper/customize/customerfunctiondetail/updatecustomerfunction'
          window.app.ajax.post(url, this.dataForm,
            (data, rspMsg) => {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
              this.visible = false
              this.$emit('editDiaOK')
            },
            (rspMsg, data) => {
              this.endLoading()
              this.$message.error(rspMsg)
            })
        }
      })
    },
    handleClose () {
      this.visible = false
    }

  }
}
</script>

<style scoped>
.function-icon {
  width: 28px;
}
.item_width_1 {
  width: 300px;
}
.item_width_2 {
  width: 700px;
}
.block {
  display: block;
}
</style>
