<template>
  <div @click="openDrawer">
    <slot></slot>
  </div>
  <a-drawer
    :visible="visible"
    class="drawerStyle addonlineDrawer"
    width="1000"
    title="添加在线"
    placement="right"
  >
    <div class="drawerMain">
      <div class="header">
        <div v-if="taskIndex >= 0" class="headerTitle">编辑项目</div>
        <div v-else class="headerTitle">添加项目</div>
        <img
          style="width: 29px; height: 29px; cursor: pointer"
          src="../../assets/images/basicinfo/close.png"
          @click="closeDrawer"
        />
      </div>
      <div class="contentMain">
        <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.name"
                  style="width: 210px; height: 40px; border-radius: 8px"
                  placeholder="请输入项目名称"
                  maxlength="20"
                />
              </div>
            </div>
            <div class="mii_ipt">
              <div class="ipt_name"></div>
              <div class="fi_input">
                <a-input
                  v-model:value="params.manager"
                  style="width: 210px; height: 40px; border-radius: 8px"
                  placeholder="请输入项目经理"
                  maxlength="20"
                />
              </div>
            </div>
            <div class="mii_ipt">
              <div class="ipt_name"></div>
              <div class="fi_input">
                <a-input
                  v-model:value="params.createName"
                  style="width: 210px; height: 40px; border-radius: 8px"
                  placeholder="请输入创建人"
                  maxlength="20"
                />
              </div>
            </div>
          </div>
          <div class="mi_btns" style="margin-left: 0px">
            <div class="btn btn1">
              <div class="search"></div>
              <div class="btnText" @click="search">搜索</div>
            </div>
            <div class="btn btn2" @click="reset">
              <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_item" style="margin-top: 16px; margin-bottom: 32px">
          <button class="xkbtn" @click="goProjectmanage">新建项目</button>
        </div>

        <div
          class="main_notice"
          v-if="taskIndex >= 0"
          style="background-color: #e9f6fe; height: 40px"
        >
          <div class="mntc_left" v-if="selectedRows">
            <div class="notice_icon"></div>
            <div>
              <div>
                <span class="title"
                  >已选择
                  <span class="data" style="color: #4ea6ff">{{ 1 }}</span>
                  条&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span
                >
                <span class="title"
                  >名称：
                  <span
                    class="data"
                    style="color: #4ea6ff; margin-right: 15px"
                    >{{ selectedRows?.name }}</span
                  >
                </span>
                <span class="title"
                  >项目经理：
                  <span
                    class="data"
                    style="color: #4ea6ff; margin-right: 15px"
                    >{{ selectedRows?.manager }}</span
                  >
                </span>
                <span class="title"
                  >创建人：
                  <span
                    class="data"
                    style="color: #4ea6ff; margin-right: 15px"
                    >{{ selectedRows?.createName }}</span
                  >
                </span>
                <span class="title"
                  >创建时间：
                  <span class="data" style="color: #4ea6ff">{{
                    selectedRows?.createTime
                  }}</span>
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="main_table">
          <div class="drawerbox">
            <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>
      <div class="main_btns">
        <button @click="closeDrawer" class="btn2">取消</button>
        <button @click="confirm" class="btn2">确定</button>
      </div>
    </div>
    <!-- 有重复添加的项目时的弹窗 -->
    <a-modal
      v-model:visible="sameModal"
      :footer="null"
      wrapClassName="sameModal"
      centered="true"
    >
      <div class="delete">
        <div class="del_header"></div>
        <div class="del_main">
          <div class="header">
            <div class="icon"></div>
            <span>提示</span>
            <div class="close_exit" @click="closeSameModal"></div>
          </div>
          <div class="body">
            <span>项目已存在，是否重复添加？</span>
          </div>
          <div class="del_btnbox">
            <div class="del_btn btn1" @click="closeSameModal">
              <div class="btnText">取消</div>
            </div>
            <div class="del_btn btn2" @click="sureSameModal">
              <div class="btnText">确定</div>
            </div>
          </div>
        </div>
      </div>
    </a-modal>
  </a-drawer>
</template>
<script setup>
import { computed, defineEmits, defineProps, ref, watch } from "vue";
import { message } from "ant-design-vue";
// import { useRouter } from "vue-router";
import { useRowsPage } from "@/api/request";
import { PROJECT_AUDIT_PAGE } from "@/api/apis";
import { getProjectDetail } from "@/api/index";
const props = defineProps({
  type: Number,
  taskList: [],
});
const visible = ref(false);
const sameModal = ref(false);
const emit = defineEmits({});

const columns = ref([
  {
    title: "项目名称",
    dataIndex: "name",
    key: "projectName",
    width: "20%",
    ellipsis: true,
  },
  {
    title: "所属项目",
    dataIndex: "topName",
    key: "topName",
    width: "20%",
    ellipsis: true,
    customRender: ({ record: { gaName, faName, name } }) => (
      <div
        title={
          faName ? (gaName !== null ? gaName + "/" + faName : faName) : name
        }
      >
        {faName
          ? gaName !== null
            ? (gaName + "/" + faName).length > 16
              ? (gaName + "/" + faName).slice(0, 16) + "..."
              : gaName + "/" + faName
            : faName.length > 16
            ? faName.slice(0, 16) + "..."
            : faName
          : name.length > 16
          ? name.slice(0, 16) + "..."
          : name}
      </div>
    ),
  },
  {
    title: "项目经理",
    dataIndex: "manager",
    key: "manager",
    width: "20%",
    align: "center",
  },
  {
    title: "创建人",
    dataIndex: "createName",
    key: "createName",
    width: "20%",
    align: "center",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    key: "createTime",
    width: "20%",
    align: "center",
  },
]);
const initParams = {
  pageNo: 1,
  pageSize: 10,
  orderAsc: true,
};
const params = ref(initParams);
// const router = useRouter();
const rowSelectKeys = ref([]);
const selectsData = ref([]);
const taskIndex = ref(-1);

const { data, loading, total, fetch } = useRowsPage(
  PROJECT_AUDIT_PAGE,
  params.value,
  {
    init: false,
    result: (res) => res.result.list,
    totalPage: (res) => res.result.totalPages,
    total: (res) => res.result.count,
  }
);
console.log(data);
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.pageNo,
  pageSize: params.value.pageSize,
  onChange: changePagination,
}));

const changePagination = (e) => {
  params.value.pageNo = e;
  fetch();
};
const rowSelection = computed(() => ({
  type: "radio",
  columnWidth: 20,
  selectedRowKeys: rowSelectKeys.value,
  onChange: onSelectChange,
  preserveSelectedRowKeys: true,
}));

function onSelectChange(e, l) {
  rowSelectKeys.value = e;
  selectsData.value = l;
  selectedRows.value = l[0];
  console.log("selectedRows", selectedRows);
}

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

function reset() {
  rowSelectKeys.value = [];
  selectsData.value = [];
  params.value.pageNo = 1;
  params.value.keyWord = "";
  params.value.createName = "";
  params.value.manager = "";
  params.value.name = "";
  fetch();
}

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

function confirm() {
  if (!selectsData.value.length || !rowSelectKeys.value.length) {
    message.warning("请选择项目！");
    return;
  }
  if (taskIndex.value === -1) {
    const list = props.taskList;
    list.push({
      name: selectsData.value[0].name,
      type: props.type,
      courseId: selectsData.value[0].id,
      info: { ...selectsData.value[0] },
    });
  } else {
    const data = props.taskList[taskIndex.value];
    data.name = selectsData.value[0].name;
    data.courseId = selectsData.value[0].id;
    data.info = selectsData.value[0];
  }
  emit("update:taskList", [...props.taskList]);
  closeDrawer();
}

const goProjectmanage = () => {
  // router.push({path: "/projectmanage"});
  window.open(process.env.VUE_APP_BASE + "/projectmanage");
};

function closeSameModal() {
  sameModal.value = false;
}

function sureSameModal() {}
let selectedRows = ref(null);
function openDrawer(i, row) {
  console.log("row", row);

  row && (rowSelectKeys.value = [row.courseId]);
  row && (selectsData.value = [{ name: row.name, id: row.courseId }]);
  i >= 0 && (taskIndex.value = i);
  visible.value = true;

  //获取项目详情
  if (row && row.courseId) {
    getProjectDetail({
      projectId: row.courseId,
    }).then((res) => {
      console.log("获取选中项目详情", res);
      if (res.data.code === 200) {
        selectedRows.value = res.data.data.projectInfo;
        console.log("selectedRows", selectedRows);
      }
    });
  }
}

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

.sameModal {
  .ant-modal {
    width: 424px !important;
    height: 258px !important;

    .ant-modal-content {
      width: 424px !important;
      height: 258px !important;

      .ant-modal-body {
        width: 424px !important;
        height: 258px !important;
        padding: 0 !important;

        .delete {
          z-index: 999;
          width: 424px;
          height: 258px;
          background: #ffffff;
          box-shadow: 0px 1px 35px 0px rgba(118, 136, 166, 0.21);
          border-radius: 4px;

          // position: absolute;
          // left: 50%;
          // top: 10%;
          // transform: translate(-50%, -50%);
          .del_header {
            position: absolute;
            width: calc(100%);
            height: 68px;
            background: linear-gradient(
              rgba(78, 166, 255, 0.2) 0%,
              rgba(78, 166, 255, 0) 100%
            );
          }

          .del_main {
            width: 100%;
            position: relative;

            .header {
              display: flex;
              align-items: center;
              padding-top: 20px;
              padding-left: 26px;
              font-size: 16px;

              .icon {
                width: 16px;
                height: 16px;
                margin-right: 10px;
                background-image: url(@/assets/images/taskpage/gan.png);
                background-size: 100% 100%;
              }

              .close_exit {
                position: absolute;
                right: 42px;
                cursor: pointer;
                width: 20px;
                height: 20px;
                background-image: url(@/assets/images/coursewareManage/close.png);
                background-size: 100% 100%;
              }
            }

            .body {
              width: 100%;
              margin: 34px auto 56px auto;
              display: flex;
              justify-content: center;
              align-items: center;
              flex-direction: column;
              // background-color: red;
              position: relative;

              .back {
                position: absolute;
                top: 30px;
                font-size: 12px;
                font-weight: 400;
                color: #666666;
              }
            }

            .del_btnbox {
              display: flex;
              margin: 30px auto;
              justify-content: center;

              .del_btn {
                width: 100px;
                height: 40px;
                background: rgba(64, 158, 255, 0);
                border-radius: 8px;
                display: flex;
                align-items: center;
                justify-content: center;

                flex-shrink: 0;
                cursor: pointer;

                .btnText {
                  font-size: 14px;
                  font-weight: 400;
                  line-height: 40px;
                }
              }

              .btn1 {
                border: 1px solid rgba(64, 158, 255, 1);
                color: #4ea6ff;
                margin-right: 14px;
              }

              .btn2 {
                background-color: #4ea6ff;
                color: #ffffff;
              }
            }
          }
        }
      }
    }
  }
}

.addrefDrawer {
  .drawerMain {
    // .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;
    //     .title {
    //       color: rgba(0, 0, 0, 0.65);
    //       margin-right: 17px;
    //     }
    //     .data {
    //       color: #4ea6ff;
    //     }
    //     .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;
    //   }
    // }
    .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 {
      display: flex;
      justify-content: space-between;

      .main_left {
        margin-top: 32px;
        padding-right: 30px;
        flex: 1;
        border-right: 1px solid #e8e8e8;

        .main_item {
          display: flex;
          align-items: center;
          margin-top: 32px;
          margin-bottom: 32px;

          .signbox {
            width: 120px;
            display: flex;
            justify-content: end;
            align-items: center;

            .sign {
              margin-right: 5px;
            }
          }

          .btnbox {
            display: flex;
            flex: 1;
            align-items: center;

            .ant-input {
              height: 100%;
            }

            .xkbtn {
              cursor: pointer;
              width: 130px;
              height: 40px;
              background: #4ea6ff;
              border-radius: 8px;
              border: 0;
              margin-right: 8px;
              color: #fff;
            }
          }
        }

        .main_item2 {
          display: flex;
          align-items: flex-start;
          margin-bottom: 32px;

          .signbox {
            width: 120px;
            display: flex;
            justify-content: end;
            align-items: center;

            .sign {
              margin-right: 5px;
            }
          }

          .textarea {
            width: 423px;

            .ant-input {
              width: 100%;
            }

            .ant-input-textarea-show-count {
              position: relative;
            }

            .ant-input-textarea-show-count::after {
              position: absolute;
              right: 10px;
              bottom: 0px;
            }

            .ant-input {
              border-radius: 8px;
            }
          }
        }
      }

      .tableBox {
        // margin: 20px 38px 30px;
        margin: 10px 35px 0px 35px;

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

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

      .tableBox {
        padding-bottom: 20px;

        .pa {
          // position: absolute;
          // bottom: 20px;
          // left: 0;
          width: 100%;
          // height: 20px;
          // background-color: red;
          display: flex;
          justify-content: center;

          // margin-bottom: 10px;
          // position: absolute;
          // bottom: -40px;
          .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
          }
        }
      }
    }

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

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

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