<template>
  <div ref="tab" class="m20">
    <div class="content-header">
      <div class="body">
        <div class="body">
          <el-radio-group v-model="tabName" style="margin-bottom: 30px;margin-left: 30px">
            <el-radio-button label="market">市场主体户数</el-radio-button>
            <el-radio-button label="business">办理业务数</el-radio-button>
          </el-radio-group>
        </div>
        <div v-if="tabName == 'market'" class="body">
          <el-row style="margin-bottom: 10px;margin-left: 30px">
            <el-col :span="8">

                <label class="label-name_1">选择日期：</label> <!-- value-format="yyyy-MM-dd" 指定日期发送格式  -->
                <el-date-picker
                  v-model="searchForm.searchDate"
                  size="mini"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                />

            </el-col>
            <el-col style="text-align: bottom;" :span="4">
              <el-button style="margin-bottom: 0px;" size="mini" type="primary" @click="search">查询</el-button>
            </el-col>
          </el-row>
          <el-row style="margin-bottom: 10px;margin-left: 30px">
            <el-col :span="8">
              <label class="label-name_1">数据项：</label>
              <el-select v-model="entType" placeholder="请选择" clearable>
                <el-option
                  v-for="item in entTypeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <!-- <el-cascader
                v-model="entType"
                size="mini"
                :options="entTypeOptions"
                clearable
                filterable
              /> -->
            </el-col>
          </el-row>
          <div style="text-align: right;margin-bottom: 10px;">
            <el-button style="margin-bottom: 0px;" size="mini" type="primary" @click="exportFile">下载表格</el-button>
          </div>
        </div>
        <div v-if="tabName == 'business'" class="body">
          <el-row style="margin-bottom: 30px;margin-left: 30px">
            <el-col :span="6">
              <el-row>
                <label class="label-name_1">选择日期：</label> <!-- value-format="yyyy-MM-dd" 指定日期发送格式  -->
                <el-date-picker
                  v-model="busSearchForm.searchDate"
                  size="mini"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                />
              </el-row>
            </el-col>
            <el-col :span="4">
              <el-button size="mini" type="primary" @click="busSearch">查询</el-button>
            </el-col>
          </el-row>
          <el-row style="margin-bottom: 30px;margin-left: 30px">
            <label class="label-name_1">业务状态：</label>
            <el-radio-group v-model="busSearchForm.busstatus">
              <el-radio :label="0">全部</el-radio>
              <el-radio :label="1">完成审批</el-radio>
              <el-radio :label="2">审核/审批中</el-radio>
            </el-radio-group>
          </el-row>
        </div>
      </div>
    </div>
    <div v-if="tabName == 'market'" class="content-body">
      <div v-if="isFH" class="submit-box" style="margin-bottom: 10px;">
        <el-button @click="search2Fail()">返回</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="datashow"
        :span-method="isqj ? objectSpanMethod : objectSpanMethod2"
        cell-class-name="cell-class-name"
        :header-cell-style="{'text-align':'center','background':'#5197FF',color:'#fff','fontWeight':'500','border-color':'#8CC3FB'}"
        style="wdith:100%;border:1px solid #8CC3FB"
        :cell-style="{'text-align':'center'}"
      >
        <el-table-column prop="xzqh" label="单位" fixed min-width="250">
          <template slot-scope="scope">
            <span style="cursor:pointer" @click="search2(scope.row)" :style="cellStyle(scope.row)">{{ scope.row.xzqh }}</span>
          </template>
        </el-table-column>
        <el-table-column label="企业（户数）" v-if="entType == '' || entType == 'qy'">
          <el-table-column label="列入">
            <el-table-column label="未按规定公示或填报年报" prop="qylr1" />
            <el-table-column label="未按规定公示其他应公示的信息" prop="qylr2"/>
            <el-table-column label="通过登记的经营场所无法联系" prop="qylr4" />
            <el-table-column label="公示信息隐瞒情况，弄虚作假" prop="qylr3" />
            <el-table-column label="登记机关认为不适宜的主体名称" prop="qylr5" />
          </el-table-column>
          <el-table-column label="移出">
            <el-table-column label="未按规定公示或填报年报" prop="qyyc1" />
            <el-table-column label="未按规定公示其他应公示的信息" prop="qyyc2"/>
            <el-table-column label="通过登记的经营场所无法联系" prop="qyyc4" />
            <el-table-column label="公示信息隐瞒情况，弄虚作假" prop="qyyc3" />
            <el-table-column label="登记机关认为不适宜的主体名称" prop="qyyc5" />
          </el-table-column>
          <el-table-column label="异议" prop="qyyy"></el-table-column>
        </el-table-column>
        <el-table-column label="个体户（户数）" v-if="entType == '' || entType == 'gt'">
          <el-table-column label="列入">
            <el-table-column label="未按规定公示或填报年报" prop="gtlr1" />
            <el-table-column label="通过登记的经营场所无法联系" prop="gtlr3" />
            <el-table-column label="公示信息隐瞒情况，弄虚作假" prop="gtlr2" />
          </el-table-column>
          <el-table-column label="移出">
            <el-table-column label="未按规定公示或填报年报" prop="gtyc1" />
            <el-table-column label="通过登记的经营场所无法联系" prop="gtyc3" />
            <el-table-column label="公示信息隐瞒情况，弄虚作假" prop="gtyc2" />
          </el-table-column>
          <el-table-column label="异议" prop="gtyy"></el-table-column>
        </el-table-column>
        <el-table-column label="农专（户数）" v-if="entType == '' || entType == 'nz'">
          <el-table-column label="列入">
            <el-table-column label="未按规定公示或填报年报" prop="nhlr1" />
            <el-table-column label="通过登记的经营场所无法联系" prop="nhlr3" />
            <el-table-column label="公示信息隐瞒情况，弄虚作假" prop="nhlr2" />
          </el-table-column>
          <el-table-column label="移出">
            <el-table-column label="未按规定公示或填报年报" prop="nhyc1" />
            <el-table-column label="通过登记的经营场所无法联系" prop="nhyc3" />
            <el-table-column label="公示信息隐瞒情况，弄虚作假" prop="nhyc2" />
          </el-table-column>
          <el-table-column label="异议" prop="nhyy"></el-table-column>
        </el-table-column>
      </el-table>
    </div>
    <div v-if="tabName == 'business'">
      <el-table
        v-loading="busLoading"
        :data="cphStatistics"
        :row-class-name="tableRowClassName"
        :header-cell-style="{'text-align':'center', background:'#8cc3fb',color:'#fff'}"
        style="wdith:100%;border:1px solid #8CC3FB"
        :cell-style="{'text-align':'center'}"
      >
        <el-table-column prop="orgunitname" label="单位" min-width="20" />
        <el-table-column prop="abnlrnum" label="列入" min-width="20" />
        <el-table-column prop="abnycnum" label="移出" min-width="20" />
        <el-table-column prop="abnyynum" label="异议" min-width="20" />
      </el-table>
    </div>
  </div>
</template>

<script>
import { queryStatistics, queryCphStatisticsSplit, statisticsAbnExcel } from '@/api/comprehensive'

export default {
  data() {
    return {
      tabName: 'market',
      entType: '',
      entTypeOptions:[{
        value: 'qy',
        label: '企业（户数）'
      }, {
        value:'gt',
        label: '个体户（户数）'
      }, {
        value: 'nz',
        label: '农专（户数）'
      }],
      isqj: false, // 是否为区局
      isFH: false,
      datashow: [],
      AbnStatisticsDtos: [],
      AbnStatisticsDtos2: [],
      loading: false,
      searchForm: {
        searchDate: ''
      },

      busLoading: false,
      busSearchForm: {
        bustype: 'abnormal',
        searchDate: '',
        busstatus: 0
      },
      cphStatistics: []
    }
  },
  mounted() {
    //this.busSearch()
  },
  methods: {
    exportFile() {
      let param = {
        data : this.datashow,
        type : this.entType
      }
      statisticsAbnExcel(param).then(res => {
        const url = window.URL.createObjectURL(res, { type: 'application/vnd.ms-excel;charset=utf-8' })
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '认领情况统计.xlsx')
        link.click()
      })
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      }
    },
    busSearch() {
      this.busLoading = true
      queryCphStatisticsSplit(this.busSearchForm).then(res => {
        if (res.code === 0) {
          this.cphStatistics = res.data
          this.busLoading = false
        } else {
          this.$message.error(res.msg)
        }
      }).catch(_ => {
        this.busLoading = false
      })
    },

    cellStyle(row) {
      if (row.xzqh === '广州市市场监督管理局') {
        return 'color: #1F65FF'
      }
    },
    objectSpanMethod2({ row, column, rowIndex, columnIndex }) {
      if (row.xzqh === '汇总') {
        if (columnIndex === 0) {
          return {
            rowspan: 2,
            colspan: 1
          }
        }
      }
      if (row.xzqh === '汇总1' || row.xzqh === '汇总2') {
        console.log(this.entType)
        if(this.entType === '' || this.entType === 'qy'){
          if (columnIndex === 0) {
            return {
              rowspan: 0,
              colspan: 0
            }
          } else if (columnIndex === 1 || columnIndex === 2) {
            return {
              rowspan: 1,
              colspan: 5
            }
          } else if (columnIndex === 4 || columnIndex === 5 || columnIndex === 7 || columnIndex === 8) {
            return {
              rowspan: 1,
              colspan: 3
            }
          } else if (columnIndex === 9 || columnIndex === 0 || columnIndex === 3 || columnIndex === 6) {
            return {
              rowspan: 1,
              colspan: 1
            }
          } else {
            return {
              rowspan: 0,
              colspan: 0
            }
          }
        }else if(this.entType === 'gt' || this.entType === 'nz'){
          if (columnIndex === 0) {
            return {
              rowspan: 0,
              colspan: 0
            }
          } else if (columnIndex === 1 || columnIndex === 2) {
            return {
              rowspan: 1,
              colspan: 3
            }
          } else if (columnIndex === 3) {
            return {
              rowspan: 1,
              colspan: 1
            }
          } else {
            return {
              rowspan: 0,
              colspan: 0
            }
          }
        }
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (row.xzqh === '汇总') {
        if(this.entType === '' || this.entType === 'qy'){
          if (columnIndex === 1 || columnIndex === 2) {
            return {
              rowspan: 1,
              colspan: 5
            }
          } else if (columnIndex === 4 || columnIndex === 5 || columnIndex === 7 || columnIndex === 8) {
            return {
              rowspan: 1,
              colspan: 3
            }
          } else if (columnIndex === 9 || columnIndex === 0 || columnIndex === 3 || columnIndex === 6) {
            return {
              rowspan: 1,
              colspan: 1
            }
          } else {
            return {
              rowspan: 0,
              colspan: 0
            }
          }
        }else if(this.entType === 'gt' || this.entType === 'nz'){
          if (columnIndex === 0) {
            return {
              rowspan: 1,
              colspan: 1
            }
          } else
          if (columnIndex === 1 || columnIndex === 2) {
            return {
              rowspan: 1,
              colspan: 3
            }
          } else if (columnIndex === 3) {
            return {
              rowspan: 1,
              colspan: 1
            }
          } else {
            return {
              rowspan: 0,
              colspan: 0
            }
          }
        }
      }
    },
    search2Fail() {
      this.isFH = false
      this.datashow = this.AbnStatisticsDtos
    },
    search2(row) {
      if (row.xzqh === '广州市市场监督管理局') {
        this.isFH = true
        this.datashow = this.AbnStatisticsDtos2
      }
    },
    search() {
      this.AbnStatisticsDtos = []
      this.isFH = false
      this.loading = true
      queryStatistics(this.searchForm).then(res => {
        if (res.code === 0) {
          this.loading = false
          if (res.data.abnStatisticsDtos1.length === 2) { // 区局数据
            this.isqj = true // 更改标识
            res.data.abnStatisticsDtos1.forEach(each => {
              if (each.xzqh === '汇总') {
                each.qylr1 = each.qylrzs
                each.qylr2 = each.qyyczs
                each.qylr4 = each.qyyy
                each.qylr3 = each.gtlrzs
                each.qylr5 = each.gtyczs
                each.qyyc1 = each.gtyy
                each.qyyc2 = each.nzlrzs
                each.qyyc4 = each.nzyczs
                each.qyyc3 = each.nhyy
                each.gtlr1 = each.gtlrzs
                each.gtlr3 = each.gtyczs
                each.gtlr2 = each.gtyy
                each.nhlr1 = each.nzlrzs
                each.nhlr3 = each.nzyczs
                each.nhlr2 = each.nhyy
              }
            })
            this.AbnStatisticsDtos = res.data.abnStatisticsDtos1
            this.datashow = this.AbnStatisticsDtos
          } else {
            res.data.abnStatisticsDtos1.forEach(each => {
              if (each.xzqh === '汇总1') {
                each.qylr1 = each.qylrzs
                each.qylr2 = each.qyyczs
                each.qylr4 = each.qyyy
                each.qylr3 = each.gtlrzs
                each.qylr5 = each.gtyczs
                each.qyyc1 = each.gtyy
                each.qyyc2 = each.nzlrzs
                each.qyyc4 = each.nzyczs
                each.qyyc3 = each.nhyy
                each.gtlr1 = each.gtlrzs
                each.gtlr3 = each.gtyczs
                each.gtlr2 = each.gtyy
                each.nhlr1 = each.nzlrzs
                each.nhlr3 = each.nzyczs
                each.nhlr2 = each.nhyy
              }
            })
            this.AbnStatisticsDtos = res.data.abnStatisticsDtos1
            this.datashow = this.AbnStatisticsDtos
            res.data.abnStatisticsDtos2.forEach(each => {
              if (each.xzqh === '广州市市场监督管理局') {
                each.xzqh = '汇总'
              }
              if (each.xzqh === '汇总2') {
                each.qylr1 = each.qylrzs
                each.qylr2 = each.qyyczs
                each.qylr4 = each.qyyy
                each.qylr3 = each.gtlrzs
                each.qylr5 = each.gtyczs
                each.qyyc1 = each.gtyy
                each.qyyc2 = each.nzlrzs
                each.qyyc4 = each.nzyczs
                each.qyyc3 = each.nhyy
                each.gtlr1 = each.gtlrzs
                each.gtlr3 = each.gtyczs
                each.gtlr2 = each.gtyy
                each.nhlr1 = each.nzlrzs
                each.nhlr3 = each.nzyczs
                each.nhlr2 = each.nhyy
              }
            })
            this.AbnStatisticsDtos2 = res.data.abnStatisticsDtos2
          }
        } else {
          this.$message.error(res.msg)
        }
      }).catch(_ => {
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/.bgColor{
  background: #7cb1ff!important;
}
.main-content {
  padding: 10pt;

  .arrow{
    color: #3296FA;
    cursor: pointer;
    margin-right: 16px;
  }
  .search-box{
    padding:10px 0;
    .search-field{
      display:flex;
      width:100%;
      >p{
        margin:3px 16px 10px 0;
        font-family: Microsoft YaHei, Microsoft YaHei-Bold;
        color: #666;
        font-size:16PX;
        width:200px;
        text-align: right;
      }
    }
    .el-col{
      margin-bottom:15px;
    }
  }

  .content-header {
    background: white;

    .label-name_1u {
      text-align: right;
      display: inline-block; //转成行内快，才可定义宽度
      width: 180px;
      font-size: 16px;
    }

    .search-input-box_1u {
      display: inline-block;
      width: calc(100% - 230px);
    }

    .label-name_2u {
      text-align: right;
      display: inline-block; //转成行内快，才可定义宽度
      width: 210px;
      font-size: 16px;
    }

    .search-input-box_2u {
      display: inline-block;
      width: calc(65% - 120px);
    }

    .label-name_u {
      text-align: right;
      display: inline-block; //转成行内快，才可定义宽度
      width: 210px;
      font-size: 16px;
    }

    .search-input-box_u {
      display: inline-block;
      width: calc(65% - 120px);
    }

    .label-name_3u {
      text-align: right;
      display: inline-block; //转成行内快，才可定义宽度
      width: 180px;
      font-size: 16px;
    }

    .label-name_1 {
      text-align: right;
      display: inline-block; //转成行内快，才可定义宽度
      width: 100px;
      font-size: $font-size-content-text;
    }

    .search-input-box_1 {
      display: inline-block;
      width: calc(100% - 110px);
    }

    .label-name_2 {
      text-align: right;
      display: inline-block; //转成行内快，才可定义宽度
      width: 110px;
      font-size: $font-size-content-text;
    }

    .search-input-box_2 {
      display: inline-block;
      width: calc(100% - 120px);
    }

    .label-name_3 {
      text-align: right;
      display: inline-block; //转成行内快，才可定义宽度
      width: 140px;
      font-size: $font-size-content-text;
    }

    .custom {
      width: calc(100% - 150px);
    }

    .search-input {
      cursor: pointer;
    }

    .body {
      padding: 20px 0;
      border-top: none;
      margin-bottom: 10PX;
    }
  }

  .content-body {
    background: white;
    margin-bottom: 100px;
  }

  .header {
    /* height:50PX; */
    /* padding:15PX 10PX; */
    .title {
      font-weight: 600;
      /* font-size: 1.2rem; */
    }
  }

  .page-box {
    margin: 10PX;
    text-align: right
  }

  // .mb10{
  //   margin-bottom: 10px;
  // }
  .btn-box {
    display: inline-block;
    padding-left: 10px;
  }

  .prompt {
    color: #F53633;
  }
}

.tab {
  margin-bottom: 10px;
}

.fixed-bottom {
  position: fixed;
  width: 100%;
  bottom: -10px;
  right: 0;
  z-index: 99;
  padding: 10px;
  /* background: #8bbef6; */
  border-radius: 5px 5px 0 0;
  background: linear-gradient(to bottom, #fff 0%, #8bbef6 100%);
  box-shadow: 1px -4px 4px rgba(0, 21, 41, 0.08);
}

.content-body {
  background: #fff;

  /deep/ .el-table .warning-row {
    background: #eaf4fe !important;
  }

  /deep/ .el-table .sort-caret.descending {
    border-top-color: #fff !important;
  }

  /deep/ .el-table .sort-caret.ascending {
    border-bottom-color: #fff !important;
  }
}
</style>

<style lang="scss">

.el-input__inner {
  height: 28px !important;
}

.fixed-bottom {
  .el-pagination {
    text-align: right !important;
  }
}
</style>

<style lang="scss">
  .market.search-result{
    .el-table__row{
      cursor: pointer;
    }
  }
</style>

