<!-- 事项甘特图 -->
<template>
  <div class="gantt-chart">
    <div class="condition-legend-bar">
      <Select 
        class="system-select"
        width="217"
        :isReadOnly="true" 
        tipPlace="top" 
        caption="系统名称："
        v-model="systemId" 
        :selectdata="systemList" 
        :placeholder="'请选择'"
        @change="getGanttChartData"
      />
      <div class="legend-container">
        <div v-for="(item) in legends" :key="'key_' + item.id" class="item-legend">
          <div :style="{'background': item.backgroundColor, 'border-color': item.borderColor}"></div>
          <div>{{item.text}}</div>
        </div>
        <div >
          <el-radio-group v-model="timeType">
            <el-radio style="line-height: 32px;margin-right: 16px;" label="month">月视图</el-radio>
            <el-radio style="line-height: 32px;" label="day">日视图</el-radio>
          </el-radio-group>
        </div>
      </div>
    </div>
    <div class="main-gantt-container">
      <div class="task-title">
        <div class="title">任务标题</div>
        <Tree :data="treeData" @openNode="toggleNode" />
      </div>
      <div class="chart-container" id="chartContainer" ref="chartContainer">
        <div id="ganttContainer"></div>
      </div>
      <el-dialog :title="detailTitle" :visible.sync="showDetail" width="840px">
        <div class="dialog-container">
          <div class="row task-status">
            <div>
              <Task class="icon"/>
              <div class="title">任务状态</div>
            </div>
            <div>进行中</div>
          </div>
          <div class="row">
            <div>
              <PlanTime class="icon" />
              <div class="title">计划时间</div>
            </div>
            <div class="info">{{this.ganttDetail.planTime}}</div>
          </div>
          <div class="row">
            <div>
              <RealTime class="icon" />
              <div class="title">实际执行时间</div>
            </div>
            <div class="info">{{this.ganttDetail.realTime}}</div>
          </div>
          <div class="row picture">
            <div>
              <Picture class="icon" />
              <div class="title">照片</div>
            </div>
            <div>
              <div>
                <div class="num">12张</div>
                <div class="more">查看更多</div>
              </div>
              <div class="pictrue-container">
                <div v-for="(item) in pictures" :key="'id_' + item.id">
                  <img :src="item.imgSrc" alt="" />
                  <div class="info">{{item.title}}</div>
                </div>
              </div>
            </div>
          </div>
          <div class="row picture">
            <div>
              <Report class="icon" />
              <div class="title">报告</div>
            </div>
            <div>
              <div>
                <div class="num">2张</div>
              </div>
              <div class="pictrue-container">
                <div v-for="(item) in reports" :key="'id_' + item.id">
                  <img :src="item.imgSrc" alt="" />
                  <div class="info">{{item.title}}</div>
                </div>
              </div>
            </div>
          </div>
          <div class="row test-content">
            <div>
              <Menu class="icon" />
              <div class="title">检测内容</div>
            </div>
            <el-table :data="tableData">
              <el-table-column property="time" label="设备简称"></el-table-column>
              <el-table-column property="evenType" label="设备编号"></el-table-column>
              <el-table-column property="objid" label="设备数"></el-table-column>
              <el-table-column property="content" label="空间位置"></el-table-column>
            </el-table>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { queryGanttChart, queryGanttDetail } from '../../api/ganttChart';
import { querySystemList, queryDetailData } from '../../api/coreDeviceReport';
import { Tree } from 'meri-design';
import _ from 'lodash';
import moment from 'moment';
// import '../../utils/ganttChart/g.js';
import { GanttChart } from '../../utils/ganttChart/GanttChart.js';
import { parse } from 'path';
import Task from '../../assets/svg/task.svg';
import PlanTime from '../../assets/svg/plan_time.svg';
import RealTime from '../../assets/svg/real_time.svg';
import Picture from '../../assets/svg/picture.svg';
import Report from '../../assets/svg/report.svg';
import Menu from '../../assets/svg/menu.svg';
export default {
  data () {
    return {
      systemList: [], // 系统列表
      systemId: '', // 系统名称
      legends: [
        {id: 1, text: '按时完成', backgroundColor: '#E7E9EA', borderColor: '#C3C7CB'},
        {id: 2, text: '未开始/进行中', backgroundColor: 'rgba(91, 143, 249, 0.2)', borderColor: '#5B8FF9'},
        {id: 3, text: '逾期完成', backgroundColor: '#FBCE99', borderColor: '#F58300'},
        {id: 4, text: '逾期未完成', backgroundColor: '#FBB8B5', borderColor: '#F54E45'},
      ], // 图例
      timeType: 'month', // 甘特图时间类型
      treeData: [], // 树结构数据
      
      // 弹窗相关
      showDetail: false, // 弹框显示状态
      detailTitle: '', // 弹窗标题
      pictures: [
        {id: 1, imgSrc: require('../../assets/imgs/analysis/picture.png'), title: '2019年5月高压用具检测报告.jpg'},
        {id: 2, imgSrc: require('../../assets/imgs/analysis/picture.png'), title: '2019年5月高压用具检测报告.jpg'},
      ], // 照片
      reports: [
        {id: 1, imgSrc: require('../../assets/imgs/analysis/report_pdf.png'), title: '2019年5月高压用具检测报告.pdf'},
        {id: 2, imgSrc: require('../../assets/imgs/analysis/report_word.png'), title: '2019年5月高压用具检测报告.docx'},
      ], // 报告
      ganttDetail: {}, // 甘特图明细
      tableData: [], // 表数据
    }
  },

  components: {
    Task,
    PlanTime,
    RealTime,
    Picture,
    Report,
    Menu
  },

  computed: {},

  mounted() {
    this.getSystemList();
  },

  methods: {
    /**
     * 获取系统列表数据
     */
    getSystemList() {
      querySystemList().then((res) => {
        if (res.result === 'success') {
          let data = res.data;
          let newData = [];
          _.forEach(data, (item) => {
            newData.push({
              id: item.code,
              name: item.name
            })
          })
          this.systemList = newData;
          const { query } = this.$route;
          if (!_.isEmpty(query) && query.smsxt) {
            this.systemId = query.smsxt;
          } else {
            this.systemId = newData[0].id;
          }
          this.getGanttChartData();
        }
      })
    },
    /**
     * 处理请求数据的时间
     */
    disQueryTime() {
      let startTime, endTime;
      let curTime = new Date(),
          curYear = curTime.getFullYear(),
          curMonth = curTime.getMonth() + 1;
      if (this.timeType === 'month') {
        startTime = `${curYear}0101000000`;
        endTime = `${curYear + 1}0101000000`;
      } else {
        startTime = `${curYear}${curMonth}01000000`;
        if (curMonth + 1 > 12) {
          endTime = `${curYear + 1}0101000000`;
        } else {
          endTime = `${curYear}${curMonth + 1}01000000`;
        }
      }
      return [startTime, endTime];
    },
    /**
     * 获取甘特图数据
     */
    getGanttChartData() {
      let time = this.disQueryTime();
      let param = {
        smsxt: this.systemId,
        plazaId: 1000423,
        startDate: 20000101000000, // time[0]
        endDate: 20200708000000, // time[1]
      }
      queryGanttChart('/data/base/queryGanttChart', param).then((res) => {
        const { dsfList, wbList, zwList, result } = res;
          console.log('res', res)
        if (result === 'success') {
          let newData = [],
              data = [wbList, zwList, dsfList],
              name = ['维保', '专维', '第三方检测'];
          _.forEach(data, (item, index) => {
            newData.push({
              id: index + 1,
              name: name[index],
              open: true,
              // children: name[index] === '专维'?[] : this.disGanttData(dsfList)
              children: this.disGanttData(data[index])
            })
          })
          
          this.treeData = newData;
          
          let taskData = _.map(_.cloneDeep(newData), (item) => {
            return {
              id: item.id,
              type: item.name,
              open: item.open,
              dataList: _.map(item.children, (_item) => {
                return {
                  id: _item.id,
                  title: _item.name,
                  tasks: _item.originData
                }
              })
            }
          })
          console.log('taskData', taskData)
          let width = this.$refs.chartContainer.offsetWidth;
          window.gc = new GanttChart({
            chartParentContainer: 'chartContainer',
            chartContainer: 'ganttContainer',
            viewWidth: width,
            cWidth: 3*width,
            tasks: taskData,
            daysCount: parseInt(3*width/40),
            callback: this.showDialog
          });
          gc.main()
        }
      })
    },
    /**
     * 处理甘特图数据
     */
    disGanttData(arr) {
      let data = [];
      _.forEach(arr, (item) => {
        data.push({
          id: item.id,
          name: item.name,
          checked: 'uncheck',
          originData: _.map(item.data, (_item) => {
            _item.startDate = moment.unix((new Date().getTime() - 1000*60*60*24*7)/1000).format('YYYY-MM-DD HH:mm:ss');
            _item.endDate = moment.unix(new Date().getTime()/1000).format('YYYY-MM-DD HH:mm:ss');
            _item.description = _item.parentname;
            return _item
          })
        })
      })
      return data
    },
    /**
     * 展开收起节点
     */
    toggleNode(status) {
      console.log('status', status)

    },
    /**
     * 显示弹窗
     */
    showDialog(task) {
      this.showDetail = true;
      this.detailTitle = task._pdata.description;
      this.getGanttDetailData();
      this.getPictureOrReportData(0);
      this.getPictureOrReportData(1);
    },
    /**
     * 获取甘特图详情
     */
    getGanttDetailData() {
      let param = {
        plazaId: 1000423,
        type: 2,
        id: 1078467
      }
      queryGanttDetail('/data/base/queryGanttChartDetails', param).then((res) => {
        const { result, data } = res;
        if (result === 'success') {
          const { status, planStartDate, planEndDate, realStartDate, realEndDate } = data;
          let newData = {
            status,
            color: '',
            planTime: this.dealDetailTime(planStartDate, planEndDate),
            realTime: this.dealDetailTime(realStartDate, realEndDate)
          }
          this.ganttDetail = newData;
        }
      })
    },
    /**
     * 处理时间
     */
    dealDetailTime(startTime, endTime) {
      let start = moment.unix(startTime / 1000).format('YYYY.MM.DD HH:mm:ss'),
          end = moment.unix(endTime / 1000).format('YYYY.MM.DD HH:mm:ss');
      return `${start}-${end}`
    },
    /**
     * 获取甘特图的图片/报告详情
     */
    getPictureOrReportData(type) {
      let param = {
        file_type: 1,
        file_type_id: 1985,
        type: type
      }
      queryDetailData('/data/base/queryFileDetails', param).then((res) => {
        console.log('photo', res)

      })
    }
  }
}
</script>

<style lang='less' scoped>
.gantt-chart{
  padding-left: 16px;
  padding-right: 16px;
  padding-top: 12px;
  padding-bottom: 16px;
  width: 100%;
  height: 100%;
  background: #fff;
  .condition-legend-bar{
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
  }
  .legend-container{
    display: flex;
    .item-legend{
      padding-left: 18px;
      position: relative;
      font-size: 14px;
      color: #646C73;
      line-height: 32px;
      &:not(:nth-of-type(4)){
        margin-right: 20px;
      }
      >div:first-child{
        position: absolute;
        left: 0;
        top: calc(50% - 6px);
        width: 12px;
        height: 12px;
        border: 1px solid;
      }
      &:nth-of-type(4){
        margin-right: 32px;
      }
    }
  }
  .main-gantt-container{
    width: 100%;
    height: calc(100% - 44px);
    background: #F2F5F7;
    border:1px solid rgba(239,240,241,1);
    border-radius:4px;
    display: flex;
    .task-title{
      width: 404px;
      height: 100%;
      border-right: 1px solid #EFF0F1;
      .title{
        padding-left: 32px;
        padding-top: 15px;
        padding-bottom: 15px;
        font-size: 14px;
        color: #646C73;
        line-height: 19px;
        border-bottom: 1px solid #EFF0F1;
      }
    }
    .chart-container{
      flex: 1;
      overflow: hidden;
      background: #fff;
    }
  }
  .dialog-container{
    max-height: 580px;
    overflow: auto ;
    .row{
      display: flex;
      >div:first-child{
        margin-right: 24px;
        display: flex;
        align-items: center;
      }
      &:not(:last-of-type){
        margin-bottom: 14px;
      }
      .info{
        font-size: 14px;
        color: #1F2429;
        line-height: 22px;
      }
    }
    .icon{
      margin-right: 6px;
      width: 16px;
    }
    .title{
      width: 84px;
      font-size: 14px;
      color: #646C73;
      line-height: 22px;
      font-weight: 400;
    }
    .task-status>div:nth-of-type(2){
      width: 58px;
      height: 24px;
      background: rgba(91,143,249,0.2);
      border-radius: 2px;
      color: #5B8FF9;
      line-height: 24px;
      text-align: center;
    }
    .picture{
      margin-bottom: 19px!important;
      >div:first-child{
        align-items: baseline;
      }
      >div:nth-of-type(2){
        flex: 1;
      }
      >div:nth-of-type(2)>div:first-child{
        margin-bottom: 12px;
        display: flex;
      }
      .num,
      .more{
        font-size: 14px;
        line-height: 22px;
        font-weight: 400;
      }
      .num{
        margin-right: 22px;
        color: #1F2429;
      }
      .more{
        color: #025BAA;
        cursor: pointer;
      }
    }
    .pictrue-container{
      >div{
        padding: 10px 16px;
        width: 100%;
        border-radius:4px;
        border:1px solid rgba(233,233,233,1);
        display: flex;
        align-items: center;
        >img{
          width: 24px;
          margin-right: 15px;
        }
      }
      >div:not(:last-child){
        margin-bottom: 8px;
      }
    }
    .test-content{
      display: block;
      >div:first-child{
        margin-bottom: 13px;
      }
    }
  }
}
</style>