<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-input v-model="searchParam.nameLike" clearable size="small" placeholder="企业名称" style="width: 200px;" class="filter-item" />
        <el-select v-model="searchParam.auditState" placeholder="审核状态" clearable size="small" class="filter-item">
          <el-option v-for="e in enterpriseApi.auditStates()" :key="e.key" :label="e.value" :value="e.key" />
        </el-select>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="searchSubmit">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="searchParamReset">重置</el-button>
        <el-tooltip class="item" effect="dark" content="按当前查询条件导出" placement="top">
          <el-button :loading="exporting" class="filter-item" size="mini" type="info" plain icon="el-icon-download" @click="exportSubmit">导出</el-button>
        </el-tooltip>
      </div>
    </div>
    <!--企业信息窗-->
    <el-dialog append-to-body :close-on-click-modal="true" :visible.sync="dialog1Show" :title="'企业信息'" width="1160px">
      <el-form ref="mainForm" :model="dialog1Data" size="mini" label-width="170px">
        <el-divider content-position="left">基础信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="企业名称：" prop="name">
              <span>{{ dialog1Data.name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="统一社会信用代码：" prop="idCode">
              <span>{{ dialog1Data.idCode }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="行政区域：" prop="administrativeAreaCode">
              <span>{{ dialog1Data.administrativeAreaName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司地址：" prop="address">
              <span>{{ dialog1Data.address }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="单位性质：" prop="dict1">
              <span>{{ dialog1Data.dict1Name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经济类型：" prop="dict2">
              <span>{{ dialog1Data.dict2Name }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="行业类别：" prop="dict3">
              <span>{{ dialog1Data.dict3Name }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否有注册资本：" prop="hasRegisteredCapital">
              <span>
                {{ dialog1Data.hasRegisteredCapital === true ? '是' : '' }}
                {{ dialog1Data.hasRegisteredCapital === false ? '否' : '' }}
              </span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="dialog1Data.hasRegisteredCapital" label="注册资本：" prop="registeredCapital">
              <span>{{ dialog1Data.registeredCapital }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="18">
            <el-form-item label="经营范围：" prop="businessScope">
              <span>{{ dialog1Data.businessScope }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">企业资质</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="成立日期：" prop="setupDate">
              <span>{{ dialog1Data.setupDate }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="经营期限（起）：" prop="beginLimitDate">
              <span>{{ dialog1Data.beginLimitDate }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营期限（止）：" prop="endLimitDate">
              <span>{{ dialog1Data.endLimitDate }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="生产经营方式：" prop="dict4">
              <span>{{ dialog1Data.dict4Name }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="企业规模：" prop="dict5">
              <span>{{ dialog1Data.dict5Name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否规模以上企业：" prop="dict6">
              <span>{{ dialog1Data.dict6Name }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="隶属层级：" prop="dict7">
              <span>{{ dialog1Data.dict7Name }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="责任人：" prop="liablePerson">
              <span>{{ dialog1Data.liablePerson }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任人电话：" prop="liablePersonPhone">
              <span>{{ dialog1Data.liablePersonPhone }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="生产经营地址：" prop="businessAddress">
              <span>{{ dialog1Data.businessAddress }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">证书</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="采矿许可证：" prop="_workLicenseFileList">
              <div v-for="file in dialog1Data._workLicenseFileList" :key="file.url">
                <el-link type="primary" @click="openFile(file)">{{ file.name }}</el-link>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div>
              <el-form-item label="采矿许可有效期：" prop="workLicenseBeginDate">
                <span>{{ dialog1Data.workLicenseBeginDate }}</span>
              </el-form-item>
            </div>
            <div>
              <el-form-item label="至：" prop="workLicenseEndDate">
                <span>{{ dialog1Data.workLicenseEndDate }}</span>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="安全生产许可证：" prop="_safetyLicenseFileList">
              <div v-for="file in dialog1Data._safetyLicenseFileList" :key="file.url">
                <el-link type="primary" @click="openFile(file)">{{ file.name }}</el-link>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div>
              <el-form-item label="安全许可有效期：" prop="safetyLicenseBeginDate">
                <span>{{ dialog1Data.safetyLicenseBeginDate }}</span>
              </el-form-item>
            </div>
            <div>
              <el-form-item label="至：" prop="safetyLicenseEndDate">
                <span>{{ dialog1Data.safetyLicenseEndDate }}</span>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-divider content-position="left">位置</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="经度：" prop="longitude">
              <span>{{ dialog1Data.longitude }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度：" prop="latitude">
              <span>{{ dialog1Data.latitude }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="18">
            <el-form-item v-show="dialog1Data.longitude && dialog1Data.latitude" label="位置地图：">
              <AMapSearchPoint :is-search="false" :point="mapPoint" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!--审核确认窗-->
    <el-dialog append-to-body :close-on-click-modal="true" :visible.sync="dialog2Show" :title="'请确认'" width="580px">
      <span>确认要</span>
      <span v-show="dialog2Data.auditState===2" style="color:#66cb71;font-weight:bold;">通过</span>
      <span v-show="dialog2Data.auditState===3" style="color:#e75851;font-weight:bold;">拒绝</span>
      <span style="font-weight:bold;">{{ dialog2Data.name }}</span>
      <span>的申请吗？</span>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="dialog2Show=false">取消</el-button>
        <el-button :loading="dialog2Saving" type="primary" @click="dialog2Submit">确认</el-button>
      </div>
    </el-dialog>
    <!--表格-->
    <el-table ref="table" v-loading="searching" :data="tableData" row-key="id" @select="tableSelectChange" @select-all="tableSelectAll" @selection-change="tableSelectionChange">
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column :show-overflow-tooltip="true" label="编号" prop="id" width="100px" align="center" />
      <el-table-column :show-overflow-tooltip="true" label="企业名称" prop="name" align="center" />
      <el-table-column :show-overflow-tooltip="true" label="审核状态" prop="auditState" align="center">
        <template slot-scope="scope">
          <span v-for="e in enterpriseApi.auditStates()" :key="e.key">
            <span v-if="e.key==scope.row.auditState">{{ e.value }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="统一社会信用代码" prop="idCode" align="center" />
      <el-table-column :show-overflow-tooltip="true" label="成立日期" prop="setupDate" align="center" />
      <el-table-column :show-overflow-tooltip="true" label="公司地址" prop="address" align="center" />
      <el-table-column :show-overflow-tooltip="true" label="法定代表人" prop="liablePerson" align="center" />
      <el-table-column :show-overflow-tooltip="true" label="法人电话" prop="liablePersonPhone" align="center" />
      <el-table-column :show-overflow-tooltip="true" label="采矿许可证有效期至" prop="workLicenseEndDate" align="center">
        <template slot-scope="scope">
          <span :style="{'color':dateToNow(scope.row.workLicenseEndDate)<0?'#F56C6C':dateToNow(scope.row.workLicenseEndDate)<30?'#E6A23C':null}">
            {{ scope.row.workLicenseEndDate }}
          </span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="安全生产许可证有效期至" prop="safetyLicenseEndDate" align="center">
        <template slot-scope="scope">
          <span :style="{'color':dateToNow(scope.row.safetyLicenseEndDate)<0?'#F56C6C':dateToNow(scope.row.safetyLicenseEndDate)<30?'#E6A23C':null}">
            {{ scope.row.safetyLicenseEndDate }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button v-show="scope.row.auditState!=1" size="mini" round @click="showEnterpriseInfo(scope.row.id)">查看详情</el-button>
          <el-button v-show="scope.row.auditState==1" size="mini" round @click="auditBtn(scope.row, 2)">通过</el-button>
          <el-button v-show="scope.row.auditState==1" size="mini" round @click="auditBtn(scope.row, 3)">不通过</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      :page-sizes="[10, 20, 50, 100]"
      :current-page.sync="searchParam.page"
      :page-size.sync="searchParam.count"
      :total="searchResult.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="searchSubmit"
      @current-change="searchSubmit"
    />
  </div>
</template>

<script>
import enterpriseApi from '@/api/kd/enterprise'
import AMapSearchPoint from '@/components/AMap/AMapSearchPoint'
import { mapGetters } from 'vuex'
import { downloadFile } from '@/utils'

const searchParam = {// 默认搜索条件
  page: 1,
  count: 10,
  searchCount: true
}
export default {
  name: 'CEnterprise', // 本页面名
  components: { AMapSearchPoint },
  data() {
    return {
      searching: false,
      exporting: false,
      enterpriseApi: enterpriseApi,
      searchParam: {},
      searchResult: {
        total: 0,
        results: [] // 搜索结果
      },
      dialog1Show: false,
      dialog1Data: {},
      dialog2Show: false,
      dialog2Saving: false,
      dialog2Data: {}
    }
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ]),
    tableData() {
      // 将返回结果转换为可展示文本
      return this.searchResult.results
    },
    mapPoint() {
      if (this.dialog1Data.longitude && this.dialog1Data.latitude) {
        return [this.dialog1Data.longitude * 1, this.dialog1Data.latitude * 1]
      }
      return null
    }
  },
  mounted() {
    this.searchParamReset()
    this.searchSubmit()
  },
  methods: {
    searchParamReset() {
      this.searchParam = JSON.parse(JSON.stringify(searchParam))
    },
    searchSubmit() {
      this.searching = true
      enterpriseApi.getPage(this.searchParam).then(response => {
        this.searchResult = response
        this.searching = false
      }).catch(() => {
        this.searching = false
      })
    },
    exportSubmit() {
      this.exporting = true
      enterpriseApi.export(this.searchParam).then(response => {
        downloadFile(response, '企业信息', 'xlsx')
        this.exporting = false
      }).catch(() => {
        this.exporting = false
      })
    },
    auditBtn(enterprise, auditState) {
      this.dialog2Data = JSON.parse(JSON.stringify(enterprise))
      this.dialog2Data.auditState = auditState
      this.dialog2Show = true
    },
    dialog2Submit() {
      this.doAudit(this.dialog2Data)
    },
    showEnterpriseInfo(enterpriseId) {
      enterpriseApi.getById(enterpriseId).then(response => {
        this.dialog1Data = response
        if (this.dialog1Data.workLicenseFilePath) {
          this.dialog1Data._workLicenseFileList = JSON.parse(this.dialog1Data.workLicenseFilePath)
        }
        if (this.dialog1Data.safetyLicenseFilePath) {
          this.dialog1Data._safetyLicenseFileList = JSON.parse(this.dialog1Data.safetyLicenseFilePath)
        }
        this.dialog1Show = true
      })
    },
    tableSelectChange(selection, row) {
      console.log('select', selection, row, this.tableData)
    },
    tableSelectAll(selection) {
      console.log('selectAll', selection, this.tableData)
    },
    tableSelectionChange(selection) {
      console.log('selectionChange', selection, this.tableData)
    },
    doAudit(enterprise) {
      this.dialog2Saving = true
      enterpriseApi.audit(enterprise).then(() => {
        this.dialog2Saving = false
        this.dialog2Show = false
        this.$message({
          message: '审核完成',
          type: 'success'
        })
        this.searchSubmit()
      }).catch(() => {
        this.dialog2Saving = false
        this.dialog2Show = false
      })
    },
    openFile(file) {
      if (file.url) {
        window.open(this.baseApi + '/file' + file.url)
      } else if (file.response && file.response.result) {
        window.open(this.baseApi + '/file' + file.response.result)
      }
    },
    dateToNow(dateString) {
      return (new Date(dateString).getTime() - new Date().getTime()) / (1000 * 60 * 60 * 24)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
  ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
</style>
