<template>
  <div @click="openDrawer">
    <slot></slot>
  </div>
  <a-drawer
    :visible="visible"
    class="drawerStyle addonlineDrawer"
    width="800"
    title="选择测评"
    placement="right"
  >
    <div class="drawerMain">
      <div class="header">
        <div class="headerTitle">选择测评</div>
        <img
          style="width: 29px; height: 29px; cursor: pointer"
          src="../../assets/images/basicinfo/close.png"
          @click="closeDrawer"
        />
      </div>
      <div class="contentMain" v-if="data.length > 0">
        <div class="main_items">
          <div class="mi_ipts">
            <div class="mii_ipt">
              <div class="ipt_name"></div>
              <div class="fi_input">
                <a-input
                  v-model:value="params.keyword"
                  style="width: 424px; height: 40px; border-radius: 8px"
                  placeholder="请输入测评名称"
                />
              </div>
            </div>
          </div>
          <div class="mi_btns">
            <div class="btn btn2" @click="search">
              <div class="search"></div>
              <div class="btnText">搜索</div>
            </div>
            <div class="btn btn2" @click="reset">
              <div class="search"></div>
              <div class="btnText">重置</div>
            </div>
          </div>
        </div>
        <div class="main_table">
          <a-table
            :customRow="customRow"
            class="ant-table-striped"
            :row-class-name="
              (_, index) => (index % 2 === 1 ? 'table-striped' : null)
            "
            row-key="id"
            :columns="columns"
            :data-source="data"
            :loading="loading"
            :pagination="pagination"
            :row-selection="rowSelection"
          />
        </div>
      </div>
      <div class="contentMain nolimits" v-else>
        <img style="width: 700px" src="../../assets/images/evImg.png" />
      </div>
      <div class="main_btns">
        <button class="btn2" @click="closeDrawer">取消</button>
        <button class="btn2" @click="confirm">确定</button>
      </div>
    </div>
  </a-drawer>
</template>
<script setup>
import { computed, defineEmits, defineProps, ref, watch } from "vue";
import { useBoeApiPage } from "@/api/request";
import { TEST_PAGE } from "@/api/ThirdApi";
import { message } from "ant-design-vue";
import { useStore } from "vuex";
// const toolList = ref([
//   {
//     name: ["能力类", "测验工具"],
//     children: [
//       {
//         text1: "职场准备度测评",
//         text2: "无工作经验学生",
//         text3: "职业能力成熟度测评",
//         text4: "五年以内工龄员工",
//       },
//       {
//         text1: "职业认知能力测评",
//         text2: "五年内工龄员工",
//         text3: "商业推荐测评",
//         text4: "五年内工龄员工",
//       },
//       {
//         text1: "沟通能力测评",
//         text2: "五年内工龄员工",
//         text3: "情商测评",
//         text4: "各层级人员",
//       },
//       {
//         text1: "管理行为自检问卷",
//         text2: "基/中/高层管理者",
//         text3: "管理者情境判断测评",
//         text4: "基/中高层管理者",
//       },
//     ],
//   },
//   {
//     name: ["性格/风格", "测验工具"],
//     children: [
//       {
//         text1: "PDP性格类型测验",
//         text2: "各层级人员",
//       },
//       {
//         text1: "大五职业性格测评",
//         text2: "各层级人员",
//       },
//       {
//         text1: "领导风格测验",
//         text2: "各层级人员",
//       },
//     ],
//   },
//   {
//     name: ["动机/价值观", "测验工具"],
//     children: [
//       {
//         text1: "动机测验（内隐&外显）",
//         text2: "各层级人员",
//       },
//       {
//         text1: "职业兴趣测验",
//         text2: "各层级人员",
//       },
//     ],
//   },
// ]);
const props = defineProps({
  taskList: [],
  evaluationTypeId: String,
  evaluationTypeName: Number,
});
const visible = ref(false);
const emit = defineEmits({});

const columns = ref([
  {
    title: "测评编号",
    width: "20%",
    dataIndex: "quiz_code",
    key: "code",
    ellipsis: true,
  },
  {
    title: "测评名称",
    width: "20%",
    dataIndex: "title",
    key: "title",
    align: "center",
    ellipsis: true,
  },
  {
    title: "备注",
    width: "40%",
    dataIndex: "theme_desc",
    key: "content",
    align: "center",
    ellipsis: true,
  },
  {
    title: "可答题人数",
    width: "20%",
    dataIndex: "quiz_range",
    key: "time",
    align: "center",
    ellipsis: true,
  },
]);

const store = useStore();

const initParams = {
  keyword: "",
  page: 1,
  size: 10,
  user_id: store.state.userInfo.id,
  // orderAsc: true
};
const params = ref(initParams);

const rowSelectKeys = ref([]);
const selectsData = ref([]);
const taskIndex = ref(-1);

const { data, loading, total, fetch } = useBoeApiPage(TEST_PAGE, params.value, {
  init: false,
  result: (res) => {
    console.log("获取测评", res);
    let newArr = [];
    for (let i = 0; i < res.result.data.length; i++) {
      res.result.data[i].id = res.result.data[i].quiz_kid;
      newArr.push(res.result.data[i]);
    }
    return newArr;
  },
  totalPage: (res) => res.result.total_page_num,
  total: (res) => res.result.count,
});

watch(taskIndex, () => {
  if (taskIndex.value >= 0) {
    rowSelectKeys.value = [props.taskList[taskIndex.value].courseId];
    selectsData.value =
      data.value.find(
        (t) => t.id === props.taskList[taskIndex.value].courseId
      ) || {};
  }
});

const customRow = (record) => ({
  onClick: () => {
    rowSelectKeys.value = [record.id];
    selectsData.value = [record];
  },
});

const pagination = computed(() => ({
  total: total.value,
  showSizeChanger: false,
  current: params.value.pageIndex,
  pageSize: params.value.pageSize,
  onChange: changePagination,
}));

const changePagination = (e) => {
  params.value.pageIndex = e;
  fetch();
};
const rowSelection = computed(() => ({
  type: "radio",
  columnWidth: 20,
  selectedRowKeys: rowSelectKeys.value,
  onChange: onSelectChange,
  preserveSelectedRowKeys: true,
  getCheckboxProps: getCheckboxProps,
}));
const getCheckboxProps = () => ({
  // 某几项默认禁止选中(R: 当state等于1时)
  disabled: false,
});

function onSelectChange(e, l) {
  rowSelectKeys.value = e;
  selectsData.value = l;
}

function search() {
  params.value.pageIndex = 1;
  fetch();
}

function reset() {
  rowSelectKeys.value = [];
  selectsData.value = [];
  params.value.pageIndex = 1;
  params.value.keyWord = "";
  fetch();
}

const closeDrawer = () => {
  visible.value = false;
  taskIndex.value = -1;
  reset();
};

function confirm() {
  if (!selectsData.value.length || !rowSelectKeys.value.length) {
    message.warning("请选择测评！");
    return;
  }
  // if (taskIndex.value === -1) {
  //   let list = props.taskList
  //   console.log(props,222,list);
  //   if(list!=undefined){
  //     list.push({
  //     name: selectsData.value[0].title,
  //     type: 3,
  //     courseId: selectsData.value[0].id,
  //     info: {...selectsData.value[0]}
  //   })
  //   }
  //
  // } else {
  //   const data = props.taskList[taskIndex.value]
  //   data.name = selectsData.value[0].title
  //   data.courseId = selectsData.value[0].id
  //   data.info = selectsData.value[0]
  // }
  // if(props.taskList!=undefined){
  //   emit('update:taskList', [...props.taskList])
  // }
  emit("update:evaluationTypeId", selectsData.value[0].id);
  emit("update:evaluationTypeName", selectsData.value[0].title);
  closeDrawer();
}

function openDrawer(i) {
  i >= 0 && (taskIndex.value = i);
  visible.value = true;
}

defineExpose({ openDrawer });
</script>
<style lang="scss">
.ant-table-striped :deep(.table-striped) td {
  background-color: #fafafa !important;
}

.addonlineDrawer {
  .drawerMain {
    .header {
      height: 73px;
      border-bottom: 1px solid #e8e8e8;
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-shrink: 0;

      .headerTitle {
        font-size: 18px;
        font-weight: 600;
        color: #333333;
        line-height: 25px;
        margin-left: 24px;
      }
    }

    .contentMain {
      padding-right: 12px;

      .main_items {
        display: flex;

        margin-bottom: 12px;
        flex-wrap: wrap;

        .mi_ipts {
          display: flex;
          margin-bottom: 20px;

          .mii_ipt {
            display: flex;
            align-items: center;
            margin-right: 24px;

            .ipt_name {
              white-space: nowrap;
            }
          }
        }

        .mi_btns {
          display: flex;

          cursor: pointer;

          .btn {
            padding: 0px 26px 0px 26px;
            height: 38px;
            border-radius: 8px;
            border: 1px solid rgba(64, 158, 255, 1);
            display: flex;
            align-items: center;
            justify-content: center;
            margin-left: 14px;
            flex-shrink: 0;

            .search {
              background-size: 100%;
            }

            .btnText {
              font-size: 14px;
              font-weight: 400;
              line-height: 36px;
              margin-left: 5px;
            }
          }

          .btn1 {
            background: rgb(64, 158, 255);

            .search {
              width: 15px;
              height: 17px;
              background-image: url("@/assets/images/coursewareManage/search0.png");
            }

            .btnText {
              color: rgb(255, 255, 255);
            }
          }

          .btn2 {
            background: rgb(64, 158, 255);

            .search {
              width: 15px;
              height: 17px;
              background-image: url("@/assets/images/coursewareManage/reset0.png");
            }

            .btnText {
              color: rgb(255, 255, 255);
            }
          }

          // .btn1:hover {
          //   background: rgb(255, 255, 255);

          //   .search {
          //     background-image: url("@/assets/images/courseManage/search1.png");
          //   }

          //   .btnText {
          //     color: #4ea6ff;
          //   }
          // }

          // .btn2:hover {
          //   background: rgba(64, 158, 255, 1);

          //   .search {
          //     background-image: url("@/assets/images/courseManage/reset0.png");
          //   }

          //   .btnText {
          //     color: #ffffff;
          //   }
          // }
        }
      }

      .main_notice {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 32px;
        height: 40px;
        background-color: #e9f6fe;

        .mntc_left {
          display: flex;
          align-items: center;

          .notice_icon {
            width: 14px;
            height: 14px;
            margin-right: 9px;
            margin-left: 9px;
            background-image: url(@/assets/images/coursewareManage/gan.png);
            background-size: 100% 100%;
          }
        }

        .mntc_right {
          cursor: pointer;
        }
      }

      .main_table {
        position: relative;
        padding-bottom: 80px;

        .classify {
          margin-left: 10px !important;
          padding-left: 9px !important;
        }

        .ant-checkbox-wrapper {
          align-items: center;
          margin-top: -2px;
        }

        .ant-table-selection-column {
          padding: 0px !important;
          padding-left: 5px !important;
        }

        .ant-table-thead > tr > th {
          background-color: rgba(239, 244, 252, 1);
        }

        th.h {
          background-color: #eff4fc !important;
        }

        .ant-table-tbody
          > tr:hover:not(.ant-table-expanded-row):not(.ant-table-row-selected)
          > td {
          background: #f6f9fd;
        }

        .pa {
          left: 0;
          width: 100%;
          display: flex;
          justify-content: center;
          position: absolute;
          bottom: 20px;
        }
      }
    }
    .nolimits {
      display: flex;
      flex-direction: column;
      align-items: center;
      .limitstext1 {
        margin-top: 30px;
        font-size: 23px;
        color: #333333;
        font-weight: 600;
      }
      .limitstext2 {
        margin-top: 30px;
        font-size: 18px;
        color: #333333;
        font-weight: 800;
      }
      .limitsRow {
        display: flex;
        align-items: center;
        margin-top: 20px;
        width: 100%;
        .limitsRowDrop {
          width: 8px;
          height: 8px;
          background-color: #333333;
          border-radius: 4px;
          margin-left: 20px;
          margin-right: 20px;
          flex-shrink: 0;
        }
        .limitsRowText {
          display: flex;
          flex-wrap: wrap;
          font-size: 16px;
          color: #333333;
          font-weight: 400;
          line-height: 28px;
        }
      }
      .tool {
        width: 100%;
        border-bottom: 1px dashed rgba(0, 78, 126);
        display: flex;
        margin-top: 30px;
        align-items: center;
        padding-bottom: 30px;
        .toolName {
          width: 100px;
          height: 100px;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          color: #fff;
          font-size: 16px;
          font-weight: 600;

          border-radius: 5px;
          margin-left: 20px;
          margin-right: 20px;
        }
        .toolEvery1 {
          text-align: center;
          font-size: 16px;
          color: rgba(71, 74, 76);
          margin-right: 12px;
          line-height: 26px;
        }
      }
    }

    .main_btns {
      height: 72px;
      width: 100%;
      bottom: 0;
      left: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      box-shadow: 0px 1px 35px 0px rgba(118, 136, 166, 0.16);

      .btn1 {
        width: 100px;
        height: 40px;
        border: 1px solid #4ea6ff;
        border-radius: 8px;
        color: #4ea6ff;
        background-color: #fff;
        cursor: pointer;
      }

      .btn2 {
        cursor: pointer;
        width: 100px;
        height: 40px;
        background: #4ea6ff;
        border-radius: 8px;
        border: 0;
        margin-left: 15px;
        color: #fff;
      }
    }
  }
}
</style>