<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-news__enterprisereport}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="所属机构">
          <el-cascader v-model="ids"
                       :options="options"
                       :props="{ checkStrictly: true }"
                       clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="所属年度"
                      prop="year">
          <el-date-picker v-model="dataForm.year"
                          type="year"
                          value-format="yyyy"
                          format="yyyy"
                          placeholder="选择年度">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所属季度">
          <el-select v-model="dataForm.quarter" placeholder="所属季度" clearable>
            <el-option
                v-for="item in enterpriseQuarterList"
                :key="item.dictName"
                :label="item.dictName"
                :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司名称" prop="enterpriseName">
          <el-input v-model.trim="dataForm.enterpriseName" placeholder="请输入关键字模糊搜索" clearable ></el-input>
        </el-form-item>
        <el-form-item label="法人或联络人" prop="legalPerson">
          <el-input v-model.trim="dataForm.legalPerson" placeholder="请输入关键字模糊搜索" clearable ></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="concat">
          <el-input v-model.trim="dataForm.concat" placeholder="请输入关键字模糊搜索" clearable ></el-input>
        </el-form-item>
        <el-form-item label="公司状态">
          <el-select v-model="dataForm.enterpriseStatus" placeholder="公司状态" clearable>
            <el-option
                v-for="item in enterpriseStatusList"
                :key="item.dictName"
                :label="item.dictName"
                :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column prop="grid" label="网格" header-align="center" align="center"></el-table-column>
        <el-table-column prop="year" label="年度" header-align="center" align="center"></el-table-column>
        <el-table-column prop="quarter" label="季度" header-align="center" align="center" :formatter="showEnterpriseQuarterFormatter"></el-table-column>
        <el-table-column prop="enterpriseName" label="公司名称" header-align="center" align="center"></el-table-column>
        <el-table-column prop="legalPerson" label="法人或联络人" header-align="center" align="center"></el-table-column>
        <el-table-column prop="concat" label="联系电话" header-align="center" align="center"></el-table-column>
        <el-table-column prop="enterpriseStatus" label="公司状态" header-align="center" align="center" :formatter="showEnterpriseStatusFormatter" ></el-table-column>
        <el-table-column prop="enterpriseIncome" label="营业收入" header-align="center" align="center"></el-table-column>
        <el-table-column prop="enterprisePeopleNum" label="用工人数" header-align="center" align="center"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button  type="text" size="mini" @click="detailHandle(scope.row.id)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <enterprisereport-detail v-if="detailVisible" ref="enterprisereportDetail" @refreshDataList="getDataList"></enterprisereport-detail>

    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './enterprisereport-add-or-update'
import EnterprisereportDetail from './enterprisereport-detail'

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/custom/enterprisereport/page',
        getDataListIsPage: true,
        deleteURL: '/custom/enterprisereport',
        deleteIsBatch: true
      },
      dataForm: {
        id: '',
        streetId: '',
        communityId: '',
        gridId: ''
      },
      ids: [],
      options: [],
      enterpriseStatusList: [],
      enterpriseQuarterList: [],
      detailVisible: false
    }
  },
  created: function () {
    this.$http
      .get(`/sys/user/deptOptions/getByLoginUser`)
      .then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      })
      .catch(() => { })
    this.getDataList()
    this.getEnterpriseStatusList()
    this.getEnterpriseQuarterList()
  },
  components: {
    AddOrUpdate,
    EnterprisereportDetail
  },
  watch: {
    'ids': function (val) {
      if (val.length === 0) {
        this.dataForm.streetId = ''
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
      }
      if (val.length === 1) {
        this.dataForm.streetId = this.ids[0]
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
      }
      if (val.length === 2) {
        this.dataForm.streetId = this.ids[0]
        this.dataForm.communityId = this.ids[1]
        this.dataForm.gridId = ''
      }
      if (val.length === 3) {
        this.dataForm.streetId = this.ids[0]
        this.dataForm.communityId = this.ids[1]
        this.dataForm.gridId = this.ids[2]
      }
    }
  },
  methods: {
    showEnterpriseStatusFormatter (row, column) {
      if (row.enterpriseStatus) {
        let dict = this.enterpriseStatusList.filter(item => item.dictValue === row.enterpriseStatus)[0]
        if (dict) {
          return dict.dictName
        }
      }
      return ''
    },
    showEnterpriseQuarterFormatter (row, column) {
      if (row.quarter) {
        let dict = this.enterpriseQuarterList.filter(item => item.dictValue === row.quarter)[0]
        if (dict) {
          return dict.dictName
        }
      }
      return ''
    },
    getEnterpriseStatusList () {
      this.$http
        .get(`/sys/dict/listSimple/enterprise_status`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.enterpriseStatusList = res.data
        })
        .catch(() => {})
    },
    getEnterpriseQuarterList () {
      this.$http
        .get(`/sys/dict/listSimple/enterprise_quarter`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.enterpriseQuarterList = res.data
        })
        .catch(() => {})
    },
    detailHandle (id) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.enterprisereportDetail.dataForm.id = id
        this.$refs.enterprisereportDetail.init()
      })
    }
  }
}
</script>
