<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-09-22 11:03:42
 -->
<!--
  <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="materialTitle" v-if="!multiple">
          <limitInput :inputWidth="440" :inputValue.sync="ruleForm.materialTitle" :holder="'请输入图片标题'" :getByType="'word'" :maxLength="20"> </limitInput>
        </el-form-item>
        <el-form-item label="图片" prop="imgUrl">
          <mult-upload v-if="multiple" :imgList.sync="imgList" />
          <single-upload v-else uploadType="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" :loading="loading" @click="submitForm('ruleForm')">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import singleUpload from '@/components/single-upload.vue';
import multUpload from '@/components/mult-upload-img.vue';
import limitInput from '@/components/limit-input.vue';
import limitTextarea from '@/components/limit-textarea.vue';
import { postRequest, postJson } 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 '';
      }
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  components: {
    limitInput,
    limitTextarea,
    singleUpload,
    multUpload
  },
  data() {
    return {
      editPersion: localStorage.getItem('userName'),
      dialogVisible: true,
      ruleForm: {
        materialId: '',
        materialTitle: '',
        imgUrl: ''
      },
      rules: {
        materialTitle: [{ required: true, message: '请输入图片标题', trigger: 'blur' }],
        imgUrl: [{ required: true, message: '请上传图片', trigger: 'blur' }]
      },
      imgList: [],
      loading: false,
      emojiList: emojiArr
    };
  },
  methods: {
    toCancel() {
      const that = this;
      that.$emit('closeText');
      that.$refs['ruleForm'].resetFields();
      this.imgList = [];
    },
    handleClose() {
      this.toCancel();
    },
    submitForm(formName) {
      if (this.multiple) {
        if (!this.imgList.length) return showMsg.showmsg('请上传图片', 'info');
        this.submitImgList();
      } else {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.postSave();
          }
        });
      }
    },
    submitImgList() {
      const data = {
        material: {
          materialTitle: this.categoryId,
          materialType: 2,
          categoryId: this.categoryId,
          wxEnterpriseId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : ''
        },
        materialMediaList: this.imgList.map(item => {
          return { mediaUrl: item.url, mediaName: item.mediaName };
        })
      };
      this.loading = true;
      postJson('/haoban-manage3-web/material-batch-add', data)
        .then(res => {
          const { data } = res;
          if (data.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            this.imgList = [];
            this.$emit('submitText');
          } else {
            errMsg.errorMsg(data);
          }
        })
        .catch(error => {
          this.$message.error({
            duration: 1000,
            message: error.message
          });
        })
        .finally(() => {
          this.loading = false;
        });
    },
    postSave() {
      const that = this;
      const data = {
        categoryId: that.categoryId,
        materialType: 2,
        materialId: that.ruleForm.materialId,
        materialTitle: that.ruleForm.materialTitle,
        imgUrl: that.ruleForm.imgUrl
      };
      this.loading = true;
      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
          });
        })
        .finally(() => {
          this.loading = false;
        });
    }
  },
  watch: {
    editRow(newData) {
      const that = this;
      if (Object.keys(newData).length) {
        that.ruleForm = JSON.parse(JSON.stringify(newData));
      }
    }
  },
  mounted() {
    const that = this;
    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>
