<template>
  <div class="templatelibrary">
    <div class="tmpl">
      <div class="tmpl_header">
        <div class="tmplh_inp">
          <div class="inpbox">
            <div class="inpbox1">
              <span>模版名称：</span>
              <a-input v-model:value="value1" style="
                  width: 270px;
                  height: 40px;
                  border-radius: 8px;
                  margin-right: 14px;
                " placeholder="请输入模板名称" />
            </div>
            <!--
            <div class="inpbox1">
              <span>创建时间：</span>
              <a-date-picker v-model:value="time" type="date" placeholder="创建时间" style="
                width: 270px;
                height: 40px; 
                border-radius: 8px;
                margin-right: 14px;
                " />
            </div>
-->

          </div>
        </div>
        <div class="tmplh_btn">
          <div class="btn btn1" @click="searchLevel">
            <div class="search"></div>
            <div class="btnText btnText1">搜索</div>
          </div>
          <div class="btn btn2" @click="resetLevel">
            <div class="search"></div>
            <div class="btnText btnText2">重置</div>
          </div>
        </div>
      </div>
      <div class="tmpl_body">
        <div class="tableBox">
          <a-table :columns="columns1" :data-source="tableData1"
            expandRowByClick="true" @expand="expandTable" :pagination="false"/>
            <div class="tableBox" style="display:flex;justify-content:center;padding:20px;">
     
              <div class="pa">
                <a-pagination
                  v-if="tableDataTotal > 10"
                  :showSizeChanger="false"
                  showQuickJumper="true"
                  hideOnSinglePage="true"
                  :pageSize="pageSize"
                  v-model:current="currentPage"
                  :total="tableDataTotal"
                  class="pagination"
                  @change="changePagination"
                />
              </div>
            </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { reactive, defineComponent, toRefs, onMounted } from "vue";
import { message } from "ant-design-vue";
import { useRouter } from "vue-router";
import * as api from "@/api/indexTemplate"

const columns1 = [
  {
    title: "模版名称",
    width: "30%",
    dataIndex: "name",
    key: "name",
    ellipsis: true,
    align: "center",
  },
  {
    title: "状态",
    width: "15%",
    dataIndex: "status",
    key: "status",
    align: "center",
  },
  {
    title: "创建人",
    width: "15%",
    dataIndex: "creator",
    key: "creator",
    align: "center",
  },
   {
     title: "创建时间",
     width: "20%",
     dataIndex: "createTime",
     key: "createTime",
     align: "center",
   },
   //{
   //  title: "发布时间",
   //  width: "15%",
   //  dataIndex: "publishTime",
   //  key: "publishTime",
   //  align: "center",
  // },
  {
    title: "操作",
    width: "20%",
    className: "h",
    dataIndex: "operation",
    key: "operation",
    align: "center",
  },
];
export default defineComponent({
  name: "TemplateLibrary",
  setup() {
    // 编辑页面跳转过来时候，自动填充表格
    const router = useRouter();
    const state = reactive({
      value1: null,
      time:null,
      currentPage: 1,
      pageSize: 10,
      total:0,
      tableDataTotal:0,
      tableData1: [
        {
        
        }
      ],
    });
    const getTableDate1 = () => {
      let data = state.tableData1;
      data.map((value) => {
        {
          //单层项目
          value.operation = (
            <div class="operation" style="justify-content: flex-end; margin-right:65px;">
              <div class="nSelect">
                {value.status === "已发布" ? (
                  <div class="nselect">
                    <div class="ops2">
                        <div class="jc" onClick={() => {toLibraryAdd(value.projectTemplateId)}}>
                          查看{" "}
                          <span style="color:#E9E9E9;margin-left:15px;">|</span>
                        </div>
                    </div>
                    <div class="ops3">
                      <div class="jc"
                        onClick={() => { templateHadler({ "projectTemplateId": value.projectTemplateId, "type": 0 }) }}
                      >撤回</div>
                    </div>
                  </div>
                ) : (
                  <div></div>
                )}
                {value.status === "未发布" ? (
                  <div class="nselect">
                    <div class="ops1">
                      <div class="jc"
                        onClick={() => { templateHadler({ "projectTemplateId": value.projectTemplateId, "type": 1 }) }}>
                        发布
                        <span style="color:#E9E9E9;margin-left:15px;">|</span>
                      </div>
                    </div>
                    <div class="ops2">
                        <div class="jc" onClick={() => {toLibraryAdd(value.projectTemplateId)}}>
                          查看
                          <span style="color:#E9E9E9;margin-left:15px;">|</span>
                        </div>
                    </div>
                    <div class="ops3">
                      <div class="jc"
                        onClick={() => { templateHadler({ "projectTemplateId": value.projectTemplateId, "type": -1 }) }}
                      >删除</div>
                    </div>
                  </div>
                ) : (
                  <div></div>
                )}
              </div>
            </div>
          );
        }
      });
      state.tableData = data;
    };
    const pagination = reactive({
      current: state.currentPage,
      total: state.total,
      defaultPageSize: state.pageSize,
      onChange: (id) => { pagination.current = id; getLibraryList() },
    })
    getTableDate1();

    onMounted(() => {
      getLibraryList();
    })

     // 翻页
     const changePagination = (page) => {
      pagination.current = page;
      getLibraryList();
    };

    // 获取模板列表
    const getLibraryList = () => {
      let date = Math.ceil(new Date(state.time).getTime() / 1000);
     
      let obj = {
      //  "create_time": state.time,
        "createTime" : date==0?null:date,
        "name": state.value1,
        "pageNo": pagination.current,
        "pageSize": state.pageSize
      }
      api.templateList(obj).then((res) => {
        state.tableData1 = []
        state.tableDataTotal = res.data.data.total;
        console.log(res);
        let resData = res.data.data.rows;
        state.total =res.data.data.total;
        if (resData.length) {
          for (let i in resData) {
            state.tableData1.push({
              id : i + 1,
              projectTemplateId:resData[i].projectTemplateId,
              status:resData[i].status ? "已发布" : "未发布",
              name:resData[i].name,
              creator:resData[i].createName,
              publishTime:resData[i].publishTime,
              createTime:resData[i].createTime,
              operation:"operation"
            })
          }
          getTableDate1()
        } else {
          message.warning("获取的列表数据为空值")
        }
      }).catch((error) => {
        message.error(`获取模板列表失败` + error)
      });
    };
    const templateHadler = (obj) => {
      api.handleTemplates(obj).then((res) => {
        if (res.data.success) {
          message.success(`模板${obj.type == 1 ? '发布' : obj.type == 0 ? '撤回' : '删除'}成功`)
          getLibraryList();
        }
      }).catch((error) => {
        message.error(`模板${obj.type == 1 ? '发布' : obj.type == 0 ? '撤回' : '删除'}失败` + error)
      })
    }
    const searchLevel = () => {
      getLibraryList();
      /*
      setTimeout(() => {
        const result = state.tableData1.filter(item => item.name.includes(state.value1))
        state.tableData1 = result;
      },100)
      */
    };
    const resetLevel = () => {
      state.value1 = '';
      state.time = '';
      getLibraryList();
      getTableDate1()
    };
    const toLibraryAdd = (id) => {
      router.push("/libraryAdd");
      localStorage.setItem("projectTemplateId",id);
    }

    return {
      ...toRefs(state),
      columns1,
      templateHadler,
      searchLevel,
      resetLevel,
      pagination,
      toLibraryAdd,
      changePagination
    };
  },
});
</script>
<style lang="scss">
.templatelibrary {
  width: 100%;

  .tmpl {
    width: 100%;

    .tmpl_header {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      margin-bottom: 32px;

      margin-left: 32px;
      margin-right: 32px;

      .tmplh_inp {
        .inpbox {
          display: flex;
          margin-top: 32px;

          .inpbox1 {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-right: 24px;

            span {
              white-space: nowrap;
            }
          }
        }
      }

      .tmplh_btn {
        display: flex;
        margin-left: 38px;
        margin-top: 32px;

        .btn {
          padding: 0px 26px 0px 26px;
          height: 38px;
          border-radius: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-left: 14px;
          cursor: pointer;
          flex-shrink: 0;

          .search {
            background-size: 100%;
          }

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

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

          .btnText2 {
            color: rgba(64, 158, 255, 1);
          }
        }

        .btn1 {
          background: #4ea6ff;

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

        .btn2 {
          background: #ffffff;
          border: 1px solid #4ea6ff;

          .search {
            width: 16px;
            height: 18px;
            background-image: url("../../assets/images/courseManage/reset1.png");
          }
        }

        .btn1:hover {
          background: rgb(255, 255, 255);
          border: 1px solid #4ea6ff;

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

          .btnText {
            color: rgba(64, 158, 255, 1);
          }
        }

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

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

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

    .tmpl_body {
      padding: 0px 30px;

      .tableBox {
        .operation {
          display: flex;
          justify-content: center;
          align-items: center;
          color: #4ea6ff;

          .nselect {
            justify-content: center;
            align-items: center;
            display: flex;

            .jc {
              margin-left: 20px;
              white-space: nowrap;
              cursor: pointer;
            }
          }
        }
      }
    }
    .tableBox {
    margin: 20px 38px 30px;

    .ant-table-thead > tr > th {
    
      background-color: #eff4fc;
    }
  }
  }
}
</style>
