<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
      <template v-if="!checkRole(['entAdmin','entUser'])">
        <el-form-item label="省" prop="provinceId">
          <el-select v-model="queryParams.provinceId" placeholder="请选择省" @change="getCityData" filterable style="width: 200px;" clearable>
            <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;" clearable>
            <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;" clearable>
            <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="年份" prop="year">
          <el-date-picker
                type="years"
                v-model="years"
                placeholder="选择一个或多个年份" value-format="yyyy">
              </el-date-picker>
        </el-form-item>
        <el-form-item label="企业名称" prop="entName">
          <el-input v-model="queryParams.entName" clearable style="width: 200px;" placeholder="请填入企业名称"></el-input>
        </el-form-item>
        <el-form-item label="社会信用代码" prop="license">
          <el-input v-model="queryParams.license" style="width: 200px;" placeholder="请输入社会信用代码" clearable></el-input>
        </el-form-item>
      </template>
      <el-form-item label="项目编号" prop="projectNo">
        <el-input v-model="queryParams.projectNo" style="width: 200px;" placeholder="请输入项目编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" style="width: 200px;" placeholder="请输入项目名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="申报状态" prop="reportState">
        <el-select v-model="queryParams.reportState" filterable style="width: 200px;" clearable>
          <el-option value="1" label="待补充">待补充</el-option>
          <el-option value="2" label="待申报">待申报</el-option>
          <el-option value="3" label="待审核">待审核</el-option>
          <el-option value="4" label="已申报">已申报</el-option>
        </el-select>
      </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-download" size="mini" @click="handleDownloadBatch">下载</el-button>
        <el-button type="primary" plain icon="el-icon-delete" size="mini" @click="handleBatchDel">批量删除</el-button>
      </div>
    </h3>
    <el-table v-loading="loading" :data="list" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
      <el-table-column label="加计扣除年度" prop="projectYear" align="center" width="120"></el-table-column>
      <el-table-column label="项目名称" prop="projectName" align="center" width="180"></el-table-column>
      <el-table-column label="企业名称" prop="entName" align="center" width="180"/>
      <el-table-column label="统一社会信用代码" prop="creditCode" align="center" width="180" />
      <el-table-column label="是否跨年项目" prop="overYearFlag" :formatter="overYearFormat" align="center"></el-table-column>
      <!-- <el-table-column label="跨年申报"></el-table-column> -->
      <el-table-column label="是否追溯项目" prop="backFlag" :formatter="flagFormat" align="center"></el-table-column>
      <el-table-column label="项目起止时间" align="center" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.projectStartDate}} - {{scope.row.projectEndDate}}</span>
        </template>
      </el-table-column>
      <el-table-column label="资料完善度" prop="linkEmail" align="center" width="220">
        <template slot-scope="scope">
          <span>技术 {{scope.row.techRate}} 财务 {{scope.row.finRate}}</span>
        </template>
      </el-table-column>
      <el-table-column label="申报状态" prop="reportState" align="center" width="100" :formatter="reportStateFormat"></el-table-column>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDownload(scope.row)">下载</el-button>
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          <el-button type="text" @click="handleAuditAdmin(scope.row)" v-if="scope.row.reportState == 3">审核</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"/>
      <audit-project v-if="auditVisible" ref="auditProject" @refreshData="getList()"></audit-project>
  </div>
</template>

<script>
  import {getProvinceDataApi,getRegionChildrenApi,downloadProjectByIDApi} from "@/api/common/common"
  import {deleteProjectApi,listProjectApi} from '@/api/enterprise/project/project'
  import {projectAuditApi} from '@/api/admin/project/project'
  import {listProjectAdminApi} from '@/api/admin/project/project'
  import {entStateOptions, getLabel, authStateOptions, reportStateOptions,booleanOptions} from '@/utils/dataFormat'
  import AuditProject from '../../enterprise/project/components/audit.vue'
  import { downLoadZip } from "@/utils/zipdownload";
  import { checkRole } from "@/utils/permission"; // 权限判断函数
  export default {
    components: {
      AuditProject
    },
    data () {
      return {
        years: [],
        loading: false,
        auditVisible: false,
        ids: [],
        selectCount: '',
        provinceDataList: [],
        cityDataList: [],
        districtDataList: [],
        entStateOptions: entStateOptions,
        authStateOptions: authStateOptions,

        queryParams: {
          pageNum: 1,
          pageSize: 10,
          provinceId: undefined,
          cityId: undefined,
          districtId: undefined,
          entName: '',
          year: '',
          license: '',
          projectNo: '',
          projectName: ''
        },
        list: [],
        total: 0,
        open: false,
        openInfo: false,
      }
    },
    created() {
      this.getList()
      this.getProvinceData()

    },
    watch:{
      $route(){
        this.getList()
        this.getProvinceData()
      }
    },
    methods: {
      checkRole,
      getList() {
        this.loading = true;
        this.queryParams.year = this.years.join(',')
        listProjectApi(this.queryParams).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.selectCount = selection.length;
      },
      handleBatchDel() {
        if(this.selectCount <= 0) {
          this.$message.error('请选择要删除的数据');
          return
        }
        const delIds = this.ids.join(',')
        this.$confirm('是否确认删除该数据?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return deleteProjectApi(delIds);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          })
      },
      //选择省份
      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
          }
        })
      },
      handleDownload(row) {
        downLoadZip('/project/download/' +row.id, "项目附件")
      },
      handleDownloadBatch() {
        if(this.selectCount <= 0) {
          this.$message.error('请选择要下载的项目');
          return
        }
        const delIds = this.ids.join(',')
        downLoadZip('/project/download/' +delIds, "项目附件")
      },
      //编辑
      handleEdit(row) {
        this.$router.push({name: 'editProject', query: {id: row.id, entId: row.entId, type: 'update', activeName: '2'}})
      },
      // 审核
      handleAudit(row) {
        const projectId = row.id;
        const authState = '2'
        this.$confirm('是否确认提交审核该数据?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return projectAuditApi(projectId,authState);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.$message({
              message: '提交成功',
              type: 'success'
            });
          })
      },
      handleAuditAdmin(row) {
        this.$router.push({name: 'editProject', query: {id: row.id, entId: row.entId, type: 'view', oper: 'audit'}})
        // this.auditVisible = true
        // this.$nextTick(() => {
        //   this.$refs.auditProject.init(row.id)
        // })
      },
      handleDelete(row) {
        const id = row.id;
        this.$confirm('是否确认删除该数据?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return deleteProjectApi(id);
          }).then(() => {
            this.queryParams.pageNum = 1;
            this.getList();
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          })
      },
      // 重置
      resetQuery() {
        this.years = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 搜索
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      reportStateFormat(row, col, val, index) {
        return getLabel(reportStateOptions, val)
      },
      flagFormat(row, col, val, index) {
        return getLabel(booleanOptions, val)
      },
      overYearFormat(row, col, val, index) {
        return getLabel(booleanOptions, val)
      }
    }
  }
</script>

<style>
</style>
