<template>
  <div class="systemManage">
    <div
      style="
        width: 130px;
        height: 40px;
        background: #388be1;
        border-radius: 8px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        line-height: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
      "
      @click="showDrawer"
    >
      添加投票
    </div>
    <div>
      <add-vote v-model:addvoteVisible="visible" />
    </div>
    <a-radio v-model:checked="checked" @click="changeRadio">Option A</a-radio>
    <a-upload
      v-model:file-list="fileList"
      name="avatar"
      list-type="picture-card"
      class="avatar-uploader"
      :show-upload-list="false"
      :before-upload="beforeUpload"
    >
      <!-- <img v-if="imageUrl" :src="imageUrl" alt="avatar" />
      <div v-else>
        <loading-outlined v-if="loading"></loading-outlined>
        <plus-outlined v-else></plus-outlined>
        <div class="ant-upload-text">Upload</div>
      </div> -->
    </a-upload>
  </div>
</template>
<script>
import { reactive, toRefs } from "vue";
import AddVote from "../../components/drawers/AddVote";
// import { PlusOutlined, LoadingOutlined } from "@ant-design/icons-vue";
import { message } from "ant-design-vue";
import * as api from "../../api/index1";
// function getBase64(img, callback) {
//   const reader = new FileReader();
//   reader.addEventListener("load", () => callback(reader.result));
//   reader.readAsDataURL(img);
// }
export default {
  name: "SystemManage",
  components: {
    AddVote,
    // LoadingOutlined,
    // PlusOutlined,
  },
  setup() {
    const state = reactive({
      visible: false,
      checked: true,
      imageUrl: "",
      loading: false,
      fileList: [],
    });

    const showDrawer = () => {
      state.visible = true;
    };
    const changeRadio = () => {
      state.checked = false;
    };
    const handleChange = (info) => {
      if (info.file.status === "uploading") {
        console.log("uploading", info.file);
        state.loading = true;
        return;
      }

      if (info.file.status === "done") {
        console.log("done", info.file);
        let obj = {
          file: info.file,
        };
        api
          .uploadFile(obj)
          .then((res) => {
            console.log("res", res);
          })
          .then((err) => {
            console.log("err", err);
          });
        // Get this url from response in real world.
        // getBase64(info.file.originFileObj, (base64Url) => {
        //   state.imageUrl = base64Url;
        //   state.loading = false;
        // });
      }

      if (info.file.status === "error") {
        state.loading = false;
        message.error("upload error");
      }
    };

    const beforeUpload = (file) => {
      console.log("file", file);
      const isJpgOrPng =
        file.type === "image/jpeg" || file.type === "image/png";

      if (!isJpgOrPng) {
        message.error("You can only upload JPG file!");
      }

      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isLt2M) {
        message.error("Image must smaller than 2MB!");
      }
      if (isJpgOrPng && isLt2M) {
        let obj = {
          file: file,
        };
        api
          .uploadFile(obj)
          .then((res) => {
            console.log("res", res);
          })
          .then((err) => {
            console.log("err", err);
          });
        // Get this url from response in real world.
        // getBase64(info.file.originFileObj, (base64Url) => {
        //   state.imageUrl = base64Url;
        //   state.loading = false;
        // });
      }
      // return isJpgOrPng && isLt2M;
    };

    return {
      ...toRefs(state),
      showDrawer,
      changeRadio,
      handleChange,
      beforeUpload,
    };
  },
};
</script>
<style lang="scss">
.systemManage {
  width: 100%;
  .ant-input-textarea-show-count {
    position: relative;
  }
  .ant-input-textarea-show-count::after {
    position: absolute;
    right: 0px;
    bottom: 0px;
  }
}
.drawerStyle {
  .drawerMain {
    .header {
      height: 73px;
      border-bottom: 1px solid #e8e8e8;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .headerTitle {
        font-size: 18px;
        font-weight: 600;
        color: #333333;
        line-height: 25px;
        margin-left: 24px;
      }
    }
    .contentMain {
      .vote {
        display: flex;
        align-items: center;
        margin-top: 32px;
        margin-left: 20px;
        .votebox {
          width: 100px;
          display: flex;
          align-items: center;
          justify-content: flex-end;
          // background-color: pink;
          .voteimg {
            width: 10px;
            height: 10px;
            margin-right: 5px;
          }
          .votename {
            font-size: 14px;
            font-weight: 400;
            color: #333333;
            line-height: 20px;
          }
        }

        .votebtn {
          width: 130px;
          height: 40px;
          background: #388be1;
          border-radius: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          font-size: 14px;
          font-weight: 400;
          color: #ffffff;
          line-height: 20px;
        }
        .voteclassify {
          font-size: 14px;
          font-weight: 400;
          color: #999999;
          line-height: 20px;
          margin-top: 16px;
        }
        .ant-picker {
          width: 424px;
        }
      }
    }
  }
}
</style>
