<template>
  <div class="content">
    <div v-loading.fullscreen.lock="loadingFull" />
    <div class="search_box flexS">
      <div v-for="(item, idx) in categoryList" :key="idx">
        <el-select v-model="params[item.mark]" :placeholder="item.name" class="select" @change="toSearch()" clearable
          @clear="toSearch()">
          <el-option v-for="item in item.children" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </div>
      <el-input v-model="params.search_name" clearable placeholder="企业名称" prefix-icon="el-icon-search"
        @keyup.enter.native="toSearch()" @clear="toSearch()" />
      <el-button type="primary" icon="el-icon-search" :loading="loadingFull" @click="toSearch()">搜索</el-button>
      <el-upload class="upload-demo" action="" :http-request="uploadFile" :multiple="false" :show-file-list="false">
        <el-button type="primary" style="margin:0 10px;">导入</el-button>
      </el-upload>

      <el-button type="primary" icon="el-icon-edit" class="filter-item" @click="handleAdd">添加</el-button>
    </div>
    <div class="table_box">
      <el-table ref="list" :data="listData" align="center" :header-cell-style="{ background: '#f8fbfc' }"
        class="tableBorder" border stripe>
        <el-table-column prop="name" label="企业名称" align="center" />
        <el-table-column label="行业类型" prop="industry" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.industry && row.industry.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="数据总览类型" prop="datatype" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.datatype && row.datatype.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="高新技术类型" prop="high_tech" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.high_tech && row.high_tech.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所在园区" prop="parks" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.parks && row.parks.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否龙头企业" prop="is_leading" align="center">
          <template slot-scope="{ row }">
            <el-tag :type="row.is_leading == 1 ? 'success' : 'danger'">
              {{ row.is_leading == 1 ? "是" : "不是" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否第三方机构" prop="is_service" align="center">
          <template slot-scope="{ row }">
            <el-tag :type="row.is_service == 1 ? 'success' : 'danger'">
              {{ row.is_service == 1 ? "是" : "不是" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否研发机构" prop="is_research" align="center">
          <template slot-scope="{ row }">
            <el-tag :type="row.is_research == 1 ? 'success' : 'danger'">
              {{ row.is_research == 1 ? "是" : "不是" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" align="center" />
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-container">
      <pagination :total="listTotal" :limit="params.page_size" :page="params.page_index" @pagination="changePagination"
        @update:limit="changePagination" />
    </div>
  </div>
</template>

<script>
import { companyList, deleteCompany, importCompany, companyCategory } from '@/api/company'

export default {
  name: 'CompanyList',
  data() {
    return {
      params: {
        page_size: 20,
        page_index: 1,
        search_name: '',
        industry: '',
        tech: '',
        type: '',
        parks: ''
      },
      listData: [], // 商品列表
      listTotal: 0,
      dialogVisible: false,
      loadingFull: false,
      industryOptions: [],
      techOptions: [],
      typeOptions: [],
      parksOptions: [],
      categoryList: []
    }
  },
  created() {
    this.getList()
    this.getCategory()
  },
  methods: {
    // 获取列表
    async getCategory() {
      try {
        const res = await companyCategory()
        const { msg, code, data } = res
        if (code === 200) {
          this.categoryList = data
          // for (let i = 0; i < data.length; i++) {
          //   if (data[i].mark === "industry") {
          //     data[i].children.map(j => {
          //       this.industryOptions.push({ name: j.name, value: j.id })
          //     })
          //   }else if(data[i].mark === '')
          // }
        } else {
          this.$message.error(msg)
        }
      } catch (e) {
        console.log(`获取失败:${e}`)
      }
    },
    // 搜索
    toSearch() {
      this.params.page_index = 1
      this.getList()
    },
    // 添加企业
    handleAdd() {
      this.$router.push({
        name: 'EditCompany'
      })
    },
    // 导入
    uploadFile(params) {
      const file = params.file
      var formData = new FormData()
      formData.append('file', file)
      const isExcel = file.name.endsWith('.xlsx') || file.name.endsWith('.xls')
      console.log(file.name, 'file')
      if (!isExcel) {
        this.$message.error('仅支持上传 .xlsx 或 .xls 格式的文件!')
        return
      }
      importCompany(formData).then(res => {
        if (res.code === 200) {
          this.$message.success('导入成功！')
          this.getList()
        } else {
          this.$message.error(res.message || '导入失败！')
        }
      })
    },
    // 获取列表
    async getList() {
      this.loadingFull = true
      try {
        const res = await companyList(this.params)
        const { message, code, data } = res
        if (code === 200) {
          const { list, total } = data
          this.listData = list
          this.listTotal = total
        } else {
          this.$message.error(message)
        }
      } catch (e) {
        console.log(`获取企业列表失败:${e}`)
        this.loadingFull = false
      }
      this.loadingFull = false
    },
    // 删除确认
    handleDelete(row) {
      const { id } = row
      this.$confirm('确认要删除该企业吗?', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteEvent(id)
      })
    },
    // 删除企业
    async deleteEvent(id) {
      const res = await deleteCompany({ id })
      try {
        const { message, code } = res
        if (code === 200) {
          this.$message.success('删除成功！')
          this.getList()
        } else {
          this.$message.success(message || '删除失败！')
        }
      } catch (e) {
        this.$message.error('请求失败！')
      }
    },

    // 分页页码变化
    changePagination(data) {
      const { page, limit } = data
      this.params.page_size = limit
      this.params.page_index = page
      this.getList()
    },

    // 编辑企业信息
    handleEdit(row) {
      this.$router.push({
        name: 'EditCompany',
        params: { id: row.id }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.size_remark {
  .remark_title {
    font-size: 18px;
    font-weight: bold;
  }

  .remark_con {
    display: flex;
    justify-content: flex-start;
    align-items: center;

    span,
    div {
      width: 33.33%;
      text-align: center;
      font-size: 16px;
    }

    div {
      font-size: 18px;
      font-weight: bold;
      margin-top: 10px;
    }
  }
}

.size_img {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  margin-top: 40px;

  .img_title {
    margin: 5px 20px 0 0;
    font-size: 16px;
    font-weight: bold;
  }
}

.img_box {
  margin-top: 15px;
  width: 145px;
  height: 145px;
  position: relative;
  list-style: none;

  img {
    display: block;
    width: 145px;
    height: 145px;
  }
}

.img_box:hover .remove {

  display: block;
}

.remove {
  display: none;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  background: #000;
  opacity: 0.6;
  height: 30px;
  text-align: center;
  line-height: 30px;
  z-index: 1000;
  cursor: pointer;
}

.el-tag {
  margin: 5px 0 0 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.filter-container {
  margin-bottom: 20px;
}

.batch-container {
  margin-top: 20px;
}

.goods_info {
  width: 375px;
  margin: 0 auto;

  .banner_img {
    display: block;
    width: 100%;
    height: auto;
  }
}

.size_box {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;

}
</style>

<style lang="scss">
.goodsInfoDesc {
  img {
    display: block;
    width: 100%;
    height: auto;
  }
}
</style>
