<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-05-19 11:40:14
 -->
<!--
  <image-edit :categoryId="categoryId" @closeText="closeText" @submitText="submitText"></image-edit>
  import imageEdit from '@/components/set/image-edit.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="appId">
          <template v-if="!!editRow.materialId">{{ ruleForm.materialDesc }}</template>
          <template v-else>
            <el-select v-model="ruleForm.appId" placeholder="请选择小程序" @change="appsChange">
              <el-option v-for="item in appList" :key="item.value" :label="item.miniprogramName" :value="item.appId"> </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="标题" prop="materialTitle">
          <limitInput :inputWidth="440" :inputValue.sync="ruleForm.materialTitle" :holder="'请输入标题'" :getByType="'word'" :maxLength="8"> </limitInput>
        </el-form-item>
        <el-form-item label="页面路径" prop="link">
          <limitInput :inputWidth="440" :inputValue.sync="ruleForm.link" :holder="'请输入页面路径'" :getByType="'word'" :maxLength="100"> </limitInput>
        </el-form-item>
        <el-form-item label="封面" prop="imgUrl">
          <single-upload uploadType="4" :uploadLimit="1" :imgSrc.sync="ruleForm.imgUrl"> </single-upload>
        </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')">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import singleUpload from '@/components/single-upload.vue';
import limitInput from '@/components/limit-input.vue';
import limitTextarea from '@/components/limit-textarea.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';
import { emojiArr } from '@/utils/emoji';
export default {
  props: {
    editRow: {
      type: Object,
      default() {
        return {};
      }
    },
    categoryId: {
      type: Object,
      default() {
        return '';
      }
    }
  },
  components: {
    limitInput,
    limitTextarea,
    singleUpload
  },
  data() {
    const validateUrl = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入页面路径'));
      } else {
        callback();
      }
    };
    return {
      editPersion: localStorage.getItem('userName'),
      dialogVisible: true,
      appList: [],
      ruleForm: {
        appId: null,
        materialId: '',
        link: '',
        materialTitle: '',
        imgUrl: ''
      },
      rules: {
        appId: [{ required: true, message: '请选择小程序', trigger: 'blur' }],
        materialTitle: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        link: [{ required: true, validator: validateUrl, trigger: 'blur' }],
        imgUrl: [{ required: true, message: '请上传封面', trigger: 'blur' }]
      },
      emojiList: emojiArr
    };
  },
  methods: {
    getAppData() {
      const that = this;
      getRequest('/haoban-manage3-web/list-miniprogram-setting', {})
        .then(res => {
          if (res.data.errorCode == 1) {
            let resData = res.data;
            that.appList = Array.isArray(resData.result) ? resData.result : [];
            return;
          }
          errMsg.errorMsg(res.data);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    appsChange(v) {
      const that = this;
      const item = that.appList.filter(item => item.appId === v)[0];
      this.ruleForm = Object.assign({}, this.ruleForm, {
        appId: item.appId,
        materialTitle: item.title,
        imgUrl: item.imageUrl
      });
    },
    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) {
          that.postSave();
        }
      });
    }, 300),
    postSave() {
      const that = this;
      const data = {
        categoryId: that.categoryId,
        materialType: 6,
        appId: that.ruleForm.appId,
        link: that.ruleForm.link,
        materialTitle: that.ruleForm.materialTitle,
        imgUrl: that.ruleForm.imgUrl
      };
      if (that.ruleForm.materialId) data.materialId = that.ruleForm.materialId;
      postRequest(that.ruleForm.materialId ? '/haoban-manage3-web/material-edit' : '/haoban-manage3-web/material-add', data)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitText');
          } else {
            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.ruleForm = JSON.parse(JSON.stringify(newData));
      }
    }
  },
  mounted() {
    const that = this;
    that.getAppData();
    if (Object.keys(that.editRow).length) {
      that.ruleForm = JSON.parse(JSON.stringify(that.editRow));
    }
  }
};
</script>
<style lang="scss" scoped>
.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 {
      >>> .el-textarea__inner {
        height: 273px;
      }
    }
  }
  .emoji-img {
    position: absolute;
    left: 10px;
    bottom: 10px;
  }
}
.el-textarea {
  >>> .el-textarea__inner {
    height: 273px;
  }
}
</style>
