<template>
  <a-drawer
    :visible="addinvistVisible"
    class="drawerStyle addinvistDrawer"
    width="70%"
    title="添加评估"
    placement="right"
    @after-visible-change="afterVisibleChange"
  >
    <div class="drawerMain">
      <div class="header">
        <div v-if="edit" class="headerTitle">编辑评估</div>
        <div v-else class="headerTitle">添加评估{{assessmentName}}{{assessmentId}}</div>
        <img
          style="width: 29px; height: 29px; cursor: pointer"
          src="../../assets/images/basicinfo/close.png"
          @click="closeDrawer"
        />
      </div>
      <div class="contentMain">
        <div class="main">
          <div class="main_left">
            <AssessmentAll
                      v-model:assessmentVisible="assessmentVisible"
                      v-model:assessmentId="assessmentId"
                      v-model:assessmentName="assessmentName"
                     
                    />

          </div>
         
        </div>
    
      </div>
      <div class="main_btns">
            <button class="btn1" @click="closeDrawer">取消</button>
            <button class="btn2" @click="updateTask">确定</button>
          </div>
    </div>
  </a-drawer>
</template>
<script>
import { reactive, toRefs } from "vue";
import * as apiTask from "../../api/indexTaskadd";
import { message } from "ant-design-vue";
import {
  RouterEditTask,
  IsExistence,
  IsExistenceProject,
  IsExistenceProjectTemplate,
} from "@/api/indexTask";

import { addTempTask } from "../../api/indexTaskadd";
import AssessmentAll from "@/components/drawers/AssessmentAll.vue";
export default {
  name: "AddInvist",
  components: {
    AssessmentAll,
  },
  props: {
    addinvistVisible: {
      type: Boolean,
      default: false,
    },
    EditInvistId: {
      type: Number,
      default: null,
    },
    edit: {
      //  是否为编辑
      type: Boolean,
      default: null,
    },
    projectId: {
      type: Number,
      default: null,
    },
    chooseStageId: {
      type: Number,
      default: null,
    },
    routerTaskId: {
      type: Number,
      default: 0,
    },
    isLevel: {
      // 是否是关卡页面触发
      type: Number,
      default: null,
    },

    projectTaskId: {
      // 要编辑的projectId
      type: Number,
      default: null,
    },
    routerId: {
      type: Number,
      default: null,
    },
    isactive: {
      type: Number,
      default: null,
    },
    projectTemplateId: {
      type: Number,
      default: null,
    },
  },
  setup(props, ctx) {
    const state = reactive({
      inputV1: "",
      time: undefined,
      currentPage: 1,
      pageSize: 10,
      tableDataTotal: 0,
      tableData: [],
      assessmentId: null,
      assessmentName: "",
      assessment: null,
      assessment1: null,
      assessmentVisible:false,
      titleTag: false,
    });
   // const getCheckedAss = (ass) => {
     // state.assessmentId = ass.assessmentId;
     // state.assessment = ass;
     // console.log("state.assessment1",state.assessment);
   //  
   // };
  
    const closeDrawer = () => {
      ctx.emit("update:addinvistVisible", false);
      ctx.emit("update:edit", false);
      state.assessmentId = null;
      state.assessmentName = null;
      state.inputV1 = "";
      // ctx.emit("changeData", false);
      localStorage.setItem("stageId", props.chooseStageId);
      localStorage.setItem("chapterId", props.isactive);
     
    };
    const checkAssDrawer = () => {
      state.assessmentVisible = true;
    };
    const afterVisibleChange = (bool) => {
      if(bool && props.EditInvistId >0){
        state.assessmentVisible = true;
        state.assessmentId = props.EditInvistId;
      }else{
        state.assessmentId =null;
        state.assessmentName =null;
      }
   
    };
   
    const updateTask = () => {
      console.log("state.assessmentId ",state.assessmentId ,state.assessmentName );
      if (!state.assessmentId) {
        return message.warning("请选择评估");
      }
      let l_data_id = [];
      l_data_id.push(Number(state.assessmentId))
      console.log("state.assessment",state.assessment,state.assessmentId);
      if (props.isLevel == 1) {
        IsExistence({
          chapterId: Number(props.isactive),
          courseTaskId:l_data_id,
          routerId: props.routerId,
          type: 11,
        })
          .then((res) => {
            console.log('shifouchongfu',res)
            console.log("路径图中是否包含此评估了", res);
            if (res.data.data.length) {
              let strdata = res.data.data;
              let tipStr = "";
              for (let i = 0; i < strdata.length; i++) {
                if (i == strdata.length - 1) {
                  tipStr += strdata[i].courseName;
                } else {
                  tipStr += strdata[i].courseName + "/";
                }
              }
              message.destroy();
              message.warning("评估（" + tipStr + "）重复添加");
              return;
            } else {
              RouterEditTask({
                chapterId: props.isactive,
                courseTaskId: state.assessmentId,
                name: state.assessmentName,
                routerId: props.routerId,
                routerTaskId: props.routerTaskId || 0,
                type: 11,
              })
                .then(() => {
                  message.destroy();
                  message.success(`${props.edit ? "编辑" : "新增"}关卡任务成功`);
                  ctx.emit("changeData", false);
                  closeDrawer();
                  state.addLoading = false;
                })
                .catch(() => {
                  message.destroy();
                  message.error(`${props.edit ? "编辑" : "新增"}关卡任务失败`);
                });
            }
          }).catch(err=>{
            console.log(err)
          })

       
      } else if (props.isLevel == 2) {
        IsExistenceProject({
          courseTaskId: l_data_id,
          projectId: props.projectId,
          stageId: props.chooseStageId || 0,
          type: 11,
        }).then(res=>{
          console.log(res)
          console.log("项目中是否包含此评估了", res);
            if (res.data.data.length) {
              let strdata = res.data.data;
              let tipStr = "";
              for (let i = 0; i < strdata.length; i++) {
                if (i == strdata.length - 1) {
                  tipStr += strdata[i].courseName;
                } else {
                  tipStr += strdata[i].courseName + "/";
                }
              }
              message.destroy();
              message.warning("评估（" + tipStr + "）重复添加");
              return;
            } else {
              apiTask
              .addTask({
                courseId: state.assessmentId,
                name: state.assessmentName,
                projectId: props.projectId,
                projectTaskId: props.projectTaskId || 0,
                stageId: props.chooseStageId || 0,
                type: 11,
              })
              .then(() => {
                message.success(`${props.edit ? "编辑" : "新增"}任务成功`);
                ctx.emit("changeData", false);
                closeDrawer();
              })
              .catch(() => {
                message.error(`${props.edit ? "编辑" : "新增"}任务失败`);
              });
            }
        }).catch(err=>{
          console.log(err)
        })
        
      } else if (props.isLevel == 3) {
        IsExistenceProjectTemplate({
          courseTaskId: l_data_id,
          projectTemplateId: props.projectTemplateId,
          stageId: props.chooseStageId || 0,
          type: 11,
        }).then((res)=>{
          console.log(res)
          console.log("项目中是否包含此评估了", res);
            if (res.data.data.length) {
              let strdata = res.data.data;
              let tipStr = "";
              for (let i = 0; i < strdata.length; i++) {
                if (i == strdata.length - 1) {
                  tipStr += strdata[i].courseName;
                } else {
                  tipStr += strdata[i].courseName + "/";
                }
              }
              message.destroy();
              message.warning("评估（" + tipStr + "）重复添加");
              return;
            }else{
          addTempTask({
          courseId: state.assessmentId,
          name: state.assessmentName,
          projectTemplateId: props.projectTemplateId,
          projectTaskId: props.projectTaskId || 0,
          stageId: props.chooseStageId || 0,
          type: 11,
        })
          .then(() => {
            message.destroy();
            message.success(`${props.edit ? "编辑" : "新增"}任务成功`);
            ctx.emit("changeData", false);
            closeDrawer();
          })
          .catch(() => {
            message.destroy();
            message.error(`${props.edit ? "编辑" : "新增"}任务失败`);
          });
            }
        });

      }
    };

    return {
      ...toRefs(state),
      afterVisibleChange,
      closeDrawer,
      updateTask,
      checkAssDrawer,
    
    };
  },
};
</script>
<style lang="scss">
.ant-table-striped :deep(.table-striped) td {
  background-color: #fafafa !important;
}
.addinvistDrawer {
  .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 {
    
      .main {
        width: 100%;
        .main_left {
          padding-right: 30px;
          margin-top: 32px;
          .main_item {
            display: flex;
            align-items: center;
          
            .fi_input {
              margin-right: 20px;
            }
            .btns {
              margin-right: 20px;
              padding: 0px 26px 0px 26px;
              height: 38px;
              background: #4ea6ff;
              border-radius: 8px;
              //border: 1px solid rgba(64, 158, 255, 1);
              display: flex;
              align-items: center;
              justify-content: center;
              margin-right: 14px;
              flex-shrink: 0;
              cursor: pointer;
              .search {
                width: 15px;
                height: 17px;
                background-image: url("../../assets/images/courseManage/search0.png");
                background-size: 100% 100%;
              }
              .btnText {
                font-size: 14px;
                font-weight: 400;
                color: #ffffff;
                line-height: 36px;
                margin-left: 5px;
              }
            }
            .btnsn {
              padding: 0px 26px 0px 26px;
              height: 38px;
              background: #ffffff;
              border-radius: 8px;
              border: 1px solid rgba(64, 158, 255, 1);
              display: flex;
              align-items: center;
              justify-content: center;
              margin-right: 14px;
              flex-shrink: 0;
              cursor: pointer;
              .search {
                width: 16px;
                height: 18px;
                background-image: url("../../assets/images/courseManage/reset1.png");
                background-size: 100% 100%;
              }
              .btnText {
                font-size: 14px;
                font-weight: 400;
                color: #4ea6ff;
                line-height: 36px;
                margin-left: 5px;
              }
            }
          }
          .main_notice {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom:3px;
            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;
            }
          }
        }
      }
    }
 
    .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>
