<template>
  <el-form
    ref="formData"
    :model="formData"
    :rules="rules"
    label-width="150px"
    label-suffix="："
    class="basic-form"
  >
    <el-form-item label="圈子名称" prop="name">
      <el-col :span="13">
        <el-input
          size="small"
          v-model="formData.name"
          placeholder="请输入圈子名称"
          style="width:70%;"
          @change="returnTypeData('name')"
        ></el-input>
        <span class="word-num">{{(formData.name).replace(/\s+/g,"").length}}/14</span>
      </el-col>
    </el-form-item>
    <div class="basic-item-icon">
      <el-form-item label="圈子头像" class="required-label">
        <el-upload
          v-model="formData.headUrl"
          class="bg-uploader"
          action="#"
          :show-file-list="false"
          :before-upload="beforeUploadPic1"
        >
          <img
            v-if="formData.headUrl"
            :src="formData.headUrl"
            @mouseover.stop="imgMouseOver1=true"
            class="bg-img"
          />
          <img v-if="!formData.headUrl" class="bg-img" src="../../assets/image/small.png" />
          <div
            class="img-delete"
            v-show="imgMouseOver1"
            @click.stop="deleteImg(1)"
            @mouseout.stop="imgMouseOver1=false"
          >
            <i class="el-icon-delete"></i>
          </div>
          <div class="limit-text">
            <p></p>
            <p>尺寸：160*160</p>
            <p>支持jpeg, png格式</p>
          </div>
        </el-upload>
      </el-form-item>
      <p class="upload-message" v-if="uploadImgMessage1">请选择圈子头像</p>
    </div>
    <el-form-item label="圈子介绍" prop="introduce">
      <el-col :span="13">
        <el-input
          type="textarea"
          rows="2"
          maxlength="40"
          size="small"
          v-model="formData.introduce"
          placeholder="请输入圈子介绍"
          @change="returnTypeData('introduce')"
          style="width:70%;"
        ></el-input>
        <span class="word-num">{{(formData.introduce).replace(/\s+/g,"").length}}/40</span>
      </el-col>
    </el-form-item>
    <el-form-item label="内容发布权限" prop="dynamicFlag">
      <el-select
        v-model="formData.dynamicFlag"
        @change="returnTypeData('dynamicFlag')"
        placeholder="请选择发布权限"
        size="small"
        clearable
      >
        <el-option
          v-for="(item,index) in contentList"
          :key="index"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>
<script>
  let vm = null;
  import { openLoading, closeLoading } from "../../utils/utils";
  import { doUpload, getFilePath } from "../../utils/qiniu-util";
  export default {
    name: "baseinfo",
    props: {
      formData: {
        type: Object,
        default: () => {
          return {}
        }
      }
    },
    created() {
      vm = this;
    },
    data() {
      let checkProjectStr = (rule, value, callback) => {
        if (value.indexOf("\\") != -1) {
          //存在
          callback(new Error("请勿输入字符“ \\ ”"));
        } else if (value.indexOf(".") != -1) {
          callback(new Error("请勿输入字符“ . ”"));
        } else {
          callback();
        }
      };
      return {
        rules: {
          name: [
            { required: true, message: "请输入圈子名称", trigger: "blur" },
            {
              min: 2,
              max: 14,
              message: "输入长度为2-14的内容，可包含中英文、数字及特殊符号",
              trigger: "blur"
            },
            { validator: checkProjectStr, trigger: "blur" }
          ],
          introduce: [
            { required: true, message: "请输入圈子介绍", trigger: "blur" },
            {
              min: 2,
              max: 40,
              message: "输入长度为2-40的内容，可包含中英文、数字及特殊符号",
              trigger: "blur"
            },
            { validator: checkProjectStr, trigger: "blur" }
          ],
          headUrl: [
            { required: true, message: "请选择圈子头像", trigger: "blur" }
          ],
          dynamicFlag: [
            { required: true, message: "请选择内容发布权限", trigger: "blur" },
          ]
        },
        imgMouseOver1: false,
        uploadImgMessage1: false,//未上传图片，校验提示语
        contentList: [
          {
            value: 1,
            label: '所有人可发布'
          }, {
            value: 2,
            label: '管理员可发布'
          },
        ],
      }
    },

    methods: {
      returnTypeData(name) {
        this.$emit('returnTypeData', {
          name: name,
          val: this.formData[name]

        })
      },
      //表单校验
      submitForm() {
        let formName = "formData";
        let flag = null;
        if (!this.formData.headUrl){
          this.uploadImgMessage1 = true;
        }
        this.$refs[formName].validate(valid => {
          if (valid&&this.formData.headUrl!="") {
            flag = true;
          } else {
            console.log("error submit!!");
            flag = false;
          }
        });
        this.$emit('returnIsNext', flag);
      },
      //上传圈子头像
      beforeUploadPic1(file) {
        let fileLimit = {
          width: 48,
          height: 48,
          size: 0.5,
          sizeText: "500K",
          key: "headUrl",
          more: "imgUrl1More",
          show: "uploadImgMessage1"
        };
        this.beforeUpload(file, fileLimit);
      },
      //上传图片
      beforeUpload(file, fileLimit) {
        let vm = this;
        const isJPG = file.type === "image/jpeg";
        const isPNG = file.type === "image/png";
        const isLt2M = file.size / 1024 / 1024 < fileLimit.size;
        if (!isJPG && !isPNG) {
          vm.$message.error("图片格式不符合规范，请根据规范上传图片");
          // return;
        }
        if (!isLt2M) {
          vm.$message.error("图片大小不符合规范，请根据规范上传图片 ");
          return;
        }
        let _img = new FileReader();
        _img.readAsDataURL(file);
        _img.onload = function (theFile) {
          let image = new Image();
          image.src = theFile.target.result;
          image.onload = function () {
            let _this = this;
            // if (_this.width != fileLimit.width || _this.height != fileLimit.height) {
            if (_this.width != _this.height) {
              vm.$message.error("图片尺寸不符合规范，请根据规范上传图片");
            } else {
              openLoading(vm);
              doUpload(vm, file, getFilePath(file, null), "preview4", "progress1", 1).then(function (path) {
                closeLoading(vm);
                console.log('上传成功后路径', path);
                if (fileLimit.show == "uploadImgMessage1") {
                  vm.uploadImgMessage1 = false;
                } else if (fileLimit.show == "uploadImgMessage2") {
                  vm.uploadImgMessage2 = false;
                }
                vm.formData[fileLimit.key] = path.fullPath;
                vm.formData[fileLimit.more] = {
                  attachmentName: path.name,
                  attachmentExt: path.ext,
                  attachmentSize: path.size
                };
                vm.$message.success("上传成功");
              });
            }
          };
        };
        return isJPG && isLt2M;
      },
      //删除图片
      deleteImg(type) {
        if (type == 1) {
          vm.formData.headUrl = "";
          vm.imgMouseOver1 = false;
        }
      }
    }
  }
</script>

<style lang="less">
.first-step {
  margin-top: 20px;

  .p-title {
    padding-left: 10px;
    margin-bottom: 15px;
  }

  .basic-item-icon {
    position: relative;

    .require {
      position: absolute;
      left: 67px;
      top: 11px;
      color: #f56c6c;
    }

    .upload-message {
      position: absolute;
      left: 160px;
      top: 105px;
      font-size: 12px;
      color: #f56c6c;
    }

    .img-delete {
      position: absolute;
      left: 0px;
      top: 0px;
      width: 84px;
      height: 100px;
      background: #000;
      opacity: 0.7;
      z-index: 999;

      i {
        color: #fff;
        margin-top: 39px;
        margin-left: 0px;
      }
    }
  }

  .word-num {
    font-size: 12px;
    color: #999;
    padding-top: 5px;
  }

  .line {
    margin-left: 10px;
    width: 20px;
  }

  .bg-uploader {
    .bg-img {
      float: left;
      width: 84px;
      height: 100px;
    }

    .limit-text {
      float: left;
      margin-left: 10px;
      margin-top: -10px;

      p {
        font-size: 12px;
        color: #999;
        text-align: left;
      }
    }
  }

  .el-upload__tip {
    position: absolute;
    top: -6px;
    left: 130px;
  }
}

.required-label .el-form-item__label::before {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>
