<template>
  <div class="dashboard-editor-container">
    <el-row :gutter="32">
      <el-col :span="10">
        <div class="card-panel">
          <div class="card-panel-description">
            <div v-if="isChildren" class="select-franchise">
              <div>{{ franchise.enterprise_name }}</div>
              <el-link :underline="false" type="danger" @click="form.franchiseId = franchise.id;projectDialog = true">【点击切换】</el-link>
            </div>
            <div class="card-panel-text">服务家庭数</div>
            <div>
              <count-to
                :start-val="0"
                :end-val="servedNums"
                :duration="2600"
                class="card-panel-num"
              />
              户
            </div>
          </div>
        </div>
        <div class="card-panel left-bottom">
          <reverse-bar-chart :chart-data="reverseBarChartData" />
        </div>
      </el-col>
      <el-col :span="14">
        <div class="card-panel">
          <mu-barchart :chart-data="muBarchartData" />
        </div>
        <div class="card-panel">
          <service-increase />
        </div>
      </el-col>
    </el-row>
    <el-dialog
      title="切换加盟商"
      :visible="projectDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="400px"
      @close="projectDialog = false"
    >
      <div>
        <el-form
          ref="form"
          :rules="rules"
          :model="form"
        >
          <el-form-item label="加盟商: " prop="franchiseId">
            <el-select v-model="form.franchiseId">
              <el-option
                v-for="(item) of franchiseList"
                :key="item.id"
                :label="item.enterprise_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="projectDialog = false">取消</el-button>
        <el-button type="primary" @click="changeFranchise">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CountTo from 'vue-count-to'
import MuBarchart from './admin/components/MuBarchart.vue'
import ReverseBarChart from './admin/components/ReverseBarChart.vue'
import ServiceIncrease from './admin/components/ServiceIncrease.vue'

import { homeCount } from '@/api/dashboard'
import { switchFranchise, franchiseSwitchLists } from '@/api/personnel'
export default {
  components: { CountTo, ServiceIncrease, MuBarchart, ReverseBarChart },
  // inject: ['reload'],
  data: function() {
    return {
      servedNums: 0,
      reverseBarChartData: [],
      muBarchartData: {
        data: [],
        title: [],
      },
      lineChartData: {
        expectedData: [100, 120, 161, 134, 105, 160, 165],
        actualData: [120, 82, 91, 154, 162, 140, 145]
      },

      isChildren: self !== top,
      franchiseList: [],
      franchise: {},
      projectDialog: false,
      form: {
        franchiseId: ''
      },
      rules: {
        franchiseId: [{ required: true, trigger: 'change', message: '请选择' }]
      },
    }
  },
  created() {
    homeCount().then(res => {
      const { family_served_nums, items, areas } = res.data
      this.servedNums = family_served_nums
      const { summary_graph, summary_graph_types } = items
      const data = Object.keys(summary_graph).map(item => [item, ...Object.values(summary_graph[item])])
      const itemNames = summary_graph_types.map(item => {
        return item.name
      })
      this.reverseBarChartData = Object.values(areas)
      this.muBarchartData = {
        title: itemNames,
        data
      }
    })
    if (this.isChildren) {
      this.franchise = JSON.parse(sessionStorage.getItem('selectFranchise'))
      franchiseSwitchLists().then(res => {
        this.franchiseList = res.data.filter(item => item.id !== 14)
      })
    }
  },
  methods: {
    changeFranchise() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const { franchiseId } = this.form
          switchFranchise({
            belong_to: franchiseId
          }).then(res => {
            localStorage.setItem('switchToken', res.data)
            sessionStorage.setItem('selectFranchise', JSON.stringify(this.franchiseList.find(item => item.id === franchiseId)))
            // this.reload()
            this.$router.go(0)
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;
  .left-bottom{
    height: 451px;
  }
  .card-panel {
    // height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);
    margin-bottom: 40px;
    .card-panel-description {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      // height: 100%;
      height: 200px;
      position: relative;
      .select-franchise{
        position: absolute;
        display: flex;
        align-items: center;
        top: 15px;
        left: 15px;
        color: #222;
        font-size: 16px;
      }

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 30px;
      }
    }
  }
}
</style>
