<!--
 * @Descripttion:企业信息-新增自定义小程序
 * @version: 1.0.0
 * @Author: 晨曦
 * @Date: 2021-06-10 15:10:24
 * @LastEditors: 晨曦
 * @LastEditTime: 2021-06-10 15:57:24
 -->
<!--
  <add-customize-app @closeText="closeText" @submitText="submitText"></add-customize-app>
  import addCustomizeApp from '@/components/set/add-customize-app.vue';
 -->
<template>
  <el-dialog :title="editRow.type === 'edit' ? '编辑' : '添加小程序'" :visible.sync="dialogVisible" width="600px" :before-close="handleClose">
    <div class="">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <el-form-item label="小程序名称" prop="miniprogramName">
          <el-input show-word-limit placeholder="请输入小程序名称" type="text" v-model="ruleForm.miniprogramName" maxlength="20"></el-input>
        </el-form-item>
        <el-form-item prop="appId">
          <span slot="label"
            ><el-tooltip class="item" effect="dark" placement="top">
              <div slot="content">如何查看AppId：<a href="https://jingyan.baidu.com/article/95c9d20d0545f9ec4e75612d.html" target="blank">https://jingyan.baidu.com/article/95c9d20d0545f9ec4e75612d.html</a></div>
              <span style="cursor: pointer;padding-bottom: 2px; border-bottom: 1px dashed #2F54EB;">AppId</span>
            </el-tooltip></span
          >
          <el-input show-word-limit placeholder="请输入AppId" type="text" v-model="ruleForm.appId" maxlength="80"></el-input>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input show-word-limit placeholder="请输入标题" type="text" v-model="ruleForm.title" maxlength="80"></el-input>
        </el-form-item>
        <el-form-item label=" 封面" prop="pic">
          <single-upload uploadType="1" :imgSrc.sync="ruleForm.imageUrl" :field="''" class="app-set" :uploadLimit="20"> </single-upload>
          <p style="margin-top: 9px;font-size: 12px;color: #909399;line-height: 17px;">建议尺寸520*416，支持png、jpg、jpeg格式</p>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="toCancel">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">{{ editRow.type === 'edit' ? '保存' : '确定' }}</el-button>
    </div>
  </el-dialog>
</template>
<script>
import singleUpload from '@/components/single-upload.vue';
import { _debounce } from '@/common/js/public';
import { postRequest } from '@/api/api';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
export default {
  props: {
    editRow: {
      type: Object,
      default() {
        return {};
      }
    },
    enterpriseId: String
  },
  components: {
    singleUpload
  },
  data() {
    return {
      dialogVisible: true,
      ruleForm: {
        miniprogramName: '',
        appId: '',
        title: '',
        imageUrl: ''
      },
      rules: {
        miniprogramName: [{ required: true, message: '请输入小程序名称', trigger: 'blur' }],
        appId: [{ required: true, message: '请输入AppID', trigger: 'blur' }],
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }]
      }
    };
  },
  methods: {
    toCancel() {
      const that = this;
      that.$emit('closeText');
      that.$refs['ruleForm'].resetFields();
    },
    handleClose(done) {
      const that = this;
      that.$emit('closeText');
      that.$refs['ruleForm'].resetFields();
    },
    submitForm: _debounce(function(formName) {
      const that = this;
      that.$refs[formName].validate(valid => {
        if (valid) {
          if (!this.ruleForm.imageUrl) return this.$message.warning('请提供小程序的封面');
          that.postSave();
        }
      });
    }, 300),
    postSave() {
      const that = this;
      const data = { ...that.ruleForm, enterpriseId: this.enterpriseId };
      data.miniprogramType = 1;
      postRequest('/haoban-manage3-web/save-miniprogram-setting', data)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitText');
          } else {
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitText');
            errMsg.errorMsg(resData);
          }
        })
        .catch(error => {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    editRow(newData) {
      const that = this;
      if (Object.keys(newData).length) {
        that.$nextTick(() => {
          that.ruleForm = JSON.parse(JSON.stringify(newData));
        });
      }
    }
  },
  mounted() {
    const that = this;
    if (Object.keys(that.editRow).length) {
      that.$nextTick(() => {
        that.ruleForm = JSON.parse(JSON.stringify(that.editRow));
      });
    }
  }
};
</script>
<style lang="scss" scoped></style>
<style lang="scss">
.app-set {
  .avatar-uploader {
    .el-upload {
      width: 60px !important;
      height: 60px !important;
      .font-12 {
        display: none;
      }
      .avatar-uploader-icon {
        font-size: 14px;
        color: #303133;
        font-weight: bold;
      }
    }
  }
  .upload-tip {
    display: none;
  }
}
</style>
