<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-05-13 16:24:27
 -->
<!--
  <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="link">
          <limitInput :inputWidth="440" :inputValue.sync="ruleForm.link" :holder="'请输入链接,格式需带https://www,或http://www'" :getByType="'word'" :maxLength="200"> </limitInput>
        </el-form-item>
        <el-form-item label="摘要" prop="materialDesc">
          <limitInput :inputWidth="440" :inputValue.sync="ruleForm.materialDesc" :holder="'请输入网页摘要'" :getByType="'word'" :maxLength="200"> </limitInput>
        </el-form-item>
        <el-form-item label="封面" prop="imgUrl">
          <single-upload uploadType="2" :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 } 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) => {
      let reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
      if (value === '') {
        callback(new Error('请输入链接'));
      } else if (!reg.test(value)) {
        callback(new Error('请输入链接,正确格式：https://www.XXX.com!'));
      } else {
        callback();
      }
    };
    return {
      editPersion: localStorage.getItem('userName'),
      dialogVisible: true,
      ruleForm: {
        materialId: '',
        link: '',
        materialTitle: '',
        materialDesc: '',
        imgUrl: ''
      },
      rules: {
        materialTitle: [{ required: true, message: '请输入网页标题', trigger: 'blur' }],
        link: [{ required: true, validator: validateUrl, trigger: 'blur' }],
        materialDesc: [{ 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: 3,
        link: that.ruleForm.link,
        materialId: that.ruleForm.materialId,
        materialTitle: that.ruleForm.materialTitle,
        imgUrl: that.ruleForm.imgUrl,
        materialDesc: that.ruleForm.materialDesc
      };
      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="less" 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 {
      /deep/ .el-textarea__inner {
        height: 273px;
      }
    }
  }
  .emoji-img {
    position: absolute;
    left: 10px;
    bottom: 10px;
  }
}
.el-textarea {
  /deep/ .el-textarea__inner {
    height: 273px;
  }
}
</style>
