<template>
  <div class="page-header-index-wide">
    <a-card :title="title" :loading="loading" v-show="visible" :bordered="false" :body-style="{padding: '0'}">
      <div class="salesCard">
        <a-tabs default-active-key="1" size="large" :tab-bar-style="{marginBottom: '24px', paddingLeft: '16px'}">
          <div class="extra-wrapper" slot="tabBarExtraContent">
            <a-button style="margin-left: 8px" type="default" @click="getData()">查询</a-button>
            <a-button style="margin-left: 8px" type="primary" icon="printer" @click="handlePrint()">打印</a-button>
            <a-button style="margin-left: 8px" type="primary" @click="doExport()">导出</a-button>
            <a-button style="margin-left: 8px" type="primary" @click="handleCancel()">返回</a-button>
          </div>
          <a-tab-pane loading="true" tab="图形统计" key="1">
            <a-row>
              <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
                <div style="padding: 10px">
                  <div id="container2" style="width: 100%;overflow-x:auto"></div>
                </div>
              </a-col>
            </a-row>
          </a-tab-pane>
          <a-tab-pane loading="true" tab="表格统计" key="2">
            <a-row>
              <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
                <div style="padding: 10px">
                  <a-table
                    bordered
                    :data-source="tableData"
                    :columns="columns"
                    tableLayout="auto"
                    :scroll="{x: 1, y: BaseTool.Constant.scrollY }"
                    rowKey="month">
                    <span slot="action" slot-scope="record">
                      <template>
                        <a @click="handleView(record)">查看明细</a>
                        <a-divider type="vertical" />
                        <a @click="doExportDetail(record)">导出</a>
                      </template>
                    </span>
                  </a-table>
                </div>
              </a-col>
            </a-row>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-card>
    <print-in-repair-report-fee ref="basePrintModal" @ok="handleOk"/>
    <detail-repair-report-fee ref="detailModal" @ok="handleOk"/>
  </div>
</template>

<script>
import { getSbInfoReportFee, exportSbInfoReportFee, exportSbInfoReportFeeYear } from '@/api/report/application-form'
import { Chart } from '@antv/g2'
import PrintInRepairReportFee from '@/views/dashboard/modules/PrintInRepairReportFee'
import DetailRepairReportFee from '@/views/dashboard/modules/DetailRepairReportFee'
import moment from 'moment'
// const DataSet = require('@antv/data-set')
export default {
  name: 'RepairReportSbInfoFee',
  components: {
    PrintInRepairReportFee,
    Chart,
    DetailRepairReportFee
  },
  props: {
    sbId: {
      type: String,
      default: null
    },
    title: {
      type: String,
      default: '设备费用分析'
    }
  },
  data () {
    return {
      loading: false,
      serverData: [],
      monthFormat: 'YYYY-MM',
      queryParam: {
        // year: 2021,
        sbId: this.sbId
      },
      visible: false,
      chart: null, // 创建一个chart变量
      chartsData: [],
      typeMap: {},
      tableData: [],
      fields: [],
      // 表头
      columns: [
        {
          title: '年份',
          width: 180,
          dataIndex: 'year'
        },
        {
          title: '费用次数',
          width: 120,
          dataIndex: 'num'
        },
        {
          title: '费用类型',
          width: 120,
          dataIndex: 'type',
          customRender: (text, record, index) => {
            return this.BaseTool.Object.getField(this.typeMap, text)
          }
        },
        {
          title: '寿命',
          width: 120,
          dataIndex: 'workYear'
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ]
    }
  },
  created () {
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_FEE_TYPE)
  },
  mounted () {
    /* this.$nextTick(function () {
      this.getData()
    }) */
  },
  methods: {
    moment,
    base () {
      this.visible = true
      this.getData()
    },
    onStartChange (date, dateString) {
      this.queryParam.startMonth = this.BaseTool.Date.formatter(dateString + '-01', this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
    },
    onEndChange (date, dateString) {
      this.queryParam.endMonth = this.BaseTool.Date.formatter(dateString + '-01', this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
    },
    getData () {
      getSbInfoReportFee(this.queryParam)
        .then(res => {
          this.chartsData = res.data.data
          this.tableData = res.data.dataTable

          // 需要将数据分组：总数，完成数
          /*  const groupData = []
          this.tableData.forEach(function (data) {
            groupData.push({ name: '总数', year: data.year + '', num: data.num })
            groupData.push({ name: '计划数', year: data.year + '', num: data.planNum })
            groupData.push({ name: '非计划数', year: data.year + '', num: data.notPlanNum })
          }) */
          this.tableData.forEach(function (data) {
            data.year = data.year + ''
            console.log('data: ' + data)
          })
          this.getCharts('container2', this.tableData)// 调用统计图
        })
    },
    getCharts (id, data) {
      this.chart && this.chart.destroy()// 防止点击搜索按钮新增一个
      this.chart = new Chart({
        container: id,
        autoFit: true,
        height: 400
      })
      this.chart.data(data)
      this.chart.scale('num', {
        nice: true
      })
      this.chart.tooltip({
        showMarkers: false,
        shared: true
      })
      this.chart.interval().position('year*num').color('name').adjust([
        {
          type: 'dodge',
          marginRatio: 0
        }
      ])
      this.chart.interaction('active-region')
      this.chart.legend({
        position: 'bottom'
      })
      // 折线数据
      this.chart.line().position('year*workYear')
      this.chart.point().position('year*workYear').size(4).shape('circle').style({
        stroke: '#fff',
        lineWidth: 1
      })
      this.chart.render()
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      exportSbInfoReportFee(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    doExportDetail (record) {
      const parameter = {
        ...this.queryParam,
        year: record.year,
        type: record.type
      }
      exportSbInfoReportFeeYear(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handlePrint (record) {
      const modal = this.$refs.basePrintModal
      this.visible = false
      modal.base({ startMonth: this.queryParam.startMonth, endMonth: this.queryParam.endMonth, title: this.title, data: this.chartsData })
    },
    handleView (record) {
      const modal = this.$refs.detailModal
      modal.base(record)
    },
    handleOk () {
      this.visible = true
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
      this.$emit('ok')
    }
  }
}
</script>

<style lang="less" scoped>
.extra-wrapper {
  line-height: 55px;
  padding-right: 24px;

  .extra-item {
    display: inline-block;
    margin-right: 24px;

    a {
      margin-left: 24px;
    }
  }
}

.antd-pro-pages-dashboard-analysis-twoColLayout {
  position: relative;
  display: flex;
  display: block;
  flex-flow: row wrap;
}

.antd-pro-pages-dashboard-analysis-salesCard {
  height: calc(100% - 24px);
  /deep/ .ant-card-head {
    position: relative;
  }
}

.dashboard-analysis-iconGroup {
  i {
    margin-left: 16px;
    color: rgba(0,0,0,.45);
    cursor: pointer;
    transition: color .32s;
    color: black;
  }
}
.analysis-salesTypeRadio {
  position: absolute;
  right: 54px;
  bottom: 12px;
}
</style>
