<!--
  @description:采购立项-项目管理(询价 单一来源 竞争性谈判 竞价)
  @Modifier:gmy
  @routerPath:/procurement/enquiryManageList 询价
  @routerPath:/procurement/singleSourceManageList 单一来源
  @routerPath:/procurement/competitiveManageList 竞争性谈判
  @routerPath:/procurement/qutcryManageList 竞价
  @date:2020/8/2 20:46
-->
<template>
  <div>
    <table-crud
      ref="tableCrud"
      :search-form.sync="searchForm"
      :menu="true"
      :tabs="tabList"
      search-label-width="130px"
      @on-load="loadData"
    >
      <!--标题-->
      <template v-slot:header>
        采购单管理（{{ $route.meta.tenderMethodText }}）
      </template>
      <template v-slot:search="search">
        <el-form-item ref="tenderNo" label="采购单编号">
          <gx-input
            v-model.trim="searchForm.tenderNo"
            placeholder="请输入采购单编号"
            :type-more="{ type: 'more', attr: 'tenderNo' }"
            @onMoreSearch="onMoreSearch"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <el-form-item ref="tenderName" label="采购单名称">
          <gx-input
            v-model.trim="searchForm.tenderName"
            placeholder="请输入采购单名称"
            :type-more="{ type: 'more', attr: 'tenderName' }"
            @onMoreSearch="onMoreSearch"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <!--        <el-form-item ref="purchaseCode" label="物资编号">
          <gx-input
            v-model.trim="searchForm.purchaseCode"
            placeholder="请输入物资编号"
            :type-more="{ type: 'more', attr: 'purchaseCode' }"
            @onMoreSearch="onMoreSearch"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>-->
        <!--        <el-form-item ref="purchaseNo" label="计划编号">
          <gx-input
            v-model.trim="searchForm.purchaseNo"
            placeholder="请输入计划编号"
            :type-more="{ type: 'more', attr: 'purchaseNo' }"
            @onMoreSearch="onMoreSearch"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>-->
        <!--        <el-form-item
          v-if="roleCode!=='10'"
          ref="sourcingUserName"
          label="采购员"
          prop="sourcingUserName"
        >
          <gx-input
            v-model.trim="searchForm.sourcingUserName"
            placeholder="请输入"
            style="width: 310px"
            :type-more="{ type: 'more', attr: 'sourcingUserName' }"
            @onMoreSearch="onMoreSearch"
            @keyup.enter.native="onSearch"
          />
        </el-form-item>-->
        <el-form-item ref="tenderMethod" label="公开/邀请">
          <el-select v-model="searchForm.tenderType" placeholder="请选择">
            <el-option
              v-for="item in tenderTypeList"
              :key="item.typename"
              :label="item.typename"
              :value="item.typecode"
            />
          </el-select>
        </el-form-item>
        <el-form-item ref="purchaseProjectStatus" label="采购单状态">
          <el-select v-model="searchForm.purchaseProjectStatus" placeholder="请选择">
            <el-option
              v-for="item in protprojectPurchaseProjectStatusList1"
              :key="item.typecode"
              :label="item.typename"
              :value="item.typecode"
            />
          </el-select>
        </el-form-item>
        <el-form-item ref="date" label="创建日期">
          <el-date-picker
            v-model="searchForm.createTimeBegin"
            :value-format="elValueFormatDate"
            :clearable="false"
            type="date"
            align="right"
            placeholder="选择日期时间"
          />
          ~
          <el-date-picker
            v-model="searchForm.createTimeEnd"
            :value-format="elValueFormatDate"
            :clearable="false"
            type="date"
            align="right"
            placeholder="选择日期时间"
          />
        </el-form-item>
        <el-form-item label="采购组织形式">
          <el-select v-model="searchForm.tenderOrgMethod" placeholder="请选择">
            <el-option
              v-for="item in plan_purchase_organizationList"
              :key="item.typecode"
              :label="item.typename"
              :value="item.typecode"
            />
          </el-select>
        </el-form-item>
        <el-form-item ref="date" label="报价截止时间 ">
          <el-date-picker
            v-model="searchForm.quoteEndTimeBegin"
            :value-format="elValueFormatDate"
            :clearable="false"
            type="date"
            align="right"
            placeholder="选择日期时间"
          />
          ~
          <el-date-picker
            v-model="searchForm.quoteEndTimeEnd"
            :value-format="elValueFormatDate"
            :clearable="false"
            type="date"
            align="right"
            placeholder="选择日期时间"
          />
        </el-form-item>
        <el-form-item
          v-if="searchForm.type!=='00'"
          ref="isFullThree"
          label-width="160px"
          label="报价数量是否满足三家"
        >
          <el-select v-model="searchForm.isFullThree" placeholder="请选择">
            <el-option
              v-for="item in isFullThreeList"
              :key="item.typename"
              :label="item.typename"
              :value="item.typecode"
            />
          </el-select>
        </el-form-item>
      </template>
      <template v-slot:GXTable>
        <el-table ref="smallTable" v-loading="loading" :data="tableData">
          <el-table-column
            type="index"
            label="序号"
            min-width="60px"
            align="center"
          />
          <el-table-column
            header-align="center"
            align="left"
            label="采购单编号/名称"
            min-width="230"
          >
            <template slot-scope="scope">
              <p>{{ scope.row.tenderNo }}</p>
              <p>{{ scope.row.tenderName }}</p>
            </template>
          </el-table-column>
          <!--          <el-table-column
            v-if="roleCode!=='11'"
            header-align="center"
            label="采购员"
            width="150"
            align="left"
            prop="sourcingUserName"
          />-->
          <!-- <el-table-column
            header-align="center"
            label="是否框架"
            width="80"
            align="center"
            prop="planType"
          />-->
          <el-table-column
            header-align="center"
            label="公开/邀请"
            min-width="80"
            align="center"
          >
            <template slot-scope="scope">
              {{ filterDictText(tenderTypeList, scope.row.tenderType) }}
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            label="采购组织形式"
            min-width="100"
            align="center"
          >
            <template slot-scope="scope">
              {{ filterDictText(plan_purchase_organizationList, scope.row.tenderOrgMethod) }}
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            label="创建日期"
            align="center"
            min-width="130"
          >
            <template slot-scope="scope">
              {{ scope.row.createTime && $Moment(scope.row.createTime).format(momentDateTime) }}
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            prop="quoteEndTime"
            label="报价截止时间"
            align="center"
            min-width="130"
          >
            <template slot-scope="scope">
              {{ scope.row.quoteEndTime && $Moment(scope.row.quoteEndTime).format(momentDateTime) }}
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            label="采购单状态"
            align="center"
            min-width="90"
          >
            <template slot-scope="scope">
              {{ filterDictText(protprojectPurchaseProjectStatusList,scope.row.purchaseProjectStatus ) }}
            </template>
          </el-table-column>
          <!-- 单一来源不显示报价数量 -->
          <!-- <el-table-column
            v-if="$route.meta.tenderMethod!=='12'"
            :key="new Date()*1"
            prop="negRecordNum"
            header-align="center"
            label="报价数量"
            align="right"
            min-width="70"
          /> -->
          <el-table-column
            v-if="searchForm.type!=='00'"
            prop="negRecordNum"
            header-align="center"
            label="报价数量"
            align="right"
            min-width="70"
          />
          <!-- 仅在单一来源和竞谈是出现报价轮次字段 -->
          <el-table-column
            v-if="searchForm.type!=='00' && (searchForm.tenderMethod==='12'||searchForm.tenderMethod==='13')"
            header-align="center"
            align="right"
            prop="stageNo"
            label="报价轮次"
            min-width="80"
          >
            <template slot-scope="scope">
              {{ scope.row.isLast === "01" && $route.meta.tenderMethod !== "11" ? "最终轮" : scope.row.stageNo }}
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            label="操作"
            align="center"
            min-width="200"
          >
            <template slot-scope="scope">
              <!--
                purchaseProjectStatus 00(维护采购单状态)10(公告审核状态) 不是维护采购单状态才可显示进入并且不是公告审核状态 显示
              -->
              <gx-link
                v-if="scope.row.purchaseProjectStatus!=='00'"
                :underline="false"
                type="primary"
                size="mini"
                plain
                @click.stop="intoProject(scope.row)"
              >
                [进入]
              </gx-link>
              <!-- 变更 tenderMethod除单一来源之外的采购方式显示变更 控制变更是否显示changeStatus 00 不显示 01 显示-->
              <gx-link
                v-if="$route.meta.tenderMethod!=='12'&& scope.row.changeStatus==='01'"
                :code="'001_001_001'"
                :underline="false"
                type="primary"
                size="mini"
                plain
                @click.stop="isChangeInfo(scope.row)"
              >
                [变更]
              </gx-link>
              <!--
                useStatus 00 表示未发布完公告  purchaseProjectStatus采购单状态 00 维护采购单 10
              -->
              <gx-link
                v-show="scope.row.useStatus==='00'&&scope.row.purchaseProjectStatus === '00'"
                :code="'001_001_009'"
                :underline="false"
                type="primary"
                size="mini"
                plain
                @click.stop="redactProject(scope.row)"
              >
                [编辑]
              </gx-link>
              <gx-link
                :underline="false"
                type="primary"
                size="mini"
                plain
                @click.stop="journalProject(scope.row)"
              >
                [日志]
              </gx-link>
              <!-- sourceType 01A  -->
              <gx-link
                v-if="scope.row.sourceType.indexOf('01A') !== -1 && scope.row.purchaseProjectStatus==='00'"
                :code="'001_001_008'"
                :underline="false"
                type="primary"
                size="mini"
                plain
                @click="callbackfn(scope.row, '00')"
              >
                [退回分配]
              </gx-link>
              <!--changeAuditStatus 01 采购单变更正在审核中-->
              <gx-link
                v-if="(searchForm.type !== '00' && searchForm.type !== '02')
                  && (scope.row.purchaseProjectStatus === '00' || scope.row.purchaseProjectStatus === '01'
                    || scope.row.purchaseProjectStatus==='02' || scope.row.purchaseProjectStatus==='07' || scope.row.purchaseProjectStatus==='09')"
                :underline="false"
                :code="'001_001_007'"
                type="primary"
                size="mini"
                plain
                :disabled="scope.row.changeAuditStatus==='00'"
                @click="planReturn(scope.row)"
              >
                [计划退回]
              </gx-link>
              <!-- purchaseProjectStatus 01 公告发布  02 评审  06 流标（废弃） 采购单位这三个时才可操作整单释放(非B类) 整单退回(B类)-->
              <template
                v-if="scope.row.purchaseProjectStatus === '01' || scope.row.purchaseProjectStatus === '02' || scope.row.purchaseProjectStatus==='07'
                  || scope.row.purchaseProjectStatus==='09'"
              >
                <!--  purchaseProjectStatus 采购项目状态 00维护采购单 01公告发布 02评审中 03中选审批 04结果公告发布 05采购终止 06流标 07(字段待定)
                      secondTimeStatus 00 (非询价&&报价时间未截止&&邀请类的询价 为00)
                      secondTimeStatus 01 询价&&报价时间截止&&公开类的询价 为01)
                      满足项目状态为06 流标 &&secondTimeStatus 01 发起二次按钮显示
                 -->
                <gx-link
                  :code="'001_001_006'"
                  :underline="false"
                  type="primary"
                  size="mini"
                  plain
                  :disabled="scope.row.changeAuditStatus==='00'"
                  @click="secondInquiry(scope.row)"
                >
                  [发起二次]
                </gx-link>
              </template>
              <!--              <gx-link type="primary" @click="viewPackInfo(scope.row)">
                打包信息
              </gx-link>-->
            </template>
          </el-table-column>
        </el-table>
      </template>
    </table-crud>
    <!-- 日志详情弹框 -->
    <journalDialog v-if="dialogDataJournal.dialog" :dialog-data="dialogDataJournal" />
    <!-- 退回分配 -->
    <projectRelease
      v-if="dialogProjectRelease.dialog"
      :dialog-data="dialogProjectRelease"
      @projectRelease="projectRelease"
    />
    <!-- 计划退回弹框 -->
    <planReturnDialog v-if="planReturnData.dialog" :dialog-data="planReturnData" @refResh="planReturnLoad" />
    <!--打包信息-->
    <purPlanBaleDetailDialog ref="purPlanBaleDetail" />
  </div>
</template>
<script>
/* 接口*/
import {
  listProjectManagementPages,
  listProjectSupervisionPages,
  cancelPurchase,
  sendSecondTime
} from '@/api/procurement/procurementProject'// 项目列表 项目删除
/* 公用方法*/
import { filterDictText } from '@/assets/js/utils'// 常用方法
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'// 分页
import { initGroupMixin } from '@/mixins/initGroupMixin'// 字典信息
import { commonMxin } from '@/mixins/commonMxin'// 字典信息
/* 常用方法*/
import { procurementGroupMixins } from '@/views/procurement/mixins/procurementGrounpMixins'
import { procurementProjectMixins } from '../mixins/procurementProjectMixins'
/* 组件*/
import pagination from '@comp/common/pagination/pagination'// 分页
import journalDialog from './procurementJournal-dialog' // 日志详情弹框
import projectRelease
  from '@/views/procurement/procurementProject/procurementProjectLineItemInfo/procurementProjectRelease-dialog.vue' // 项目整单释放退回弹框
import planReturnDialog from './planReturn-dialog' // 计划退回弹窗
import purPlanBaleDetailDialog from '@views/procurement/procurementPlan/purPlanAllocation/purPlanBaleDetail-dialog.vue' // 打包信息

export default {
  name: 'ProjectManagerTenderList',
  components: {
    purPlanBaleDetailDialog, // 打包信息
    pagination, // 分页
    journalDialog, // 日志详情弹框
    projectRelease, // 项目释放退回弹框
    planReturnDialog // 计划退回意见
  },
  mixins: [ListMixin, initGroupMixin, commonMxin, procurementGroupMixins, procurementProjectMixins],
  data() {
    return {
      // tabs
      tabList: [
        { name: '待办', index: '00', num: 0, showNum: true },
        { name: '进行中', index: '01', num: 0, showNum: true },
        { name: '已完成', index: '02', num: 0, showNum: true },
        { name: '报名已截止', index: '03', num: 0, showNum: true }
      ],
      // 待办、进行中、已完成数量
      tabsLabel: {},
      loading: true,
      // 搜索
      searchForm: {
        type: '00', // 默认选中第一个tab页待办
        tenderNo: '', // 采购项目编号
        tenderName: '', // 采购项目名称
        status: '00',
        tenderType: '', // 公开/邀请
        createTimeBegin: '', // 创建开始时间
        createTimeEnd: '', // 创建结束时间
        quoteEndTimeBegin: '', // 报价截止时间开始时间
        quoteEndTimeEnd: '', // 报价截止时间开始结束
        tenderOrgMethod: '', // 组织形式
        tenderMethod: this.$route.meta.tenderMethod // 获取采购方式
      },
      tableData: [], // 列表数据
      purchaserType: '', // 00采购员  01采购执行人
      // 日志弹框
      dialogDataJournal: {
        dialog: false
      },
      // 项目释放退回弹框
      dialogProjectRelease: {
        bizId: '', // 附件id
        dialog: false,
        status: '' // 区分整单释放和退回字段 00释放 01退回
      },
      // 整单释放 整单退回参数字段
      releaseProject: {
        tenderId: '', // 项目id
        reason: '' // 释放说明字段
      },
      // 计划退回弹窗
      planReturnData: {
        dialog: false,
        bizId: '', // 附件id
        id: ''
      },
      isEdit: false, // 是否点击编辑
      searchParams: {}, // 参数
      roleCode: this.$store.getters.info.currentRol.roleCode // 角色code
    }
  },
  watch: {
    // 监听路由meta中tenderMethod值的变化
    '$route.meta.tenderMethod': {
      handler(newVal) {
        if (newVal) {
          this.$refs.tableCrud.searchReset()
          this.searchForm.tenderMethod = newVal
          this.searchForm.type = '00'
        }
      },
      deep: true
    }
  },
  mounted() {
    // 查询项目状态
    this.initProjectBase()
    // 字典信息
    this.initWebTypeGroup()
  },
  methods: {
    filterDictText, // 字典值替换文本
    // 获取列表数据
    loadData(params, callback, tabData) {
      let apiName = ''
      if (this.roleCode === '10' || this.roleCode === '20') {
        apiName = listProjectManagementPages // 采购员查询接口
      } else {
        apiName = listProjectSupervisionPages // 除采购员外领导查看接口
      }
      if (tabData) {
        params.type = tabData.index
        this.searchForm.type = tabData.index
        params.tenderMethod = this.$route.meta.tenderMethod // 获取采购方式
        this.searchParams = params
      }
      this.loading = true
      apiName(params).then(response => {
        // todoNum 待办数量  ingNum 进行中数量  downNum 已完成数量 pastNum 报名已截止数量
        const { todoNum, ingNum, downNum, pastNum, page } = response.data
        this.loading = false
        this.tableData = page.records.map(item => {
          if (!item.sourceType) {
            return { ...item, sourceType: '' }
          }
          return item
        })
        callback(page.total)
        // todoNum 待办数量  ingNum 进行中数量  downNum 已完成数量 pastNum 报名已截止数量
        this.tabList[0].num = todoNum
        this.tabList[1].num = ingNum
        this.tabList[2].num = downNum
        this.tabList[3].num = pastNum
        this.searchForm.column = ''
        this.searchForm.orderSort = ''
      }).catch(() => {
        this.searchForm.column = ''
        this.searchForm.orderSort = ''
        this.loading = false
      })
    },
    // 编辑项目信息
    redactProject(row) {
      this.isEdit = true
      // 判断项目id
      if (row.id) {
        this.$router.push({
          path: '/procurement/procurementBaseContentAdd',
          query: {
            tenderId: row.id, // 项目
            tenderOrgMethod: row.tenderOrgMethod // 项目组织形式
          }
        })
      }
    },
    // 进入项目
    intoProject(row) {
      this.$router.push({
        path: '/procurement',
        name: 'procurementDrive',
        params: {
          tenderId: row.id, // 项目id
          packId: row.packId, // 标段id
          isChangeShow: row.changeStatus, // 是否显示变更操作按钮 00不显示 01 显示
          secondTimeStatus: row.secondTimeStatus // 是否显示发起二次按钮 00不显示 01 显示
        }
      })
    },
    // 计划退回弹窗打开
    planReturn(row) {
      this.planReturnData.id = row.id
      this.planReturnData.bizId = row.id // 附件id
      this.planReturnData.dialog = true
    },
    // 计划退回完毕刷新页面
    planReturnLoad() {
      this.$refs.tableCrud.onSearchChange()
    },
    // 退回分配
    callbackfn(row, status) {
      if (row.id) {
        this.releaseProject.tenderId = row.id // 获取项目id
        this.dialogProjectRelease.bizId = row.id
        this.dialogProjectRelease.status = status // 区分整单释放或者整单退回字段 00 释放 01 退回
        this.dialogProjectRelease.dialog = true // 开启释放说明弹框
      }
    },
    // 整单释放/整单退回释放说明的弹框回调
    projectRelease(releaseData) {
      this.releaseProject.reason = releaseData.reason // 获取释放说明
      setTimeout(() => {
        // 整单释放
        this.$confirm('确认将采购单退回采购室主任重新分配？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          // 调用整单释放 整单退回请求
          this.cancelPurchaseRequest(this.releaseProject)
        }).catch(() => {
        })
      }, 500)
    },
    // 点击发起二次询价事件
    secondInquiry(row) {
      // 获取发起二次请求参数
      const data = {
        offerNum: row.negRecordNum, // 供应商报价家数
        quoteEndTime: row.quoteEndTime, // 当前轮次的报价截止时间
        tenderId: row.id // 项目id
      }
      this.$confirm('确认发起二次报价吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        // 调用发起二次请求方法
        this.setSendSecondTime(data)
      }).catch(() => {
      })
    },
    // 日志追踪
    journalProject(row) {
      this.dialogDataJournal.dialog = true
      this.dialogDataJournal.id = row.id
    },
    // 项目变更
    isChangeInfo(row) {
      this.$router.push({
        path: '/procurement',
        name: 'procurementDrive',
        params: {
          tenderId: row.id, // 项目id
          packId: row.packId, // 标段id
          isChangeShow: row.changeStatus // 是否显示变更操作按钮 00不显示 01 显示
        }
      })
    },
    // 整单释放 整单说明接口请求
    cancelPurchaseRequest(data) {
      cancelPurchase(data).then(res => {
        this.$message.success('操作成功')
        this.$refs.tableCrud.onSearchChange()
      }).catch(() => {
      })
    },
    // 发起二次询价请求
    setSendSecondTime(data) {
      sendSecondTime(data).then(res => {
        // 请求页面数据
        this.$refs.tableCrud.onSearchChange()
      }).catch(() => {
      })
    },
    // 查看打包信息
    viewPackInfo(row) {
      this.$refs.purPlanBaleDetail.openDialogPurPlanBaleDetail(row.id)
    },
    // 多值查询
    onMoreSearch({ attr, value }) {
      const params = Object.assign(this.searchForm, {
        [attr]: value
      })
      this.$refs.tableCrud.onSearchChange(params)
    }
  },
  // 路由生命钩子动态修改前往路由的meta中的path值
  beforeRouteLeave(to, from, next) {
    // 导航离开该组件的对应路由时调用
    // 可以访问组件实例 `this`
    // 动态修改前往路由的meta中的path
    // 前往路由的path为空时在获取
    // 前往的路由的meta中pathType字段为02是动态设置其path属性值 创建采购单第一步
    if (to.meta.pathType === '02') {
      to.meta.path = from.meta.path
    }
    next()
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-textarea__inner {
  padding: 1px 15px;
  line-height: 0.9;
}
</style>
