<template>
  <div>
    <div class="gutter-example">
      <a-row :gutter="20">
        <a-col class="gutter-row" :span="8">
          <div class="gutter-box">
            <a-row type="flex" justify="space-between" style="flex:1; font-size:22px;font-weight: 500;color: #373737;">
              <a-col>维修数据</a-col>
              <a-col><a href="/repair/form?type=1&searchType=6" target="_block">{{ topData.repairTotalNum }}</a></a-col>
            </a-row>
            <a-row type="flex" justify="space-between" :gutter="[0,16]" style="flex:1;font-size:18px;font-weight: 500;">
              <a-col >
                <a-badge color="#F19149"/>
                <span style="color:#F19149;"><a style="color:#F19149;" target="_block" href="/repair/wait?type=1">待接单 &nbsp; {{ topData.completedRepairNum }}</a></span>

              </a-col>
              <a-col >
                <a-badge color="#E4007F"/>
                <span style="color:#E4007F;"><a style="color:#E4007F;" target="_block" href="/repair/form/mine?type=1">维修中 &nbsp; {{ topData.waitForRepairNum }}</a></span>
              </a-col>

            </a-row>
            <a-row type="flex" justify="space-between" :gutter="[0,16]" style="flex:1;font-size:18px;font-weight: 500;">
              <a-col >
                <a-badge color="#5864FF"/>
                <span style="color:#5864FF;"><a style="color:#5864FF;" target="_block" href="/repair/form?type=1&searchType=3">待审核 &nbsp; {{ topData.verifyRepairNum }}</a></span>

              </a-col>
              <a-col>
                <a-badge color="#009944"/>
                <span style="color:#009944;"><a style="color:#009944;" target="_block" href="/repair/form?type=1&searchType=5">已驳回 &nbsp; {{ topData.refusedRepairNum }}</a></span>

              </a-col>

            </a-row>
          </div>
        </a-col>
        <a-col class="gutter-row" :span="8">
          <div class="gutter-box">
            <a-row type="flex" justify="space-between" style="flex:1; font-size:22px;font-weight: 500;color: #373737;">
              <a-col>委外数据</a-col>
              <a-col><a target="_block" href="/repair/form?type=2&searchType=6">{{ topData.outRepairTotalNum }}</a></a-col>
            </a-row>
            <a-row type="flex" justify="space-between" :gutter="[0,16]" style="flex:1;font-size:18px;font-weight: 500;">
              <a-col >
                <a-badge color="#F19149"/>
                <span style="color:#F19149;"><a style="color:#F19149;" target="_block" href="/repair/wait?type=2">待接单 &nbsp; {{ topData.outCompletedRepairNum }}</a></span>

              </a-col>
              <a-col >
                <a-badge color="#E4007F"/>
                <span style="color:#E4007F;"><a style="color:#E4007F;" target="_block" href="/repair/form/mine?type=2">待维修 &nbsp; {{ topData.outWaitForRepairNum }}</a></span>
              </a-col>

            </a-row>
            <a-row type="flex" justify="space-between" :gutter="[0,16]" style="flex:1;font-size:18px;font-weight: 500;">
              <a-col >
                <a-badge color="#5864FF"/>
                <span style="color:#5864FF;"><a style="color:#5864FF;" target="_block" href="/repair/form?type=2&searchType=3">待审核 &nbsp; {{ topData.outVerifyRepairNum }}</a></span>

              </a-col>
              <a-col>
                <a-badge color="#009944"/>
                <span style="color:#009944;"><a style="color:#009944;" target="_block" href="/repair/form?type=2&searchType=5">已驳回 &nbsp; {{ topData.outRefusedRepairNum }}</a></span>
              </a-col>
            </a-row>
          </div>
        </a-col>
        <a-col class="gutter-row" :span="8">
          <div style="display:flex;text-align:center;background:#fff;height: 170px;">
            <div style="width:48%;margin:auto;font-size:30px; line-height: 60px;">
              <div>本月维修总计</div>
              <div style="font-size:24px;"><a target="_block" :href="`/repair/form?repairStartTimeStart=${date.repairStartTimeStart}&repairStartTimeEnd=${date.repairStartTimeEnd}`">{{ topData.totalNum }}</a></div>
            </div>
            <div style="width: 1px;height: 70px;background: #999999;margin:auto;"></div>
            <div style="width:48%;margin:auto; font-size:30px;line-height: 60px;">
              <div>本月维修费用</div>
              <div style="font-size:24px;"><a target="_block" :href="`/repair/fee?feeDateStart=${date.searchStartTime}&feeDateEnd=${date.searchEndTime}`">{{ topData.totalFee }}</a></div>
            </div>
          </div>
        </a-col>
      </a-row>
    </div>
    <div class="gutter-example">
      <a-row :gutter="[20,40]">
        <a-col class="gutter-row" :span="8">
          <div style="height: 471px;background: #FFFFFF;padding:24px">
            <div style="display:flex;justify-content: space-between;width:160px;height: 18px; font-size:16px;color: #555555; ">
              <div>维修费用类别统计</div>
            </div>
            <div id="container-pie"></div>
          </div>
        </a-col>
        <a-col class="gutter-row" :span="12">
          <div style="height: 471px;background: #FFFFFF; padding:24px;">
            <div style="display:flex;justify-content: space-between;width:190px;height: 18px; font-size:16px;color: #555555; margin-bottom:80px;">
              <div style="width: 7px;background: #4D86ED;"></div>
              <div>近一周每日产生维修单数</div>
            </div>
            <div id="container-line"></div>
          </div>
        </a-col>
        <a-col class="gutter-row" :span="4">
          <div style="height: 471px;display:flex;flex-direction:column;justify-content: space-between">
            <a style="color:#fff;" target="_block" href="/repair/form/mine?type=1">
              <div class="btn" style="background:linear-gradient(to right,#36B5FA,#26ECF5);" >
                <my-icon type="icon-weixiu" style="font-size:58px;"/>
                <span>维修</span>
              </div>
            </a>
            <div class="btn" style="background:linear-gradient(to right,#FF416D,#FF847F);" @click="$refs.repairForm.base({},{filter: -1})">
              <my-icon type="icon-baoxiu-xuanzhong-copy" style="font-size:58px;color:#fff;"/>
              <span>报修</span>
            </div>
            <div class="btn" style="background:linear-gradient(to right,#FB47D1,#F96EFE);" @click="$refs.sparePickForm.base()">
              <my-icon type="icon-tubiaozhizuomoban-copy" style="font-size:58px;color:#fff;"/>
              <span>领用</span>
            </div>
            <div class="btn" style="background:linear-gradient(to right,#941FFF,#BC72FB);" @click="$refs.sparebackform.base()">
              <my-icon type="icon-tuiku" style="font-size:58px;"/>
              <span>退库</span>
            </div>
          </div>
        </a-col>
      </a-row>
    </div>
    <div class="gutter-example">
      <a-row :gutter="20">
        <a-col class="gutter-row" :span="6">
          <div class="information">
            <div style="display:flex;justify-content: space-between;align-items: center; padding-bottom:20px;">
              <div style="display:flex; width:65px;height:17px;justify-content: space-between;align-items: center;">
                <div style=" width: 17px;height: 17px;background: #3462FD;border-radius: 50%;"></div>
                <div style="font-size: 18px;font-weight: 800;color: #333333;">资料</div>
              </div>
              <div >
                查看全部
                <my-icon type="icon-xiangyou-copy" style="font-size:13px;"/>
              </div>
            </div>
            <div
              class="demo-infinite-container"
            >
              <a-list size="small" bordered :data-source="information">
                <a-list-item slot="renderItem" slot-scope="item,index" :class="{discolor: index%2}">
                  <a-list-item-meta>
                    <div slot="title" :href="item.href">{{ item.name }}</div>
                  </a-list-item-meta>
                  <div>{{ item.time }}</div>
                </a-list-item>
              </a-list>
            </div>
          </div>
        </a-col>
        <a-col class="gutter-row" :span="18">
          <div class="information">
            <div style="display:flex;justify-content: space-between;align-items: center; padding-bottom:20px;">
              <div style="display:flex; width:65px;height:17px;justify-content: space-between;align-items: center;">
                <div style=" width: 17px;height: 17px;background: #3462FD;border-radius: 50%;"></div>
                <div style="font-size: 18px;font-weight: 800;color: #333333;">通知</div>
              </div>
              <a href="WorkplaceBacklog" target="_block">
                查看全部
                <my-icon type="icon-xiangyou-copy" style="font-size:13px;"/>
              </a>
            </div>
            <s-table
              ref="table"
              size="small"
              bordered
              rowKey="id"
              :columns="columns"
              :data="loadData"
              :pageSize="10"
              :scroll="{ y: 190 }"
            >
              <span slot="action" slot-scope="record">
                <template>
                  <a @click="handle(record)">详情</a>
                  <a-divider v-if="record.status === 1" type="vertical" />
                  <a v-if="record.status === 1" @click="dealJumpDetail(record)">跳转</a>
                </template>
              </span>
              <span slot="content" slot-scope="text" v-html="text">
              </span>
            </s-table>
          </div>
        </a-col>
      </a-row>
    </div>
    <repair-form ref="repairForm"></repair-form>
    <SparePickForm ref="sparePickForm"></SparePickForm>
    <SpareBackForm ref="sparebackform"></SpareBackForm>
  </div>
</template>

<script>
import { STable } from '@/components'
import RepairForm from './modules/RepairBaseForm'
import SpareBackForm from '@/views/store/sparebackform/modules/BaseForm'
import SparePickForm from '@/views/store/sparepickform/modules/BaseForm'
import { getWorkplaceBacklogUserPage, getWorkplaceBacklogTopData, getWorkplaceBacklogWeekData, getWorkplaceBacklogPie } from '@/api/workplace/backlog'
import { Chart } from '@antv/g2'
export default {
  name: 'NewWorkplaceBacklog',
  components: {
    STable,
    SparePickForm,
    SpareBackForm,
    RepairForm,
    Chart
  },
  data () {
    return {
      equipmentData: [],
      lineData: [],
      allCount: 100,
      chartPie: null,
      chartLine: null,
      topData: null,
      type: this.$route.query.type,
      date: {
        repairStartTimeStart: '',
        repairStartTimeEnd: '',
        searchStartTime: '',
        searchEndTime: ''
      },
      queryParam: {
        status: 1
      },
      information: [
        {
          name: '项目管理通知',
          time: '2022-4-7'
        },
        {
          name: '项目管理通知',
          time: '2022-4-7'
        },
        {
          name: '项目管理通知',
          time: '2022-4-7'
        },
        {
          name: '项目管理通知',
          time: '2022-4-7'
        }, {
          name: '项目管理通知',
          time: '2022-4-7'
        },
        {
          name: '项目管理通知',
          time: '2022-4-7'
        },
        {
          name: '项目管理通知',
          time: '2022-4-7'
        }
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          width: 50,
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        {
          title: '类型',
          dataIndex: 'type',
          align: 'center',
          width: 100,
          customRender: (text, record, index) => {
            return this.typeDict[text]
          }
        },
        {
          title: '详细类型',
          dataIndex: 'detailType',
          align: 'center',
          width: 140,
          customRender: (text, record, index) => {
            return this.typeDetailDict[text]
          }
        },
        {
          title: '内容',
          dataIndex: 'content',
          align: 'center',
          width: 460,
          ellipsis: true,
          scopedSlots: { customRender: 'content' }
        },
        {
          title: '时间',
          align: 'center',
          width: 160,
          dataIndex: 'createdTime'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          width: 100,
          customRender: (text, record, index) => {
            return (text === 1 ? '未读' : '已读')
          }
        },
        {
          title: '操作',
          key: 'action',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam
        }
        return getWorkplaceBacklogUserPage(parameter)
          .then(res => {
            return res.data
          })
      }
    }
  },
  computed: {
    role () {
      if (this.$store.state.user.roles.join().includes('manage')) {
        return 'workplace-repair-manage'
      } else if (this.$store.state.user.roles.join().includes('normal')) {
        return 'workplace-repair-normal'
      } else {
        return ''
      }
    }
  },
  created () {
    this.getdate()
    console.log(this.date)
    this.getDict()
    this.getInfo()
  },
  mounted () {
  },
  updated () {
    console.log(11)
    if (this.lineData.length > 0 && this.equipmentData.length > 0) {
      this.$nextTick(function () {
        console.log(1111)
        this.getPieCharts('container-pie')
        this.getLineCharts('container-line')
      })
    }
  },
  methods: {
    getdate () {
      const date = new Date()
      const year = date.getFullYear()
      const month = date.getMonth()
      this.date = {
        repairStartTimeStart: `${year}-0${month + 1}-01 00:00:00`,
        repairStartTimeEnd: this.BaseTool.Date.formatter(date, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN),
        searchStartTime: `${year}-0${month + 1}-01`,
        searchEndTime: this.BaseTool.Date.formatter(date, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)

      }
    },
    getDict () {
      this.typeDict = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.WORKPLACE_BACKLOG_TYPE)
      this.typeDetailDict = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.WORKPLACE_BACKLOG_DETAIL_TYPE)
    },
    getInfo () {
      getWorkplaceBacklogWeekData(this.role).then(res => {
        const data = res.data.reduce((pre, item) => {
          pre.push({
            week: item.weekDayName,
            value: item.tempTotalNum
          })
          return pre
        }, [])
        this.lineData = data
      })
      getWorkplaceBacklogPie(this.role).then(res => {
        const data = res.data.reduce((pre, item) => {
          pre.push({
            item: item.typeName,
            count: item.fee,
            percent: item.fee
          })
          return pre
        }, [])
        this.equipmentData = data
      })
      getWorkplaceBacklogTopData(this.role).then(res => {
        this.topData = res.data
      })
    },
    getPieCharts (id) {
      this.chartPie && this.chartPie.destroy()// 防止点击搜索按钮新增一个
      this.chartPie = new Chart({
        container: id,
        autoFit: true,
        height: 400
      })
      this.chartPie.data(this.equipmentData)
      this.chartPie.scale('count', {
        nice: true
      })
      this.chartPie.coordinate('theta', {
        radius: 0.6,
        innerRadius: 0.5
      })
      this.chartPie.tooltip({
        showTitle: false,
        showMarkers: false,
        itemTpl: `<li class="g2-tooltip-list-item">
                     <span style="background-color:{color};" class="g2-tooltip-marker"></span>
                     {name}: {value}
                  </li>`
      })
      // 辅助文本
      this.chartPie
        .annotation()
      this.chartPie
        .interval()
        .adjust('stack')
        .position('percent')
        .color('item')
        .label('percent', (percent) => {
          return {
            content: (data) => {
              return `${data.item}: ${percent}`
            }
          }
        })
        .tooltip('item*count', (item, count) => {
          return {
            name: item,
            value: count
          }
        })
      // this.chartPie.interaction('element-active')
      this.chartPie.interaction('active-region')

      this.chartPie.render()
    },
    getLineCharts (id) {
      this.chartLine = new Chart({
        container: id,
        autoFit: true,
        height: 300
      })

      this.chartLine.data(this.lineData)
      this.chartLine.scale({
        week: {
          range: [0, 1]
        },
        value: {
          min: 0,
          nice: true
        }
      })

      this.chartLine.tooltip({
        showCrosshairs: true, // 展示 Tooltip 辅助线
        shared: true
      })

      this.chartLine.axis('value', {
        label: {
          formatter: (val) => {
            return val
          }
        }
      })

      this.chartLine.area().position('week*value')
      this.chartLine.line().position('week*value')

      this.chartLine.render()
    }
  }
}
</script>

<style scoped>
.gutter-example >>> .ant-row > div {
  background: transparent;
  border: 0;
  font-family: PingFang SC;
}
.gutter-box {
  display:flex;
  flex-direction:column;
  justify-content: space-between;
  background: #fff;
  padding: 18px 41px;
  height: 170px;
}
.btn{
height: 113px;
width: 100%;
color: #fff!important;
font-size:28px;
border-radius: 20px;
display:flex;
align-items: center;
justify-content: space-around;
}
.btn:hover{
  cursor:pointer;
}
.information{
  background: #fff;
  padding: 30px 12px;
  height: 500px;
  color:#666;
}
.discolor{
  background: #EFEFFB;
}
.demo-infinite-container {
  overflow: auto;
  height: 430px;
}
::-webkit-scrollbar { width: 0; height: 0; color: transparent; }
</style>
