<!--
 * @Descripttion:企业信息-新增小程序
 * @version: 1.0.0
 * @Author: 阿羽
 * @Date: 2021-04-12 15:57:24
 * @LastEditors: 阿羽
 * @LastEditTime: 2021-04-12 15:57:24
 -->
<!--
  <app-set @closeText="closeText" @submitText="submitText"></app-set>
  import appSet from '@/components/set/app-set.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="enterpriseId">
          <el-select v-model="ruleForm.enterpriseId" placeholder="请选择授权商户" :disabled="editRow.type === 'edit'" @change="chooseEnterprise">
            <el-option v-for="item in options" :key="item.value" :label="item.enterpriseName" :value="item.enterpriseId"> </el-option>
          </el-select>
        </el-form-item>
        <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 label="AppID：" prop="appId">
          <el-input show-word-limit placeholder="请先选择授权商户" type="text" v-model="ruleForm.appId" maxlength="80" :disabled="true"></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, getRequest } from '@/api/api';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
export default {
  props: {
    editRow: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  components: {
    singleUpload
  },
  data() {
    return {
      dialogVisible: true,
      ruleForm: {
        miniprogramName: '',
        appId: '',
        title: '',
        imageUrl: ''
      },
      rules: {
        enterpriseId: [{ required: true, message: '请选择授权商户', trigger: 'blur' }],
        miniprogramName: [{ required: true, message: '请输入小程序名称', trigger: 'blur' }],
        appId: [{ required: true, message: '请输入AppID', trigger: 'blur' }],
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }]
      },
      options: []
    };
  },
  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) {
          this.$confirm('是否确认提交?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              that.postSave();
            })
            .catch(() => {});
        }
      });
    }, 300),
    postSave() {
      const that = this;
      const data = { ...that.ruleForm };
      data.miniprogramType = 0;
      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
          });
        });
    },
    getTableList(val) {
      const that = this;
      let para = {};
      getRequest('/haoban-manage3-web/wxa-enterprise-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.options = resData.result || [];
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    chooseEnterprise(val) {
      const that = this;
      const item = that.options.filter(item => item.enterpriseId === val)[0];
      that.ruleForm.miniprogramName = item.miniprogramName;
      that.ruleForm.appId = item.appId;
      that.ruleForm.title = item.title;
      that.ruleForm.imageUrl = item.imageUrl;
    }
  },
  watch: {
    editRow(newData) {
      const that = this;
      if (Object.keys(newData).length) {
        that.$nextTick(() => {
          that.ruleForm = JSON.parse(JSON.stringify(newData));
        });
      }
    }
  },
  mounted() {
    const that = this;
    that.getTableList();
    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>
