<template>
  <div class="container">
    <div class="page-title">
      <div class="text1">添加检索记录</div>
      <!-- <div class="text2">选择分值对各项目进行打分</div> -->
    </div>

    <div class="score-card">
      <div class="name">
        检查情况
      </div>
      <van-field
        type="textarea"
        class="textarea"
        placeholder="请输入…"
        v-model="form.content"
        :autosize="{ maxHeight: 200, minHeight: 160 }"
        :maxlength="150"
      ></van-field>
      <div class="file">
        <van-uploader
          :after-read="afterRead"
          :before-read="beforeRead"
          :before-delete="beforeDelete"
          v-model="form.imgs"
          class="miniImg"
        >
          <div class="upload-mini">
            <img src="../../assets/icon_add.svg" alt="" />
            <div class="text">添加佐证</div>
          </div>
        </van-uploader>
        <div class="tips">
          最多上传3张
        </div>
      </div>
    </div>

    <!-- <div class="page-title">
      <div class="text1">上传图片资源</div>
      <div class="text2">上传现场图片材料</div>
    </div> -->

    <!-- <van-uploader
      :after-read="afterRead"
      :before-read="beforeRead"
      :before-delete="beforeDelete"
      v-model="form.imgs"
      class="bigImg"
      name="big"
    >
      <div class="upload">
        <img src="../../assets/icon_add.svg" alt="" />
        <div class="text">点击上传</div>
      </div>
    </van-uploader> -->

    <div class="bottom">
      <van-button
        class="submit"
        @click="submit"
        :color="$colors.prim"
        block
        round
        :disabled="!canSubmit"
        >提交</van-button
      >
    </div>
  </div>
</template>

<script>
import expert from "../../mixins/expert";
import Compressor from "compressorjs";
export default {
  name: "expertAdd",
  mixins: [expert],
  metaInfo() {
    return {
      title: "上传评分"
    };
  },
  data() {
    return {
      form: {
        content: "",
        file: [],
        imgs: []
      },
      img: [],
      submitList: {}
    };
  },
  computed: {
    canSubmit() {
      return !!this.form.content;
    }
  },
  mounted() {},
  methods: {
    afterRead(file, detail) {
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true
      });
      this.$http
        .post("/upload/base64", {
          base64: file.content
        })
        .then(res => {
          this.$toast.clear();
          let imgs = [...this.form.file];
          imgs.push(res);
          this.form.file = imgs;
        });
    },
    beforeDelete(file, detail) {
      let submitForm = { ...this.form };
      var imgs1 = [...submitForm.file];
      imgs1.splice(detail.index, 1);
      var imgs2 = [...submitForm.imgs];
      imgs2.splice(detail.index, 1);
      this.form.file = imgs1;
      this.form.imgs = imgs2;
    },
    beforeRead(file) {
      return new Promise(resolve => {
        // compressorjs 默认开启 checkOrientation 选项
        // 会将图片修正为正确方向
        new Compressor(file, {
          success: resolve,
          error(err) {
            console.log(err.message);
          }
        });
      });
    },
    submit() {
      let form = { ...this.form };
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true
      });
      delete form.imgs;
      form.userId = this.$store.state.userInfo.id;
      form.recordId = this.$route.query.recordId;
      this.$http
        .post("/recordCheck/save", form, { body: "json" })
        .then(() => {
          this.saving = false;
          this.$toast.success("成功");
          setTimeout(() => {
            this.$router.go(-1);
          }, 1500);
        })
        .catch(e => {
          console.log(e);
          this.saving = false;
          this.$message.error(e.error);
        });
    }
  }
};
</script>

<style lang="less" scoped>
.bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 6px 26px calc(env(safe-area-inset-bottom) + 6px);
  background-color: #fff;
  z-index: 20;

  display: flex;

  .preview {
    min-width: 82px;
    width: 82px;
  }
  .submit {
    flex-grow: 1;
    // margin-left: 13px;
  }
}
.container {
  padding: 10px 16px 100px;
  .page-title {
    padding: 14px 16px;
    background-color: #fff;
    border-radius: 12px;
    .text1 {
      font-size: 20px;
      font-weight: bold;
      color: #313233;
      line-height: 28px;
    }
    .text2 {
      font-size: 13px;
      color: #939599;
      line-height: 22px;
    }
  }
}
.score-card {
  border-radius: 12px;
  overflow: hidden;
  background: #fff;
  margin-top: 12px;
  .name {
    font-size: 16px;
    font-weight: bold;
    color: #313233;
    line-height: 28px;
    padding: 20px 16px 0;
  }

  .score-title {
    background: #ffcf6a;
    padding: 20px;
    position: relative;

    img {
      position: absolute;
      right: 30px;
      top: 13px;
    }

    .text1 {
      font-size: 18px;
      font-weight: bold;
      color: #ffffff;
      line-height: 28px;
    }

    .text2 {
      font-size: 14px;
      color: #ffffff;
      line-height: 20px;
    }
  }
  .score-list {
    padding: 0 20px;
  }
  .file {
    padding: 16px 20px 20px;
    .tips {
      font-size: 13px;
      color: #aaabad;
      line-height: 18px;
      margin-top: 17px;
    }
  }
}

.textarea {
  border-radius: 12px;
}
.upload-mini {
  background: #ffffff;
  width: 72px;
  height: 72px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid #e6e7eb;
  img {
    width: 24px;
    height: 24px;
  }

  .text {
    font-size: 12px;
    color: #c4c7cc;
    line-height: 22px;
  }

  &:active {
    background-color: darken(#fff, 10);
  }
}
.upload {
  background: #ffffff;
  width: 140px;
  height: 140px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  img {
    width: 36px;
    height: 36px;
  }

  .text {
    font-size: 14px;
    color: #c4c7cc;
    line-height: 22px;
  }

  &:active {
    background-color: darken(#fff, 10);
  }
}
/deep/.miniImg {
  margin-top: 10px;
  .van-uploader__file,
  .van-uploader__preview-image {
    width: 72px;
    height: 72px;
    border-radius: 8px;
  }
}
/deep/.bigImg {
  .van-uploader__file,
  .van-uploader__preview-image {
    width: 140px;
    height: 140px;
    border-radius: 8px;
  }
}
</style>
