<template>
  <div class="container">
    <div class="page-title">
      <div class="text1">考级绩效评分</div>
      <div class="text2">选择分值对各项目进行打分</div>
    </div>

    <div class="score-card" :key="index" v-for="(item, index) in categories">
      <div class="score-title">
        <div class="text1">{{ item.label }}</div>
        <div class="text2">{{ item.score }}分</div>

        <img :src="require(`../../assets/${item.icon}`)" alt="" />
      </div>
      <div class="score-list">
        <div
          class="score-item"
          v-for="(score, scoreIndex) in item.children"
          :key="scoreIndex"
        >
          <div class="text1">{{ scoreIndex + 1 }}.{{ score.label }}</div>
          <div class="sub">{{ score.sub }}</div>
          <template v-if="submitList[score.value]">
            <div class="score-btn">
              <div
                class="btn"
                :class="{ active: submitList[score.value].score === b }"
                @click="submitList[score.value].score = b"
                v-for="b in score.score"
                :key="b"
              >
                {{ b }}分
              </div>
            </div>

            <van-uploader
              :after-read="afterRead"
              :before-read="beforeRead"
              :before-delete="beforeDelete"
              v-model="submitList[score.value].imgs"
              class="miniImg"
              :name="score.value"
            >
              <div class="upload-mini">
                <img src="../../assets/icon_add.svg" alt="" />
                <div class="text">添加佐证</div>
              </div>
            </van-uploader>
          </template>
        </div>
      </div>
    </div>

    <div class="page-title">
      <div class="text1">输入考察结论</div>
      <div class="text2">补充实地考察结论</div>
    </div>
    <van-field
      type="textarea"
      class="textarea"
      placeholder="请输入…"
      v-model="form.remark"
      :autosize="{ maxHeight: 200, minHeight: 160 }"
      :maxlength="150"
    ></van-field>

    <!-- <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
        @click="save"
        class="preview"
        color="#ffcf6a"
        block
        round
        style="margin-right:12px"
        >暂存</van-button
      >
      <!-- <van-button
        @click="$refs.preview.init()"
        class="preview"
        color="#C4C7CC"
        block
        round
        >预览</van-button
      > -->
      <van-button
        class="submit"
        @click="$refs.preview.init()"
        :color="$colors.prim"
        block
        round
        :disabled="!canSubmit"
        >提交</van-button
      >
    </div>

    <preview-rate
      ref="preview"
      @submit="submit"
      :info="subJson"
      :rate="form"
      :loading="saving"
    ></preview-rate>
  </div>
</template>

<script>
import expert from "../../mixins/expert";
import Compressor from "compressorjs";
import previewRate from "../../components/previewRate.vue";
export default {
  components: { previewRate },
  name: "expertAdd",
  mixins: [expert],
  metaInfo() {
    return {
      title: "上传评分"
    };
  },
  data() {
    return {
      form: {
        remark: "",
        img: [],
        imgs: []
      },
      img: [],
      submitList: {},
      saving: false
    };
  },
  computed: {
    canSubmit() {
      let info = { ...this.submitList };
      return !Object.keys(info).find(item => {
        return info[item].score === 0;
      });
    },
    subJson() {
      let info = { ...this.submitList };
      return Object.keys(info).map(item => {
        return {
          rateId: info[item].rateId,
          type: info[item].type,
          score: info[item].score,
          img: info[item].img
        };
      });
    }
  },
  mounted() {
    let subList = {};

    [...this.categories].forEach(item => {
      item.children.forEach(i => {
        subList[i.value] = {
          rateId: this.$route.query.rateId,
          type: i.value,
          score: 0,
          img: [],
          imgs: []
        };
      });
    });

    this.$http
      .post(
        "/rateExpertAudit/all",
        {
          query: {
            rateId: this.$route.query.rateId
          }
        },
        { body: "json" }
      )
      .then(res => {
        res.content.forEach(item => {
          subList[item.type] = {
            ...item,
            imgs: item.img.map(img => {
              return {
                url: img
              };
            })
          };
        });

        this.submitList = subList;
        // this.rateList = res.content;
      });
  },
  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 =
            detail.name === "big"
              ? [...this.form.img]
              : [...this.submitList[detail.name].img];
          imgs.push(res);
          console.log(detail);
          if (detail.name === "big") {
            this.form.img = imgs;
          } else {
            this.submitList[detail.name].img = imgs;
          }
        });
    },
    beforeDelete(file, detail) {
      let submitForm = {};
      if (detail.name === "big") {
        submitForm = { ...this.form };
      } else {
        submitForm = this.submitList[detail.name];
      }
      var imgs1 = [...submitForm.imgs];
      imgs1.splice(detail.index, 1);
      var imgs2 = [...submitForm.img];
      imgs2.splice(detail.index, 1);
      if (detail.name === "big") {
        this.form.imgs = imgs1;
        this.form.img = imgs2;
      } else {
        this.submitList[detail.name].imgs = imgs1;
        this.submitList[detail.name].img = imgs2;
      }
    },
    beforeRead(file) {
      return new Promise(resolve => {
        // compressorjs 默认开启 checkOrientation 选项
        // 会将图片修正为正确方向
        new Compressor(file, {
          success: resolve,
          error(err) {
            console.log(err.message);
          }
        });
      });
    },
    save() {
      let form = { ...this.form };
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true
      });
      this.$http
        .post("/rateExpertAudit/batchSave", {
          audits: JSON.stringify(this.subJson)
        })
        .then(() => {
          this.$toast.success("成功");
        })
        .catch(e => {
          console.log(e);
          this.$message.error(e.error);
        });
    },
    submit() {
      this.saving = true;
      let form = { ...this.form };
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true
      });
      this.$http
        .post("/rateExpertAudit/batchSave", {
          audits: JSON.stringify(this.subJson)
        })
        .then(() => {
          return this.$http.post(
            "/rate/save",
            {
              id: this.$route.query.rateId,
              status: "SUBMIT_GRADE",
              img: form.img,
              remark: form.remark,
              submit: true
            },
            { 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: 0 16px 100px;
  .page-title {
    padding: 30px 0 16px;
    .text1 {
      font-size: 20px;
      font-weight: bold;
      color: #313233;
      line-height: 28px;
    }
    .text2 {
      font-size: 13px;
      color: #939599;
      line-height: 22px;
    }
  }
}
.score-card + .score-card {
  margin-top: 20px;
}
.score-card {
  border-radius: 12px;
  overflow: hidden;
  background: #fff;

  .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;
  }
  .score-item {
    padding: 20px 0;
    .text1 {
      font-size: 15px;
      font-weight: bold;
      color: #313233;
      line-height: 24px;
    }

    .sub {
      font-size: 13px;
      color: #939599;
      line-height: 22px;
      font-weight: normal;
    }

    .score-btn {
      display: flex;
      align-items: center;
      padding-top: 10px;
      .btn {
        width: 44px;
        height: 44px;
        line-height: 44px;
        background: #ffffff;
        border-radius: 8px;
        border: 1px solid #e6e7eb;
        text-align: center;
        font-size: 12px;

        &:active {
          background-color: darken(#fff, 10);
        }

        &.active {
          background: rgba(44, 150, 255, 0.12);
          color: #2c96ff;
          border: 1px solid #2c96ff;
        }
      }
      .btn + .btn {
        margin-left: 16px;
      }
    }
  }
}

.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>
