<template>
  <div class="app-container">
    <div>
     
      <el-input v-model="listQuery.search"
                placeholder="组织名称/工厂编号"
                style="width: 300px;"
                class="filter-item"
                @keyup.enter.native="handleFilter" />
      <el-button class="filter-item"
                 type="primary"
                 icon="el-icon-search"
                 @click="handleFilter">搜索</el-button>
      <el-button class="filter-item"
                 style="margin-left: 10px;"
                 type="primary"
                 icon="el-icon-refresh-left"
                 @click="resetFilter">刷新重置</el-button>
    </div>
    <div style="margin-top:6px">
      <el-button type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
    </div>


    <el-table v-loading="listLoading"
              :data="enterpriseList.results"
              style="width: 100%;margin-top:10px;"
              border
              fit
              stripe
              highlight-current-row
              max-height="1000">
      <el-table-column fixed="left" type="index" width="50" />
      <el-table-column fixed="left" width="150" label="组织查询码">
        <template slot-scope="scope">
          {{ scope.row.query_code }}
        </template>
      </el-table-column>
     
      <el-table-column width="150" label="工厂编号">
        <template slot-scope="scope">
          {{ scope.row.code }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="组织名称">
        <template slot-scope="scope">
          {{scope.row.name  }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="英文名">
        <template slot-scope="scope">
          {{ scope.row.ename }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="法人">
        <template slot-scope="scope">
          {{ scope.row.legal }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="机构类型">
        <template slot-scope="scope" v-if="scope.row.type">
          {{ scope.row.type.name }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="组织员工数">
        <template slot-scope="scope">
          {{ scope.row.person_count }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="建厂时间">
        <template slot-scope="scope">
          {{ scope.row.build_time }}
        </template>
      </el-table-column>

      <el-table-column width="150" label="经济行业">
        <template slot-scope="scope" v-if="scope.row.economy_class">
          {{ scope.row.economy_class.name }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="经营范围">
        <template slot-scope="scope">
          {{ scope.row.business_type }}
        </template>
      </el-table-column>

      <el-table-column width="150" label="组织机构代码">
        <template slot-scope="scope">
          {{ scope.row.credit_code }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="注册资本">
        <template slot-scope="scope">
          {{ scope.row.gassets }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="县/区">
        <template slot-scope="scope">
          {{ scope.row.region_code }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="常用联系人">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="常用联系人电话">
        <template slot-scope="scope">
          {{ scope.row.linkman1_tel }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="常用联系人手机">
        <template slot-scope="scope">
          {{ scope.row.linkman1_mobile }}
        </template>
      </el-table-column>
      <el-table-column width="150" label="常用联系人职务">
        <template slot-scope="scope">
          {{ scope.row.linkman1_duty }}
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="right" label="操作" width="260px">
        <template slot-scope="scope">
          <el-button type="primary"
                     size="small"
                     :disabled="!checkPermission(['enterprise_update'])"
                     @click="handleUpdate(scope)">编辑</el-button>

          <el-button type="danger"
                     size="small"
                     :disabled="!checkPermission(['enterprise_delete'])"
                     @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="enterpriseList.count>0"
                :total="enterpriseList.count"
                :page.sync="listQuery.page"
                :limit.sync="listQuery.page_size"
                @pagination="getList" />
  </div>
</template>
<script>
import { getEnterpriseList,deleteEnterprise } from "@/api/enterprise"
import Pagination from "@/components/Pagination"
import checkPermission from '@/utils/permission'
export default {
  components: { Pagination },
  data() {
    return {
      enterpriseList: {count:0},
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20
      },
     
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getEnterpriseList(this.listQuery).then(response => {
        if (response.data) {
          this.enterpriseList = response.data
        }
        this.listLoading = false
      });
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20
      }
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
   handleCreate() {
      this.$router.push({path:"/crm/enterprise/create"})
    },
    handleUpdate(scope) {
      this.$router.push({path:"/crm/enterprise/update",query:{id:scope.row.id}})
    },
    handleDelete(scope) {
      this.$confirm('确定删除本条数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteEnterprise(scope.row.id).then(res=>{
        this.$message.success('删除成功')
              this.getList()
    })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    
},
  },
  
};
</script>
