<template>
  <div class="app-container">
    <!--列表页面-->
    <div v-show="viewState == 1">
      <button-bar view-title="事业部日报表" ref="btnbar" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
      <!--Start查询列表部分-->
      <div class="main-content">
<!--        <div class="searchcon">-->
<!--          <el-button size="small" class="searchbtn" @click="clicksearchShow">{{ searchxianshitit }}-->
<!--          </el-button>-->
<!--          <div v-show="isSearchShow" class="search">-->
<!--            <el-form ref="listQueryform" :inline="true" :model="listQuery" label-width="110px" class="tab-header">-->
<!--              <el-form-item label="生成日期">-->
<!--                <el-date-picker v-model="listQuery.params.startTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>-->
<!--                <span style="padding: 0 8px">至</span>-->
<!--                <el-date-picker v-model="listQuery.params.endTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>-->
<!--              </el-form-item>-->
<!--            </el-form>-->
<!--            <div class="btn" style="text-align: center;">-->
<!--              <el-button type="primary" icon="el-icon-search" size="small" @click="handleFilter">查询-->
<!--              </el-button>-->
<!--              <el-button type="primary" icon="el-icon-refresh" size="small" @click="handleReset">重置-->
<!--              </el-button>-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->
        <!--End查询列表部分-->
        <div class="listtop">
          <div class="tit">事业部日报表</div>
          <pageye v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
        </div>
        <!--Start 主页面主要部分 -->
        <div class="">
          <el-table :key="tableKey" v-loading="listLoading" :data="list" :border="true" style="width: 100%;" @selection-change="handleSelectionChange" :span-method="rowspanMethod">
            <el-table-column prop="busOrgName" label="事业部" width="120" align="center"></el-table-column>
            <el-table-column label="分公司" width="120" align="center">
              <template slot-scope="scope">
                <span class="bluezi" @click="lookBranch(scope.row)">{{ scope.row.useOrgName }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="brandName" label="品牌" width="120" align="center"></el-table-column>
            <el-table-column prop="fuelTypeValue" label="燃料" width="120" align="center"></el-table-column>
            <el-table-column prop="emissionStandardValue" label="排放标准" width="120" align="center"></el-table-column>
            <el-table-column prop="saleOfStockRatio" label="存销比" width="80" align="center"></el-table-column>
            <el-table-column prop="stock_total" label="库存总计" width="100" align="center"></el-table-column>
            <el-table-column label="库存" width="120" align="center">
              <el-table-column prop="stock_unreserve" label="未定" width="80" align="center"></el-table-column>
              <el-table-column prop="stock_reserve" label="已定" width="80" align="center"></el-table-column>
              <el-table-column prop="stock_subtotal" label="小计" width="80" align="center"></el-table-column>
              <el-table-column label="其中已买断" width="120" align="center">
                <el-table-column prop="stock_buyout_unreserve" label="未定" width="80" align="center"></el-table-column>
                <el-table-column prop="stock_buyout_reserve" label="已定" width="80" align="center"></el-table-column>
                <el-table-column prop="stock_buyout_subtotal" label="小计" width="80" align="center"></el-table-column>
              </el-table-column>
            </el-table-column>
            <el-table-column label="排产" width="120" align="center">
              <el-table-column prop="pcOrder_unreserve" label="未定" width="80" align="center"></el-table-column>
              <el-table-column prop="pcOrder_reserve" label="已定" width="80" align="center"></el-table-column>
              <el-table-column prop="pcOrder_subtotal" label="小计" width="80" align="center"></el-table-column>
            </el-table-column>
            <el-table-column label="订车" width="120" align="center">
              <el-table-column label="简易订单" width="120" align="center">
                <el-table-column prop="simpleOrder_day" label="本日" width="80" align="center"></el-table-column>
                <el-table-column prop="simpleOrder_month" label="本月" width="80" align="center"></el-table-column>
              </el-table-column>
              <el-table-column label="本日销售订单" width="120" align="center">
                <el-table-column prop="saleOrder_loan_day" label="贷款" width="80" align="center"></el-table-column>
                <el-table-column prop="saleOrder_full_day" label="全款" width="80" align="center"></el-table-column>
                <el-table-column prop="saleOrder_subtotal_day" label="小计" width="80" align="center"></el-table-column>
              </el-table-column>
              <el-table-column label="本月销售订单" width="120" align="center">
                <el-table-column prop="saleOrder_loan_month" label="贷款" width="80" align="center"></el-table-column>
                <el-table-column prop="saleOrder_full_month" label="全款" width="80" align="center"></el-table-column>
                <el-table-column prop="saleOrder_subtotal_month" label="小计" width="80" align="center"></el-table-column>
              </el-table-column>
            </el-table-column>
            <el-table-column prop="saleOrder_waitVeh_total" label="待交车累计总订单" width="140" align="center"></el-table-column>
            <el-table-column label="销售" width="120" align="center">
              <el-table-column label="本日销售" width="120" align="center">
                <el-table-column prop="sale_loan_day" label="贷款" width="80" align="center"></el-table-column>
                <el-table-column prop="sale_full_day" label="全款" width="80" align="center"></el-table-column>
                <el-table-column prop="sale_subtotal_day" label="小计" width="80" align="center"></el-table-column>
              </el-table-column>
              <el-table-column label="本月销售" width="120" align="center">
                <el-table-column prop="sale_loan_month" label="贷款" width="80" align="center"></el-table-column>
                <el-table-column prop="sale_full_month" label="全款" width="80" align="center"></el-table-column>
                <el-table-column prop="sale_subtotal_month" label="小计" width="80" align="center"></el-table-column>
              </el-table-column>
              <el-table-column label="全年销售" width="120" align="center">
                <el-table-column prop="sale_loan_year" label="贷款" width="80" align="center"></el-table-column>
                <el-table-column prop="sale_full_year" label="全款" width="80" align="center"></el-table-column>
                <el-table-column prop="sale_subtotal_year" label="小计" width="80" align="center"></el-table-column>
              </el-table-column>
            </el-table-column>
            <el-table-column prop="manufacturer_invoicing_year" label="全年厂家开票数" width="120" align="center"></el-table-column>
            <el-table-column label="买断" width="120" align="center">
              <el-table-column prop="buyout_day" label="本日" width="80" align="center"></el-table-column>
              <el-table-column prop="buyout_month" label="本月" width="80" align="center"></el-table-column>
              <el-table-column prop="buyout_year" label="本年" width="80" align="center"></el-table-column>
            </el-table-column>
            <el-table-column label="欠款出库" width="120" align="center">
              <el-table-column prop="arrears_carry_veh_count" label="数量" width="80" align="center"></el-table-column>
              <el-table-column prop="arrears_carry_veh_amount" label="金额(万元)" width="100" align="center"></el-table-column>
            </el-table-column>
            <el-table-column label="金融未放款" width="120" align="center">
              <el-table-column prop="loan_not_count" label="数量" width="80" align="center"></el-table-column>
              <el-table-column prop="loan_not_amount" label="金额(万元)" width="100" align="center"></el-table-column>
            </el-table-column>
          </el-table>
        </div>
        <!--End 主页面主要部分-->
        <div class="pages">
          <div class="tit"/>
          <!-- 翻页 -->
          <pagination v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
        </div>
        <!--End查询列表部分-->
      </div>
    </div>
    <branchofficestatistics v-show="viewState == 2" ref="divBranch" @doback="resetState"/>
  </div>
</template>

<script>
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
import ButtonBar from '@/components/ButtonBar'
import req from '@/api/businessdivisionstatistics/businessdivisionstatistics'
import branchofficestatistics from '../branchofficestatistics/branchofficestatistics'

export default {
  name: 'BusinessDivisionStatistics',
  components: {
    Pagination,
    pageye,
    ButtonBar,
    branchofficestatistics
  },
  data() {
    return {
      btndisabled: false,
      btnList: [
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      isSearchShow: false,
      searchxianshitit: '显示查询条件',
      viewState: 1, // 1、列表 2、新增 3、编辑 4、查看
      tableKey: 0,
      list: [],
      sids: [],
      multipleSelection: [],
      FormLoading: false,
      listLoading: false,
      // 翻页
      listQuery: {
        current: 1,
        size: 5,
        total: 0,
        params: {
          endTime: '',
          orgPath: '',
          startTime: '',
          todayDate: '',
          useOrgSid: ''
        }
      }
    }
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  methods: {
    // 事业部日报表 -- 统计页面调用
    init(row) {
      this.listQuery.params.todayDate = row.date
      this.listQuery.params.orgPath = window.sessionStorage.getItem('defaultOrgPath')
      this.getList()
    },
    // 集团日报表 -- 详细页面调用
    groupInit(date, orgPath) {
      this.listQuery.params.todayDate = date
      this.listQuery.params.orgPath = orgPath
      this.getList()
    },
    // 搜索条件效果
    clicksearchShow() {
      this.isSearchShow = !this.isSearchShow
      if (this.isSearchShow) {
        this.searchxianshitit = '隐藏查询条件'
      } else {
        this.searchxianshitit = '显示查询条件'
      }
    },
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX    ' + btnKey)
      switch (btnKey) {
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    // 信息条数 获取点击时当前的sid
    handleSelectionChange(row) {
      const aa = []
      this.multipleSelection = row
      row.forEach(element => {
        aa.push(element.sid)
      })
      this.sids = aa
    },
    // 合并单元格
    rowspanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2) { // 定位到需要合并的列
        // 获取当前单元格的值
        const currentValue = row[column.property]
        // 获取上一行相同列的值
        const preRow = this.list[rowIndex - 1]
        const preValue = preRow ? preRow[column.property] : null
        // 如果当前值和上一行的值相同,则将当前单元格隐藏
        if (currentValue === preValue) {
          return { rowspan: 0, colspan: 0 }
        } else {
          // 否则计算当前单元格应该跨越多少行
          let rowspan = 1
          for (let i = rowIndex + 1; i < this.list.length; i++) {
            const nextRow = this.list[i]
            const nextValue = nextRow[column.property]
            if (nextValue === currentValue) {
              rowspan++
            } else {
              break
            }
          }
          return { rowspan, colspan: 1 }
        }
      }
    },
    // 表中序号
    indexMethod(index) {
      var pagestart = (this.listQuery.current - 1) * this.listQuery.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    // 查询列表信息
    getList() {
      this.listLoading = true
      req.listByBusiness(this.listQuery).then(response => {
        this.listLoading = false
        if (response.success) {
          this.list = response.data.records
          this.listQuery.total = response.data.total
        } else {
          this.list = []
          this.listQuery.total = 0
        }
      })
    },
    // 查询按钮
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    // 点击重置
    handleReset() {
      this.listQuery = {
        current: 1,
        size: 10,
        total: 0,
        params: {
          endTime: '',
          orgPath: '',
          startTime: '',
          todayDate: '',
          useOrgSid: ''
        }
      }
      this.getList()
    },
    lookBranch(row) {
      this.viewState = 2
      this.$refs['divBranch'].branchInit(this.listQuery.params.todayDate, row.useOrgSid)
    },
    // 修改、编辑、详情返回列表页面
    resetState() {
      this.viewState = 1
    },
    doClose() {
      this.$emit('doback')
    }
  }
}
</script>
<style scoped>
</style>
