<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-11-10 14:10:08
 -->
<!--
  <add-self-app :edit-row="editRow" @closeText="closeText" @submitText="submitText"></add-self-app>
  import addSelfApp from '@/components/set/add-self-app.vue';
 -->
<template>
  <el-dialog :title="!!editRow.materialId ? '自建应用' : '自建应用'" :visible.sync="dialogVisible" width="600px" :before-close="handleClose">
    <div class="">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="应用名称" prop="secretName">
          <el-input class="w-440" type="text" v-model="ruleForm.secretName" maxlength="30"> </el-input>
          <span class="input-cell-tip">{{ruleForm.secretName.length}}/30</span>
        </el-form-item>
        <el-form-item label="Agentld" prop="agentId">
          <el-input class="w-440" type="textarea" rows="3" v-model="ruleForm.agentId" maxlength="30"></el-input>
          <span class="input-cell-tip top-40">{{ruleForm.agentId.length}}/30</span>
        </el-form-item>
        <el-form-item label="Secret" prop="secretVal">
          <el-input class="w-440" type="textarea" rows="3" v-model="ruleForm.secretVal" maxlength="100"></el-input>
          <span class="input-cell-tip top-40">{{ruleForm.secretVal.length}}/100</span>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="toCancel">取消</el-button>
      <el-button type="primary" :loading="loadBtn" @click="submitForm('ruleForm')">确认</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { _debounce, emojiToStr } from '@/common/js/public';
import fetch from '@/api/enterprise.js';
const { addSelfApp } = fetch;
import showMsg from '@/common/js/showmsg';
export default {
  name: 'AddSelfApp',
  props: {
    editRow: {
      type: Object,
      default() {
        return {};
      }
    },
    categoryId: {
      type: Object,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      dialogVisible: true,
      ruleForm: {
        secretName: '',
        secretVal: '',
        agentId: '',
        secretId: ''
      },
      rules: {
        secretName: [ { required: true, message: '请输入应用名称', trigger: 'blur' } ],
        secretVal: [ { required: true, message: '请输入secret', trigger: 'blur' } ],
        agentId: [ { required: true, message: '请输入Agentld', trigger: 'blur' } ]
      },
      loadBtn: false,
      brandOptions: [] // 品牌
    };
  },

  mounted() {
    const that = this;
    if (Object.keys(that.editRow).length) {
      that.ruleForm = JSON.parse(JSON.stringify(that.editRow));
    }
  },
  methods: {
    /**
     * @description: 关闭弹窗
     * @author: 无尘
     */
    toCancel() {
      const that = this;
      that.$emit('closeText');
      that.$refs['ruleForm'].resetFields();
    },
    handleClose(done) {
      const that = this;
      that.$emit('closeText');
      that.$refs['ruleForm'].resetFields();
    },
    /**
     * @description: 保存
     * @param {String} formName
     * @author: 无尘
     */
    submitForm: _debounce(function(formName) {
      const that = this;
      that.$refs[formName].validate(valid => {
        if (valid) {
          that.postSave();
        }
      });
    }, 300),
    postSave() {
      const that = this;
      that.loadBtn = true;
      const data = {
        agentId: emojiToStr(that.ruleForm.agentId),
        secretName: emojiToStr(that.ruleForm.secretName),
        secretVal: emojiToStr(that.ruleForm.secretVal),
        secretId: that.ruleForm.secretId,
        secretType: 3// 1 客户联系secret 2通讯录secret 3好办助手 4会员小程序
      };
      addSelfApp(data)
        .then(res => {
          that.loadBtn = false;
          if (res.code == '0000') {
            showMsg.showmsg('操作成功', 'success');
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitText');
          }else {
            that.$message({
              message: res.message || '未知错误',
              type: 'warning'
            });
            that.$emit('submitText');
          }
        })
        .catch(error => {
          that.loadBtn = false;
        });
    }
  },
  watch: {
    editRow(newData) {
      const that = this;
      if (Object.keys(newData).length) {
        that.ruleForm = JSON.parse(JSON.stringify(newData));
      }
    }
  }
};
</script>
<style lang="less" scoped>
.w-440 {
  width: 440px;
}
.m-b-20 {
  margin-bottom: 20px;
}
.p-l-18 {
  padding-left: 18px;
}
.material-content {
  position: relative;
  .material-body {
    /*  resize: none;
    width: 440px;
    height: 273px;
    border-radius: 2px;
    border: 1px solid rgba(196, 198, 207, 1); */
    .el-textarea {
      /deep/ .el-textarea__inner {
        height: 68px;
      }
    }
  }
}
.el-textarea {
  /deep/ .el-textarea__inner {
    height: 68px;
    resize: none;
  }
}
.input-cell-tip {
  position: absolute;
  top: 2px;
  right: 30px;
  display: inline-block;
  line-height: 32px;
  text-align: right;
  font-size: 12px;
  color: #c0c4cc;
  font-weight: 400;
  &.top-40 {
    top: 40px;
  }
}
</style>
