<!--
  @description:分配招标计划
  @Modifier: sunxinman/haofengxian
  @routerPath:
  @date: 2020-5-22-13:49
-->
<template>
  <div>
    <el-button type="primary" class="mb10" @click="createProject()">
      创建招标项目
    </el-button>
    <el-button
      size="mini"
      style="color: red;font-size:14px;"
      type="text"
    >
      注：只能选择招标方式、招标组织形式、并且资格审查方式相同的招标计划
    </el-button>
    <section>
      <el-table
        ref="smallTable"
        v-loading="loading"
        :data="tableDataDistribution"
        :row-key="getRowKeys"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          :reserve-selection="true"
          :selectable="isDisabled"
          fixed
          type="selection"
          width="55"
          align="center"
        />
        <el-table-column fixed type="index" label="序号" align="center" width="60" />
        <el-table-column
          fixed
          label="关联"
          prop="isBiddingProject"
          align="center"
          width="60"
        >
          <template slot-scope="scope">
            <el-link v-if="scope.row.isBiddingProject==='01'" :underline="false" type="success">
              已关联
            </el-link>
            <el-link v-if="scope.row.isBiddingProject!=='01'" :underline="false" type="danger">
              未关联
            </el-link>
          </template>
        </el-table-column>
        <el-table-column

          fixed
          prop="projectName"
          label="工程项目名称"
          min-width="230"
        />
        <el-table-column

          fixed
          prop="schemeDeptname"
          label="招标组织单位名称"
          min-width="230"
        />
        <el-table-column
          :formatter="formatBiddingPlanType"
          prop="biddingPlanType"
          label="招标计划类型"
          width="100"
        />
        <el-table-column
          :formatter="formatPurchasLevel"
          header-align="center"
          prop="purchasLevel"
          label="采购级别"
          align="center"
          width="100"
        />
        <el-table-column
          prop="schemeNo"
          label="招标汇总号"
          min-width="160"
        />
        <el-table-column

          prop="schemeName"
          label="招标汇总名称"
          min-width="230"
        />
        <el-table-column
          :formatter="formatBiddingOrganizationForm"
          prop="biddingOrganizationForm"
          label="招标组织形式"
          width="100"
        />
        <el-table-column

          prop="biddingAgencyName"
          label="招标代理机构"
          min-width="230"
        />
        <el-table-column
          :formatter="formatBiddingMethod"
          prop="biddingMethod"
          label="招标方式"
          align="center"
          width="100"
        />
        <el-table-column
          :formatter="formatQualificationCheckWay"
          prop="qualificationCheckWay"
          label="资格审查方式"
          width="100"
        />
        <el-table-column
          :formatter="formatBiddingSchemeType"
          prop="biddingSchemeType"
          label="招标项目类型"
          align="center"
          width="100"
        />
        <el-table-column
          :formatter="formatSchemeClass"

          prop="schemeClass"
          label="工程分类"
          width="120"
        />
        <el-table-column

          prop="biddingClassSecondLevel"
          label="二级分类"
          min-width="110"
        >
          <template slot-scope="scope">
            <!--父级-->
            <span
              v-for="(item,index) in purchaseSchemeProjectTypeList"
              v-if="scope.row.biddingSchemeType === item.typecode"
              :key="index"
            >
              <!--子级-->
              <span
                v-for="(child,childIndex) in item.child"
                v-if="scope.row.biddingClassSecondLevel === child.typecode"
                :key="childIndex"
              >
                {{ child.typename }}
              </span>
            </span>
          </template>
        </el-table-column>
        <el-table-column
          :formatter="formatSchemeNature"
          prop="schemeNature"
          label="项目性质"
          align="center"
        />
        <el-table-column
          :formatter="formatBidOpeningMethod"
          prop="bidOpeningMethod"
          label="开标方式"
          align="center"
        />
        <el-table-column
          :formatter="schemeBudgetMoneyFormat"
          width="120"
          prop="schemeBudget"
          label="概算金额（元）"
          align="right"
        />
        <el-table-column
          :formatter="formatBiddingEvaluationWay"
          prop="biddingEvaluationWay"
          label="评标方式"
          align="center"
        />
        <el-table-column
          :formatter="formatBiddingQuotationMethod"
          prop="biddingQuotationMethod"
          label="投标报价方式"
          width="100"
        />
        <el-table-column
          prop="biddingFileExamStatus"
          label="招标文件是否已审查"
          align="center"
          width="140"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.biddingFileExamStatus === '00'">待审查</span>
            <span v-if="scope.row.biddingFileExamStatus === '01'">已审查</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="biddingFileExamWay"
          label="招标文件审查方式"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.biddingFileExamWay === '00'">自行审查</span>
            <span v-if="scope.row.biddingFileExamWay === '01'">委托审查</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="biddingFileExamDept"
          label="招标文件审查单位"
          align="center"
          width="120"
        />
        <el-table-column
          label="操作"
          width="180"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-link type="primary" @click="intoContract(scope.row.id)">
              [查看]
            </el-link>
            <!--schemeAttachment 招标方案是否有附件（1 是，0否）-->
            <el-link
              v-if="scope.row.schemeAttachment==='1'"
              type="primary"
              @click="downloadAttachment(scope.row)"
            >
              [下载附件]
            </el-link>
            <!--招标计划类型带招标方案 && 未关联项目的可以退回到采购管理平台-->
            <!--是否可以退回计划 1 是 0否-->
            <template v-if="scope.row.ifCanReturnScheme==='1'">
              <el-link
                v-if="scope.row.biddingPlanType==='00' && scope.row.isBiddingProject!=='01' "
                type="primary"
                @click="bidSchemeReturn(scope.row)"
              >
                [退回]
              </el-link>
            </template>
            <!--代理机构 && 未关联 可以退回到部门或者公司计划管理员-->
            <el-dropdown
              v-if="roleCode==='11' && scope.row.isBiddingProject!=='01'"
              class="pointer"
              trigger="click"
              @command="handleCommand"
            >
              <span class="el-dropdown-link">
                拒收<i class="el-icon-arrow-down el-icon--right" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-if="!scope.row.assignedDepartmentId" :command="beforeHandleCommand('00',scope.row)">
                  公司
                </el-dropdown-item>
                <el-dropdown-item :command="beforeHandleCommand('01',scope.row)">
                  部门
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination :pagination-data="paginationData" @change="loadData" />
    </section>
  </div>
</template>

<script>
/* api*/
import { getPretenderingPurchasesSchemeDistributePages, returnPurchasesSchemeByProjectManager } from '@/api/purchasePlan/pretcloudPurchasescheme'
import { downLoadFileUrl } from '@/api/common/uploadCenter'
import { bidSchemeReturn } from '@api/huanengwuzids'
/* 组件*/
import pagination from '@comp/common/pagination/pagination' // 分页组件
/* 常用公共方法*/
import { filterObj } from '@/assets/js/utils'
/* 混入公共方法*/
import { ListMixin } from '@/mixins/ListMixin'
import { tenderPlan } from '../mixins/tenderPlan'
export default {
  name: 'AllocationTenderPlanList',
  components: {
    pagination
  },
  mixins: [ListMixin, tenderPlan],
  data() {
    return {
      downLoadFileUrl: downLoadFileUrl,
      userInfo: this.$store.state.user.info, // 登录人信息
      roleCode: this.$store.state.user.info.currentRol.roleCode, // 当前角色 10 采购人 11代理机构
      loading: true,
      checkedArr: [], // 配招标计划复选框选中数组
      tableDataDistribution: [] // 获取获取招标计划已分配列表
    }
  },
  created() {
    // 招标计划-字典信息
    this.initPurchaseSchemeWebTypeGroup()
    this.loadData()
  },
  methods: {
    // 查询条件
    getQueryParams(searchForm) {
      const param = Object.assign({}, searchForm)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      return filterObj(param)
    },
    // 获取数据项目经理分页获取获取招标计划已分配列表)
    loadData(searchForm) {
      const params = this.getQueryParams(searchForm)// 查询条件
      params.projectManagerId = this.$store.getters.info.id
      this.loading = true
      getPretenderingPurchasesSchemeDistributePages(params).then(response => {
        this.loading = false
        this.tableDataDistribution = response.data.records
        this.paginationData.total = response.data.total
      }).catch(() => {
        this.loading = false
      })
    },
    // 已勾选要关联得计划
    handleSelectionChange(val) {
      this.checkedArr = [...val]
    },
    // 关联计划校验
    scheduledVerify() {
      // 招标方式 biddingMethod&& 资格审查方式qualificationCheckWay && 招标组织形式biddingOrganizationForm
      let verify = true
      // const selectedRowSchemeNature = []
      // const selectedRowPurchasLevel = []
      const selectedRowBiddingMethod = []// 招标方式
      const selectedBiddingOrganizationForm = []// 招标组织形式
      const selectedQualificationCheckWay = []// 资格审查方式
      this.checkedArr.map((item) => {
        // selectedRowSchemeNature.push(item.schemeNature) // 项目性质
        // selectedRowPurchasLevel.push(item.purchasLevel) // 采购级别
        selectedRowBiddingMethod.push(item.biddingMethod) // 招标方式
        selectedBiddingOrganizationForm.push(item.biddingOrganizationForm) // 招标组织形式
        selectedQualificationCheckWay.push(item.qualificationCheckWay) // 资格审查方式
      })
      /*      // 项目性质
      const schemeNatureArr = []
      selectedRowSchemeNature.map(item => {
        if (!schemeNatureArr.includes(item)) {
          schemeNatureArr.push(item)
        }
      })
      // 采购级别
      const purchasLevelArr = []
      selectedRowPurchasLevel.map(item => {
        if (!purchasLevelArr.includes(item)) {
          purchasLevelArr.push(item)
        }
      })*/
      // 招标方式
      const biddingMethodArr = []
      selectedRowBiddingMethod.map(item => {
        if (!biddingMethodArr.includes(item)) {
          biddingMethodArr.push(item)
        }
      })
      // 招标组织形式
      const biddingOrganizationFormArr = []
      selectedBiddingOrganizationForm.map(item => {
        if (!biddingOrganizationFormArr.includes(item)) {
          biddingOrganizationFormArr.push(item)
        }
      })
      // 资格审查方式
      const qualificationCheckWayArr = []
      selectedQualificationCheckWay.map(item => {
        if (!qualificationCheckWayArr.includes(item)) {
          qualificationCheckWayArr.push(item)
        }
      })
      // 所选招标组织形式、招标方式、资格审查方式不同
      if (biddingOrganizationFormArr.length > 1 || biddingMethodArr.length > 1 || qualificationCheckWayArr.length > 1) {
        this.$message({
          type: 'warning',
          message: '请仔细检查招标方案内容，确认无误后再进行关联'
        })
        verify = false
      }
      return verify
    },
    // 项目经理创建招标项目
    createProject() {
      if (this.checkedArr.length) {
        this.$confirm('请仔细检查招标方案内容，确认无误后再进行关联', '提示', {
          confirmButtonText: '关联',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          const verify = this.scheduledVerify()
          if (verify) {
            this.sessionTaskPlanList()
            this.$router.push('/bidding/bidProjectStep')
          }
        }).catch(() => {
        })
      } else {
        this.$message({
          type: 'warning',
          message: '请至少选择一条招标方案'
        })
      }
    },
    // table记住分页多选框选中的数据
    getRowKeys(row) {
      return row.id
    },
    // 复选框部分禁用
    isDisabled(row) {
      if (this.checkedArr.length) { // 判断是否已选择数据
        const data = { ...this.checkedArr[0] }
        // 招标方式 biddingMethod&& 资格审查方式qualificationCheckWay && 招标组织形式biddingOrganizationForm
        return row.isBiddingProject !== '01' && row.biddingMethod === data.biddingMethod && row.qualificationCheckWay === data.qualificationCheckWay && data.biddingOrganizationForm === row.biddingOrganizationForm
        // 判断是否相等
      } else {
        return row.isBiddingProject !== '01'
      }
    },
    // 存储已选招标计划信息
    sessionTaskPlanList() {
      // 需要关联招标项目的计划信息
      const taskPlanList = []
      let contractBudget = 0 // 总概算金额
      this.checkedArr.map(item => {
        contractBudget += Number(item.schemeBudget)
        taskPlanList.push({
          qualificationCheckWay: item.qualificationCheckWay, // 资格审查方式 资格审查方式，00单次资格预审，01年度资格预审，02资格后审
          annualPrequalificationScreeningProject: item.annualPrequalificationScreeningProject, // 年度资格预审筛选项目 00:否 01:是
          biddingMethod: item.biddingMethod, // 招标方式
          schemeId: item.schemeId, // 采购计划ID
          schemeName: item.schemeName, // 计划名称
          schemeNo: item.schemeNo, // 计划编号
          /* 需带入立项的字段*/
          projectName: item.projectName, // 工程项目名称->招标项目名称
          biddingOrganizationForm: item.biddingOrganizationForm, // 招标组织形式
          schemeNature: item.schemeNature, // 项目性质
          biddingPlanType: item.biddingPlanType, // 招标计划类型 00:带招标方案 01:自建项目
          schemeBudget: 0, // 概算金额
          purchasLevel: item.purchasLevel // 采购级别
        })
      })
      taskPlanList[0].schemeBudget = contractBudget
      sessionStorage.setItem('taskPlanList', JSON.stringify(taskPlanList))// 招标计划信息
    },
    // 查看
    intoContract(id) {
      this.$router.push({
        name: 'TenderPlanBuildDetail',
        params: { id }
      })
    },
    // 下载附件
    downloadAttachment(row) {
      let downLoadFileUrl = '' // 下载附件地址
      downLoadFileUrl = this.downLoadFileUrl + '?fileId=' + row.schemeFileId + '&fileType=ZBFJ'
      window.location.href = downLoadFileUrl
    },
    // 拒收下拉菜单 command：01部门级计划管理员 00公司级计划管理员
    handleCommand(command) {
      const rowData = {
        departId: '', // 部门id（returnLevel为00时必填）
        departName: '', // 部门名称（returnLevel为00时必填）
        returnLevel: command.command, // 退回级别：01部门级计划管理员 00公司级计划管理员 默认00退回公司级计划管理员
        schemeId: command.row.schemeId // 招标计划id
      }
      if (command.command === '00') {
        rowData.departId = command.row.assignedCompanyId
        rowData.departName = command.row.assignedCompanyName
      } else {
        rowData.departId = command.row.assignedDepartmentId
        rowData.departName = command.row.assignedDepartmentName
      }
      this.rejection(rowData)
    },
    // command 参数
    beforeHandleCommand(item, row) {
      return {
        'command': item,
        'row': row
      }
    },
    // 拒收
    rejection(rowData) {
      this.$confirm('是否确认拒收？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        returnPurchasesSchemeByProjectManager(rowData).then(() => {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.loadData()
        })
      }).catch(() => {

      })
    },
    // 招标计划退回采购管理平台
    bidSchemeReturn(row) {
      this.$confirm('是否确认退回？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        const dataParams = {
          schemeId: row.schemeId, // 计划ID
          thnam: this.userInfo.id, // 登录人ID
          thnamc: this.userInfo.realName // 登录人名字
        }
        bidSchemeReturn(dataParams).then(() => {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.loadData()
        })
      }).catch(() => {

      })
    }
  }
}
</script>

<style scoped lang="less">
/deep/.el-dropdown{
  font-size: 12px;
  vertical-align: middle;
  margin-left: 5px;
  .el-dropdown-link.el-dropdown-selfdefine{
   color:   #007CCC;
  }
}
</style>
