<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-05-15 17:50:18
 -->
<!--
  <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">
          <limitInput :inputWidth="440" :inputValue.sync="ruleForm.materialTitle" :holder="'请输入视频标题'" :getByType="'word'" :maxLength="20"> </limitInput>
        </el-form-item>
        <el-form-item label="封面" prop="imgUrl">
          <single-upload uploadType="2" :uploadLimit="1" :imgSrc.sync="ruleForm.imgUrl"> </single-upload>
        </el-form-item>
        <el-form-item label="视频" prop="link">
          <single-upload uploadType="3" :uploadLimit="10" :videoSrc.sync="ruleForm.link"> </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 } 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() {
    return {
      editPersion: localStorage.getItem('userName'),
      dialogVisible: true,
      ruleForm: {
        materialId: '',
        materialTitle: '',
        link: '',
        imgUrl: ''
      },
      rules: {
        materialTitle: [{ required: true, message: '请输入视频标题', trigger: 'blur' }],
        link: [{ required: true, message: '请上传视频', trigger: 'blur' }],
        imgUrl: [{ required: true, message: '请上传封面', trigger: 'blur' }]
      },
      emojiList: emojiArr
    };
  },
  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) {
          that.postSave();
        }
      });
    }, 300),
    postSave() {
      const that = this;
      const data = {
        categoryId: that.categoryId,
        materialType: 4,
        materialId: that.ruleForm.materialId,
        materialTitle: that.ruleForm.materialTitle,
        link: that.ruleForm.link,
        imgUrl: that.ruleForm.imgUrl
      };
      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;
    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>
