<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" label-width="100px">
      <el-form-item label="省" prop="provinceId">
        <el-select v-model="queryParams.provinceId" placeholder="请选择省" @change="getCityData" filterable style="width: 200px;">
          <el-option v-for="(item,index) in provinceDataList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="市" prop="cityId">
        <el-select v-model="queryParams.cityId" placeholder="请选择市" @change="getDistrictData" filterable style="width: 200px;">
          <el-option v-for="(item, index) in cityDataList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="区" prop="districtId">
        <el-select v-model="queryParams.districtId" placeholder="请选择区" filterable style="width: 200px;">
          <el-option v-for="(item, index) in districtDataList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年份">
        <el-date-picker v-model="queryParams.year" type="year" placeholder="选择年" style="width: 200px;"></el-date-picker>
      </el-form-item>
      <el-form-item label="企业名称">
        <el-input v-model="queryParams.entName" clearable style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="社会信用代码">
        <el-input v-model="queryParams.license" style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="项目编号">
        <el-input v-model="queryParams.projectNo" style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="项目名称">
        <el-input v-model="queryParams.projectName" style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <h3 class="toolbar">
      <span class="title">列表</span>
      <div class="tool">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleBatchAdd">批量新增</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleDownloadMode">模板下载</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleUpload">一键上传</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleDownload">下载</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleBatchDelte">批量删除</el-button>
      </div>
    </h3>
    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="项目编号" prop="projectNo" width="120" align="center" />
      <el-table-column label="项目名称" prop="projectName" align="center" width="120"></el-table-column>
      <el-table-column label="企业名称" prop="entName" align="center" width="120"/>
      <el-table-column label="社会信用代码" prop="license" align="center" width="120" />
      <el-table-column label="项目年份" prop="projectYear" align="center" width="120" />
      <el-table-column label="项目跨年" prop="overYearFlag" align="center" width="120"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"></el-table-column>
      <el-table-column label="留存备查时间" prop="linkMobile" align="center" width="120"></el-table-column>
      <el-table-column label="资料完善度" prop="linkEmail" align="center" width="120"></el-table-column>
      <el-table-column label="负责状态" prop="createTime" align="center" width="160"></el-table-column>
      <el-table-column label="项目状态" align="center" width="120">
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)">查看</el-button>
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDownload(scope.row)">下载</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
      <add-project v-if="addProjectVisible" ref="addProject" @refreshData="getList()"></add-project>
  </div>
</template>

<script>
  import {getProvinceDataApi,getRegionChildrenApi} from "@/api/common/common"
  import {listProjectApi, deleteEntApi} from '@/api/enterprise/project/project'
  import {entStateOptions, getLabel, authStateOptions} from '@/utils/dataFormat'
  import AddProject from './components/addProject'
  export default {
    components: {
      AddProject
    },
    // ADMIN,GOV_PROV,GOV_CITY,GOV_DISTRI,ENT_ADMIN,ENT_TECH,ENT_ADMIN_TECH,ENT_FIN,ENT_ADMIN_FIN
    data () {
      return {
        loading: false,
        addProjectVisible: false,
        provinceDataList: [],
        cityDataList: [],
        districtDataList: [],
        entStateOptions: entStateOptions,
        authStateOptions: authStateOptions,
        // 模板状态
        // statusOptions: accessStatus,
        dateRange: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          provinceId: undefined,
          cityId: undefined,
          districtId: undefined
        },
        list: [],
        total: 0,
        open: false,
        openInfo: false,
      }
    },
    computed: {
      userType() {
        return this.$store.state.user.userType
      },
      roles() {
        return this.$store.state.user.roles
      }
    },
    created() {
      this.getList()
      this.getProvinceData()
    },
    methods: {
      //选择省份
      getCityData() {
        this.queryParams.cityId = ''
        this.queryParams.districtId = ''
        getRegionChildrenApi(this.queryParams.provinceId).then(res => {
          if(res.code == 200) {
            this.cityDataList = res.data
          }
        })
      },
      //选择市
      getDistrictData() {
        this.queryParams.districtId = ''
        getRegionChildrenApi(this.queryParams.cityId).then(res => {
          if(res.code == 200) {
            this.districtDataList = res.data
          }
        })
      },
      getProvinceData() {
        getProvinceDataApi().then(res => {
          if(res.code == 200) {
            this.provinceDataList = res.data
          }
        })
      },
      getList() {
        this.loading = true;
        if(this.userType == 'ENT_ADMIN' || this.userType == 'ENT_TECH' || this.userType == 'ENT_ADMIN_TECH' || this.userType == 'ENT_FIN' || this.userType == 'ENT_ADMIN_FIN') {
          listProjectApi(this.queryParams).then(response => {
            this.list = response.data.records;
            this.total = response.data.total;
            this.loading = false;
          });
        }

      },
      handleAdd() {
        this.addProjectVisible = true
        this.$nextTick(() => {
          this.$refs.addProject.init()
        })
      },
      handleEdit(row) {
        this.$router.push({name: 'editProject', query: {id: row.id, entId: row.entId}})
      },
      handleUpdate(row) {
        this.$router.push("/ent/edit/"+row.id)
      },
      // 审核
      handleAudit(row) {
        this.$router.push("/ent/info/"+row.id+'?type=audit')
      },
      // 查看
      handleView(row) {
        this.$router.push("/ent/info/"+row.id+'?type=view')
      },
      handleDelete(row) {
        const id = row.id;
        this.$confirm('是否确认删除该数据?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return deleteEntApi(id);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.msgSuccess("删除成功");
          })
      },
      // 重置
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 搜索
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      statusFormat(row, col, val, index) {
        return getLabel(accessStatus, val)
      },
      entSateFormat(row, col, val, index) {
        return getLabel(entStateOptions, val)
      },
      authStateFormat(row, col, val, index) {
        return getLabel(authStateOptions, val)
      }
    }
  }
</script>

<style>
</style>
