<template>
  <div class="item-manager-wrap">
    <bread-crumb :curmbFirst="curmbFirst" :curmbSecond="curmbSecond"></bread-crumb>
    <div class="component-content screenSet" id="screenSet">
      <div class="header-title">项目管理</div>
      <el-row :gutter="30" class="row" type="flex" style="margin-top: 10px;">
        <el-form ref="formInline" :model="formInline" label-width="75px" style="width:100%;">
          <el-col :span="5">
            <el-form-item label="项目名称：">
              <el-input v-model="formInline.projectName" size="small" placeholder="请输入项目名称"></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="项目时间：">
              <el-date-picker
                v-model="formInline.duringTime"
                size="small"
                type="daterange"
                start-placeholder="请选择开始时间"
                end-placeholder="请选择结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
              ></el-date-picker>
            </el-form-item>
          </el-col>-->
          <el-col :span="6">
            <el-form-item label="项目时间：">
              <el-date-picker
                v-model="formInline.projectBegintime"
                size="small"
                type="datetime"
                placeholder="请选择开始时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptions0"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="~">
              <el-date-picker
                v-model="formInline.projectEndtime"
                size="small"
                type="datetime"
                placeholder="请选择结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptions1"
                default-time="23:59:59"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding:0;text-align:right;padding-right:15px;">
            <el-button type="primary" size="small" @click="searchList">查询</el-button>
            <el-button type="default" size="small" @click="resetForm" style="margin-left:0;">重置</el-button>
          </el-col>
        </el-form>
      </el-row>
      <el-row>
        <el-col :span="5">
          <el-form ref="formInline" :model="formInline" label-width="75px" style="width:100%;">
            <el-form-item label="发布状态：">
              <el-select size="small" v-model="formInline.projectStatus" placeholder="请选择发布状态">
                <el-option
                  v-for="item in optionStatus"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-form :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-button class="add-button" :disabled="idType == 2 && masterAdministratorFlag == 'false'" size="small" type="primary" @click="toPage()">新建项目</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID编号" min-width="100" align="center"></el-table-column>
        <el-table-column prop="projectName" label="项目名称" min-width="100" align="center"></el-table-column>
        <el-table-column prop="createdName" label="创建人" align="center"></el-table-column>
        <el-table-column
          prop="projectIntro"
          label="项目简介"
          align="center"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="projectBegintime" label="项目开始时间" min-width="90" align="center"></el-table-column>
        <el-table-column prop="projectEndtime" label="项目结束时间" min-width="90" align="center"></el-table-column>
        <el-table-column prop="projectStatus" label="状态" align="center">
          <template slot-scope="scope">
            <span>{{ [scope.row.projectStatus , idType] | statusProject }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="200" align="center">
          <template slot-scope="scope">
            <el-button
              @click="changeStatus(scope.row,0)"
              v-show="showButton(scope.row,0)"
              type="primary"
              size="small"
            >编辑</el-button>
            <el-button
              @click="changeStatus(scope.row,1)"
              v-show="showButton(scope.row,1)"
              type="primary"
              size="small"
            >暂存</el-button>
            <el-button
              @click="changeStatus(scope.row,2)"
              v-show="showButton(scope.row,2)"
              type="primary"
              size="small"
            >完成</el-button>
            <el-button
              @click="changeStatus(scope.row,3)"
              v-show="showButton(scope.row,3)"
              type="primary"
              size="small"
            >上架</el-button>
            <el-button
              @click="changeStatus(scope.row,4)"
              v-show="showButton(scope.row,4)"
              type="primary"
              size="small"
            >通过</el-button>
            <el-button
              @click="changeStatus(scope.row,5)"
              v-show="showButton(scope.row,5)"
              type="primary"
              size="small"
            >下架</el-button>
            <el-button
              @click="changeStatus(scope.row,7)"
              v-show="showButton(scope.row,7)"
              type="primary"
              size="small"
              :disabled="scope.row.checkStatus==1"
            >提醒审核</el-button>
            <el-button
              @click="changeStatus(scope.row,8)"
              v-show="showButton(scope.row,8)"
              type="primary"
              size="small"
            >取消审核</el-button>
            <el-button
              @click="changeStatus(scope.row,6)"
              v-show="showButton(scope.row,6)"
              type="primary"
              size="small"
            >拒绝</el-button>
            <el-button
              @click="delItem(scope.row,9)"
              v-show="showButton(scope.row,9)"
              type="primary"
              size="small"
            >删除</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <div class="table-empty">
            <img src="../../assets/image/no-content1.png">
            <p>没有查询到相关结果</p>
          </div>
        </div>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="formInline.pageNo"
          :page-sizes="[10, 30, 50, 100]"
          :page-size="formInline.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalRows"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import BreadCrumb from "../../components/breadcrumb.vue";
import { create } from "domain";
import { mapGetters } from 'vuex'
import { returnData } from "../../utils/mock";
import { openLoading, closeLoading } from "../../utils/utils";
import * as commonUtil from "../../utils/utils";
import { setTimeout } from 'timers';
let vm = null;
export default {
  components: {
    BreadCrumb
  },
  data() {
    return {
      idValue: '',
      curmbFirst: "教培项目",
      curmbSecond: "项目管理",
      searchParam: {
        name: "",
        status: "",
        pageNo: 1,
        pageSize: 10
      },
      optionStatus: [
        {
          value: 1,
          label: "草稿"
        },
        {
          value: 2,
          label: "未上架"
        },
        {
          value: 3,
          label: "审核中/待审核"
        },
        {
          value: 4,
          label: "已上架"
        },
        {
          value: 5,
          label: "已下架"
        },
        {
          value: 6,
          label: "已拒绝/未上架"
        }
      ],
      tableData: [],
      totalRows: 0,
      loading: true,
      timingTime: "",
      currentPage: 1,
      formInline: {
        projectName: "",
        projectBegintime: "",
        projectEndtime: "",
        projectStatus: "",
        pageNo: 1,
        pageSize: 10
      },
      pickerOptions0: {
        disabledDate: time => {
          if (this.formInline.projectEndtime != "" && this.formInline.projectEndtime !=null) {
            return (
              time.getTime() >
              new Date(this.formInline.projectEndtime).getTime() 
            );
          }
        }
      },
      pickerOptions1: {
        disabledDate: time => {
          return (
            time.getTime() <
            new Date(this.formInline.projectBegintime).getTime() 
          ); //减去一天的时间代表可以选择同一天;
        }
      },
      idType: '',
      masterAdministratorFlag: false
    };
  },
  computed: {
    ...mapGetters([
      "_token", 
      // "idType",
      // "masterAdministratorFlag"
    ])
  },
  created() {
    vm = this;
    vm.idType = localStorage.getItem('storageIdType');
    vm.masterAdministratorFlag = localStorage.getItem('storageMaster');
    console.log('idType:',vm.idType);
    this.$nextTick(function() {;
      this.getProjectList();
    });
  },
  // 挂载到Dom完成时
  mounted: function() {
    commonUtil.resizeHeight();
  },
  methods: {
    toPage() {
      this.$router.push("add-manager");
    },
    searchList() {
      this.formInline.pageNo = 1;
      this.getProjectList();
    },
    //查询项目列表
    getProjectList() {
      let req = {};
      req = this.formInline;
      openLoading(vm);
      vm.GET("portal/portalInfo/getProjectList", req).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          vm.tableData = res.data.data;
          //console.log(vm.tableData);
          vm.totalRows = res.data.totalRows;
        }
      });
    },
    //按钮展示情况
    showButton(row, type) {
      // 按钮type类型： 0-编辑
      // 1-暂存 2-完成 3-上架 4-通过 5-下架 6-拒绝
      // 7- 提醒审核 8(2)-取消审核 9-删除
      let projectStatus = row.projectStatus;
      let idType = row.idType;
      let level = row.level;
      let flag = false;
      let typeStr = parseInt(type);
      //console.log("状态："+projectStatus+ "等级：" + level + " 按钮类型：" + type);
      //console.log('idType',idType);
      let buttonStatus = "";
      if (idType == 1) {
        //内部项目
        buttonStatus = returnData().buttonStatusIn;
      } else {
        //外部项目
        buttonStatus = returnData().buttonStatusOut;
      }
      if (level != null) {
        let statusSet = buttonStatus[projectStatus - 1][level];
        let flagValue = statusSet.indexOf(typeStr);
        if (flagValue >= 0) {
          flag = true;
        } else {
          flag = false;
        }
      }
      //flag = true;
      return flag;
    },
    //改变状态
    changeStatus(row, type) {
      let projectId = row.id;
      let level = row.level;
      if (type === 0) {
        //编辑
        this.$router.push("add-manager?projectId=" + projectId +"&level="+level);
      } else if (type === 7) {
        //提醒审核
        let req = {
          portalProjectId: projectId,
          warnType: 1
        };
        vm.GET("portal/portalCheck/remindPortalCheck", req).then(res => {
          vm.$message.info(res.message);
          vm.getProjectList();
          if (res.code == "000000") {
            console.log(res);
            //vm.getProjectList();
          }
        });
      } else {
        // if (type === 8) {
        //   //取消审批值对应2
        //   type = 2;
        //   let req = {
        //     portalProjectId: projectId,
        //     warnType: 5
        //   };
        //   vm.GET("portal/portalCheck/remindPortalCheck", req).then(res => {
        //     vm.$message.info(res.message);
        //     if (res.code == "000000") {
        //       console.log(res);
        //       vm.getProjectList();
        //     }
        //   });
        // }
        // if (type == 4 || type == 6) {
        //   //发送消息
        //   let warnType = "";
        //   if (type == 4) {
        //     warnType = 3;
        //   } else if (type == 6) {
        //     warnType = 4;
        //   }
        //   let req = {
        //     portalProjectId: projectId,
        //     warnType: warnType
        //   };
        //   vm.GET("portal/portalCheck/remindPortalCheck", req).then(res => {
        //     vm.$message.info(res.message);
        //     if (res.code == "000000") {
        //       console.log(res);
        //       vm.getProjectList();
        //     }
        //   });
        // }
        let req = {
          projectId: projectId,
          changeStatus: vm.getType(type)
        };
        vm.POST("portal/portalInfo/changeStatus", req).then(res => {
          vm.$message.info(res.message);
          if (res.code == "000000") {
            console.log(res);
            vm.getProjectList();

            //changeStatus之后remindPortalCheck
            if (type == 4 || type == 6 || type == 8) {
              //发送消息
              let warnType = "";
              if (type == 4) {
                warnType = 3;
              } else if (type == 6) {
                warnType = 4;
              } else if (type == 8) {
                warnType = 5;
              }
              let req = {
                portalProjectId: projectId,
                warnType: warnType
              };
              vm.GET("portal/portalCheck/remindPortalCheck", req).then(res => {
                vm.$message.info(res.message);
                if (res.code == "000000") {
                  vm.getProjectList();
                }
              });
            }
          }
        });
      }
    },
    getType(value) {
      let data = value;
      if(data == 8) {
        data = 2;
      }
      return data;
    },
    //删除项目
    delItem(row) {
      let req = {};
      console.log(row);
      vm.DELETE("portal/portalInfo/delete/" + row.id, req).then(res => {
        vm.$message.info(res.message);
        if (res.code == "000000") {
          console.log(res);
          vm.getProjectList();
        }
      });
    },
    //重置表单
    resetForm() {
      vm.formInline = Object.assign(
        {},
        {
          projectName: "",
          projectBegintime: "",
          projectEndtime: "",
          projectStatus: "",
          pageNo: 1,
          pageSize: 10,
          duringTime: ""
        }
      );
      vm.getProjectList();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.formInline.pageSize = val;
      this.handleCurrentChange(this.searchParam.pageNo);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.formInline.pageNo = val;
      this.getProjectList();
      //需要判断是否检索
      // if (!this.flag) {
      //   this.currentChangePage(this.tableDataEnd);
      // } else {
      //   this.currentChangePage(this.filterTableDataEnd);
      // }
    }, //组件自带监控当前页码
    currentChangePage(list) {
      let from = (this.currentPage - 1) * this.pageSize;
      let to = this.currentPage * this.pageSize;
      this.tableDataEnd = [];
      for (; from < to; from++) {
        if (list[from]) {
          this.tableDataEnd.push(list[from]);
        }
      }
    }
  }
};
</script>
<style lang="scss">
.item-manager-wrap {
  .component-content {
    background: #fff;
    //margin-top: 88px;
    padding: 10px;
    .header-title {
      padding: 10px 12px;
      font-size: 12px;
      color: #449284;
      border-bottom: 1px solid #efefef;
    }
    // .table-option span {
    //   color: #409eff;
    // }
    .add-button {
      float: right;
    }
    .table-empty {
      img {
        width: 100px;
      }
      p {
        margin-top: -50px;
      }
    }
  }
}
</style>