<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'">
      <div style="margin-top:20px">
        <el-form-item label="默认功能名称"
                      prop="customizedName">
          <div class="item_width_1">
            <span>{{dataForm.customizedName}}</span></div>
        </el-form-item>
        <el-form-item label="功能名称"
                      prop="functionName">
          <el-input class="item_width_1"
                    v-model="dataForm.functionName"
                    placeholder="功能名称"></el-input>
        </el-form-item>
      </div>
      <div style="margin-top:20px">
        <el-form-item prop="defaultLargeImg"
                      label="默认大图标">
          <div class="item_width_1">
            <img :src="dataForm.defaultLargeImg"
                 style="width:70px;height:70px"
                 class="function-icon">
          </div>

        </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-button class="btn_reset"
                     type="primary"
                     size="mini"
                     @click="resetLargeImg">恢复默认</el-button>

        </el-form-item>
      </div>
      <div style="margin-top:10px">
        <el-form-item prop="defaultSmallImg"
                      label="默认小图标">
          <div class="item_width_1">
            <img :src="dataForm.defaultSmallImg"
                 style="width:40px;height:40px"
                 class="function-icon">
          </div>

        </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:40px;height:40px"
                 class="function-icon">
            <i v-else
               class="el-icon-plus avatar-uploader-icon"></i>

          </el-upload>
          <el-button class="btn_reset"
                     type="primary"
                     size="mini"
                     @click="resetSmallImg">恢复默认</el-button>

        </el-form-item>
      </div>

    </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: '',
        customizedName: '', // 默认名称
        functionName: '', // 自定义名称
        defaultLargeImg: '', // 默认大图标
        iconLargeImg: '', // 自定义大图标
        defaultSmallImg: '', // 默认小图标
        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)
      })
    },

    // 重置大图标
    resetLargeImg () {
      this.dataForm.iconLargeImg = this.dataForm.defaultLargeImg
    },
    // 重置小图标
    resetSmallImg () {
      this.dataForm.iconSmallImg = this.dataForm.defaultSmallImg
    },

    // 上传大图标成功
    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 {
          if (this.dataForm.functionName.length > 5) {
            this.$message.warning('功能名称长度不能大于5个字!')
            return false
          } else if (this.dataForm.functionName.length < 2) {
            this.$message.warning('功能名称长度不能小于2个字!')
            return false
          }
          // 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: 200px;
}
.item_width_2 {
  width: 700px;
}
.block {
  display: block;
}
.btn_reset {
  vertical-align: bottom;
  margin-left: 10px;
}
</style>
