<template>
  <el-dialog
    :close-on-click-modal="false"
    class="dialogSeal"
    title="选择签章"
    :visible.sync="dialogVisibleZ"
    width="400px"
  >
    <el-radio-group v-model="sealMd5">
      <el-radio
        v-for="item in sealList"
        :label="item.sealMd5"
        :key="item.sealMd5"
        >{{ item.sealName }}</el-radio
      >
    </el-radio-group>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" class="iconfont iconqueren" @click="getSealData"
        >确 定</el-button
      >
      <el-button
        type="primary"
        class="iconfont iconquxiao"
        @click="
          dialogVisibleZ = false;
          $emit('sealError');
        "
        >取 消</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { userSealList } from "@/api/seal/index";
export default {
  props: ["sealId"],
  data() {
    return {
      sealList: [],
      dialogVisibleZ: false,
      sealMd5: "",
    };
  },
  methods: {
    getData() {
      userSealList().then((res) => {
        if (res.infoCode == 200) {
          if (res.data.length > 0) {
            this.dialogVisibleZ = true;
            this.sealList = res.data;
            this.sealMd5 = res.data[0].sealMd5;
          } else {
            this.$message({
              type: "waring",
              message: "请联系管理员获取签章权限！",
            });
          }
        }
      });
    },
    getSealData() {
      // this.$emit("getSeal", this.sealMd5);

      this.dialogVisibleZ = false;
      this.signatureTodo(this.sealMd5);
    },
    // new签章
    async signatureTodo(val) {
      let protectedItems = ["protectedItems"];

      let data = {
        keysn: val,
        scaling: 1,
        keysnType: 0,
        protectedItems: protectedItems,
        position: this.sealId,
        documentId: this.sealId,
      };
      this.$signature(data);
      // alert(2);
      this.$prompt("请输入签章密码", "提示", {
        closeOnClickModal: false,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /.*[^\s]/,
        inputErrorMessage: "请输入签章密码",
      })
        .then(({ value }) => {
          data.signPassword = value;
          console.log(data.signPassword);

          this.$preservSignature(data);
          let gInterTime = 0;
          let _this = this;
          //其他浏览器
          gInterTime = setInterval(function () {
            //取newstring的返回值做判断：0失败 1成功
            if (sessionStorage.getItem("sealFlag") === "0") {
              //盖章失败
              clearInterval(gInterTime);
              _this.$message({
                type: "error",
                message: sessionStorage.getItem("sealFlagMes"),
              });
              _this.$emit("sealError");

              sessionStorage.setItem("sealFlag", "");
              sessionStorage.setItem("sealImgData", "");
              // var box=document.getElementById("box");
              // box.parentNode.removeChild(box);
            } else if (
              sessionStorage.getItem("sealFlag") === "1" &&
              sessionStorage.getItem("sealImgData")
            ) {
              clearInterval(gInterTime);
              _this.$emit("sealTodo", sessionStorage.getItem("sealImgData"));
              // _this.sealTodo(
              //   sessionStorage.getItem("sealImgData"),
              //   "sealImgData"
              // );
              sessionStorage.setItem("sealFlag", "");
              sessionStorage.setItem("sealImgData", "");
            } else {
              //为空则一直轮询，直到sealFlag有返回值
            }
          }, 500);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
          this.$emit("sealError");
        });
    },
  },
};
</script>
<style scoped lang="scss">
.dialogSeal /deep/ .el-radio {
  margin: 0 30px 10px 0 !important;
}
</style>