<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="12vh"
    width="800px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card style="margin-top: -25px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-form-item label="视频名称" prop="video_name">
              <el-input
                v-model="ruleForm.video_name"
                placeholder="请输入视频名称"
                :disabled="isDetail === '007'"
              />
            </el-form-item>
            <el-form-item label="视频链接" prop="video_url">
              <el-input
                v-model="ruleForm.video_url"
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4 }"
                placeholder="请输入视频链接"
                :disabled="isDetail === '007'"
              />
            </el-form-item>
            <el-form-item label="视频描述" prop="remark">
              <el-input
                v-model="ruleForm.remark"
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4 }"
                placeholder="请输入视频描述"
                :disabled="isDetail === '007'"
                maxlength="250"
                show-word-limit
              />
            </el-form-item>
            <el-form-item label="视频封面" prop="video_img">
              <div class="videoCoverBox">
                <i
                  v-show="ruleForm.video_img !== '' && isDetail !== '007'"
                  class="el-icon-close"
                  @click="closeImg()"
                >
                </i>
                <img
                  v-if="ruleForm.video_img !== ''"
                  :src="ruleForm.video_img"
                  alt=""
                  class="videoCover"
                />
                <div class="btnupload" v-else style="position: relative">
                  <i class="el-icon-plus avatar-uploader-icon"></i>
                  <Upload
                    class="Upload"
                    :accept="'.jpg,.png,.bmp,.jpeg'"
                    :multiple="true"
                    :uploadcondition="beforeAvatarUpload"
                    @UploadErrorEvent="UploadErrorEvent"
                    @UploadSuccessEvent="UploadSuccessEvent"
                  >
                  </Upload>
                </div>
              </div>
              <div class="txt-tips fl">
                <p>
                  <span>大小：小于5MB；</span
                  ><span>尺寸：250*160,单位:px；</span
                  ><span>类型：jpg.png.bmp.jpeg</span>
                </p>
              </div>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button
            v-if="isDetail !== '007'"
            type="primary"
            @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail !== "007" ? "取 消" : "关 闭"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/mobile/videos";
import resToken from "@/mixins/resToken";
import Upload from "@/components/Upload";
export default {
  name: "videos",
  props: ["showModel", "id", "isDetail", "sitem"],
  mixins: [resToken],
  components: {
    Upload,
  },
  data() {
    return {
      dynamicTags: [],
      options: [],
      value: [],
      list: [],
      states: [],
      tags: [],
      disabled: false,
      loading: false,
      loading_video_set: false,
      title: "新建视频",
      showModelThis: this.showModel,
      ruleForm: {
        video_sn: "", //视频编号
        video_name: "", // 视频名称
        video_url: "", // 视频地址
        remark: "", //视频描述
        video_img: "", //视频封面
        video_set: "", //视频设置
      },
      rulesThis: this.rules,
      rules: {
        video_name: [
          {
            required: true,
            trigger: "blur",
            message: "请输入视频名称",
          },
          {
            min: 2,
            max: 50,
            message: "视频名称在3到50个字符",
            trigger: "blur",
          },
        ],
        video_url: [
          {
            required: true,
            trigger: "blur",
            message: "请输入视频地址",
          },
        ],
        remark: [
          {
            required: true,
            trigger: "blur",
            message: "请输入视频描述",
          },
          {
            min: 3,
            max: 250,
            message: "视频描述在3到250个字符",
            trigger: "blur",
          },
        ],
        video_img: [
          {
            required: true,
            trigger: "blur",
            message: "请上传视频封面",
          },
        ],
        video_set: [
          {
            required: false,
            trigger: "blur",
            message: "asdadsdsa",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  mounted() {
    this.list = this.states.map((item) => {
      return { value: `value:${item}`, label: `label:${item}` };
    });
  },
  methods: {
    async initForm() {
      this.loading = true;
      if (this.isDetail === "003") {
        this.title = "新建视频";
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        if (this.isDetail === "007") {
          this.title = "视频详情";
          this.rulesThis = {};
        } else {
          this.title = "修改视频";
          this.rulesThis = this.rules;
        }
        await this.resetForm();
        await this.initData();
      }
      this.loading = false;
    },
    async initData() {
      const res = await asyncRequest.detail({ id: this.id });
      if (res && res.code === 0 && res.data) {
        this.ruleForm = res.data;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      }
    },
    async resetForm() {
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            video_sn: "",
            video_name: "",
            remark: "",
            video_url: "", // 视频地址
            video_img: "", //视频封面
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let model = Object.assign({}, this.ruleForm);
          let res = {};
          if (this.id === "add") {
            res = await asyncRequest.add(model);
          } else {
            res = await asyncRequest.update(model);
          }
          this.loading = false;
          if (res && res.code === 0) {
            const title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.msg);
          }
        } else {
          return false;
        }
      });
    },
    UploadErrorEvent() {
      this.$message.error("图片上传失败!");
      this.$refs.ruleForm.validateField("video_img");
    },
    UploadSuccessEvent(data) {
      if (data && data.url && data.url !== "break") {
        const { url, name } = data.url;
        this.ruleForm.video_img = url;
        this.$message.success("图片上传成功!");
        this.$refs.ruleForm.validateField("video_img");
      }
    },
    beforeAvatarUpload(file) {
      let isJPG = false,
        isLt5M = false; //判断文件的大小
      if (file) {
        //传入文件
        if (
          file.type === "image/jpg" ||
          file.type === "image/png" ||
          file.type === "image/bmp" ||
          file.type === "image/jpeg"
        ) {
          isJPG = true;
        }
        isLt5M = file.size / 1024 / 1024 < 5;
        if (!isJPG) {
          this.$message.error("图片格式不正确！");
        }
        if (!isLt5M) {
          this.$message.error("图片大小不能超过 5MB!");
        }
      }
      return isJPG && isLt5M;
    },
    async closeImg() {
      this.ruleForm.video_img = "";
      this.$refs.ruleForm.validateField("video_img");
    },
  },
};
</script>

<style lang="scss" scoped>
.videos {
  .btnupload {
    float: left;
    border: 1px solid rgb(220, 223, 230);
    box-sizing: border-box;
    width: 254px;
    height: 164px;
    line-height: 164px;
    text-align: center;
  }
  .Upload {
    width: 254px;
    height: 164px;
    line-height: 164px;
    text-align: center;
    position: absolute;
    line-height: 0px;
    top: 0;
    left: 0;
    z-index: 2;
    line-height: 164px;
  }
  .fileUp {
    vertical-align: top;
  }
  .avatar {
    width: 254px;
    height: 164px;
    line-height: 164px;
    text-align: center;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 33px;
    color: #8c939d;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
  }
  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
  .txt-tips {
    display: inline-block;
    font-size: 13px;
    color: #606266;
    padding: 15px 0 0 15px;
    p {
      margin: 0;
      line-height: 30px;
      span {
        padding: 0 15px 0 0;
      }
    }
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
}
.videoCover {
  //视频封面的容器样式
  width: 250px;
  height: 160px;
  overflow: hidden;
  display: block;
  object-fit: cover;
}
.videoCoverBox {
  width: 250px;
  height: 160px;
  position: relative;
  left: 0;
  top: 0;
  cursor: pointer;
  .el-icon-close {
    width: 20px;
    height: 20px;
    display: block;
    position: absolute;
    left: 100%;
    top: 0;
    margin-left: 4px;
  }
}
// 视频设置的容器
.video-set {
  width: 100%;
  height: 100px;
}
</style>
