<template>
  <div class="common-page-shadow">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        :model="search"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="是否开标">
          <el-select v-model="search.code" size="small" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="container-btn">
        <el-button
          size="small"
          icon="el-icon-search"
          type="danger"
          class="btn btn-plus"
          @click="handleSearch"
        >
          搜索
        </el-button>
        <el-button
          size="small"
          icon="el-icon-refresh-right"
          class="btn btn-plus"
          @click="clearSearchForm"
        >
          重置
        </el-button>
      </div>
    </div>
    <el-row
      class="common-table-container flex searchPanel"
      :style="SearchPanel"
    >
      <el-col :span="24" class="common-table-wrapper">
        <div class="common-btn-group">
          <el-button
            size="small"
            icon="el-icon-thumb"
            type="primary"
            class="btn btn-plus"
            @click="handleAdd"
          >
            查看报名企业
          </el-button>
          <el-button
            :disabled="handleDeleteActive"
            size="small"
            type="primary"
            icon="el-icon-download"
            @click="handleDelete"
          >
            导出明细
          </el-button>
        </div>
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            v-loading="tableLoading"
            ref="multipleSelection"
            :data="tableData"
            @selection-change="handleSelectionChange"
          >
          <el-table-column
              type="selection"
              min-width="50px"
              align="center"
            ></el-table-column>
            <el-table-column
              type="index"
              width="50"
              align="center"
              label="序号"
            >
            </el-table-column>
            <el-table-column
              prop="projectNumber"
              min-width="120px"
              label="项目编号"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="noticeTitle"
              show-overflow-tooltip
              min-width="130px"
              align="center"
              label="项目名称"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="procureOrgName"
              show-overflow-tooltip
              min-width="130px"
              align="center"
              label="采购单位"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="biddingStartTime"
              show-overflow-tooltip
              min-width="130px"
              align="center"
              label="开始时间"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="biddingEndTime"
              show-overflow-tooltip
              min-width="130px"
              align="center"
              label="结束时间"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="bids"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="是否开标"
              resizable
            >
              <template v-slot="{row}">
                {{ row.bidStatus == 5 ? '是' : '否' }}
              </template>
            </el-table-column>
            <el-table-column min-width="110px" align="center" label="操作">
              <template v-slot="{row}">
                <template>
                  <el-button
                    v-if="row.bidStatus == 3 || row.bidStatus == 4"
                    type="primary"
                    plain
                    size="small"
                    @click="bidsBtn(row)"
                    >开标</el-button
                  >
                  <el-button
                    type="primary"
                    plain
                    size="small"
                    @click="markerFlow(row)"
                    >流标</el-button
                  >
                </template>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="common-pagenation">
          <el-pagination
            ref="paginationPanel"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryParam.pageBean.page"
            :page-sizes="[20, 30, 40, 50]"
            :page-size="queryParam.pageBean.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="queryParam.pageBean.total"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <!-- 开标弹层 -->
    <el-dialog
      :close-on-click-modal="false"
      title="开标"
      :visible.sync="bidsLoading"
      width="50%"
      :before-close="handleClose"
      top="10vh"
    >
      <el-table
        :border="true"
        stripe
        fit
        size="mini"
        v-loading="loading"
        ref="multipleSelection"
        :data="manageData"
      >
        <el-table-column type="index" width="55" align="center" label="序号">
        </el-table-column>
        <el-table-column
          prop="quotationCompanyName"
          min-width="20%"
          label="供应商"
          align="center"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="totalPrice"
          show-overflow-tooltip
          min-width="20%"
          align="center"
          label="总价（元）"
          resizable
        >
        </el-table-column>
        <el-table-column min-width="20%" align="center" label="操作">
          <template v-slot="{row}">
            <template>
              <el-button
                type="primary"
                plain
                size="small"
                @click="handleDetails(row)"
                >报价明细</el-button
              >
            </template>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="bidsSaveing"
          @click="bidsSave"
          >保 存</el-button
        >
      </span>
    </el-dialog>
    <!-- 报名企业 -->
    <el-dialog
      :close-on-click-modal="false"
      title="报名企业"
      :visible.sync="visible"
      width="60%"
      :before-close="close"
      top="10vh"
    >
      <el-table
        :border="true"
        stripe
        fit
        size="mini"
        v-loading="loading"
        ref="multipleSelection"
        :data="registrationList"
      >
        <el-table-column type="index" width="55" align="center" label="序号">
        </el-table-column>
        <el-table-column
          prop="companyName"
          min-width="15%"
          label="报名企业"
          align="center"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matCode"
          show-overflow-tooltip
          min-width="15%"
          align="center"
          label="是否报价"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matPlatcode"
          show-overflow-tooltip
          min-width="15%"
          align="center"
          v-if="multipleSelection.length!=0&&multipleSelection[0].bidStatus==5"
          label="报价金额"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matName"
          show-overflow-tooltip
          min-width="10%"
          align="center"
          v-if="multipleSelection.length!=0&&multipleSelection[0].bidStatus==5"
          label="投标文件"
          resizable
        >
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取消</el-button>
      </span>
    </el-dialog>
    <!-- 报价明细 -->
    <el-dialog
      :close-on-click-modal="false"
      title="报价明细"
      :visible.sync="showDetals"
      width="60%"
      :before-close="close"
      top="10vh"
    >
      <el-table
        :border="true"
        stripe
        fit
        size="mini"
        v-loading="loading"
        ref="multipleSelection"
        :data="bidDataList"
      >
        <el-table-column type="index" width="55" align="center" label="序号">
        </el-table-column>
        <el-table-column
          prop="matCompanyCode"
          min-width="10%"
          label="企业编号"
          align="center"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matCode"
          show-overflow-tooltip
          min-width="15%"
          align="center"
          label="企业物料编号"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matPlatcode"
          show-overflow-tooltip
          min-width="15%"
          align="center"
          label="平台物料编号"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matName"
          show-overflow-tooltip
          min-width="10%"
          align="center"
          label="物料名称"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matMaterial"
          show-overflow-tooltip
          min-width="10%"
          align="center"
          label="物料牌号"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matSpec"
          show-overflow-tooltip
          min-width="10%"
          align="center"
          label="规格型号"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matUnit"
          show-overflow-tooltip
          min-width="10%"
          align="center"
          label="数量"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="matUnit"
          show-overflow-tooltip
          min-width="10%"
          align="center"
          label="计量单位"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="myPrice"
          show-overflow-tooltip
          min-width="15%"
          align="center"
          label="报价金额(元)"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="minQuoPrice"
          show-overflow-tooltip
          min-width="15%"
          align="center"
          label="最低报价(元)"
          resizable
        >
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDetals = false">取消</el-button>
      </span>
    </el-dialog>
    <!-- 流标弹层 -->
    <el-dialog
      :close-on-click-modal="false"
      title="流标原因"
      :close-on-press-escape="false"
      :visible.sync="showTroubleshooting"
      width="50%"
      :before-close="close"
      top="10vh"
    >
      <el-form
        justify="left"
        label-width="auto"
        label-position="right"
        :model="markerFlowValue"
        class="demo-form-inline"
      >
        <el-form-item label="流标原因">
          <el-input
            type="textarea"
            size="small"
            v-model="markerFlowValue.failBidReason"
            placeholder="请输入流标原因"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="上传附件">
          <div style="text-align: left">
            <el-upload
              class="upload-demo"
              ref="upload"
              :headers="headers"
              :action="action"
              :file-list="fileList"
              :multiple="false"
              accept=".png,.jpg,.jpeg,.xlsx,.xls,.pdf,.docx,.doc,.zip,.rar"
              :show-file-list="false"
              :before-upload="beforeUpload"
              :on-success="handleSuccess"
            >
              <el-button
                icon="el-icon-upload"
                slot="trigger"
                style="width: 100% !important"
                size="small"
                type="primary"
                >上传文件</el-button
              >
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
      <el-table
        class="down"
        :data="fileList"
        border
        stripe
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column prop="name" label="文件名称"></el-table-column>
        <el-table-column prop="size" label="文件大小">
          <template slot-scope="scope">
            <span>{{ scope.row.fileSize }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="上传时间">
          <template slot-scope="scope">
            <div>
              {{ scope.row.createTime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column width="150px" label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="text">
              <a
                target="_blank"
                @click="downloadFile(scope.row.name, scope.row.url)"
                >下载</a
              >
            </el-button>
            <el-button
              size="small"
              type="text"
              @click="deleteHandle(scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="flowing"
          @click="handleSave"
          >保 存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import routerManagement from '@/api/BasicDataManagement/routerManagement.js'
export default {
  name: 'DemandEntry',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      currentPage: 1,
      tableMaxHeight: 400,
      loading: false,
      objData: {},
      expertTitle: '选择评标人员',
      rounding: false,
      tableLoading: false,
      showTroubleshooting: false,
      markerFlowValue: {},
      bidsLoading: false,
      echoValue: {
        name: '',
      },
      manageData: [],
      bidOpening: [],
      options: [
        {
          value: 0,
          label: '未开标',
        },
        {
          value: 1,
          label: '已开标',
        },
      ],
      fileList: [],
      tableData: [],
      multipleSelection: [],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      querys: [
        {
          group: 'main',
          operation: 'IN',
          parentGroup: '',
          property: 'bidStatus',
          relation: 'AND',
          value: ['3', '4', '5'],
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'procureType',
          relation: 'AND',
          value: 1,
        },
        {
          group: 'main',
          operation: 'LESS',
          parentGroup: '',
          property: 'biddingEndTime',
          relation: 'AND',
          value: '',
        },
      ],
      search: {
        code: 0,
      }, // 顶部搜索对象
      flowing: false,
      bidDataList: [],
      showDetals: false,
      visible:false,
      bidsSaveing: false,
      edit: false, // 是否是修改功能
      registrationList:[]
    }
  },
  mounted() {},
  created() {
    const moment = require('moment')
    this.querys[2].value = moment().format('YYYY-MM-DD HH:mm:ss')
    this.loadData()
  },
  computed: {
    ...mapState({
      headers: (state) => {
        return {Authorization: `Bearer ${state.login.currentUser.token}`}
      },
      action: function () {
        return window.context.portal + '/biz/universal/v1/minioUpload'
      },
    }),
  },
  methods: {
    // 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用,function(file, fileList)
    handleSuccess(res, file, fileList) {
      console.log(res, fileList)
      this.fileList.push(res.value)
    },
    beforeUpload(file) {
      let acceptList = ['xlsx', 'xls','png','jpg','jpeg','xlsx','xls','pdf','docx','doc','zip','rar']
      // 根据文件名获取文件的后缀名
      let fileType = file.name.split('.').pop().toLowerCase()
      // 判断文件格式是否符合要求
      if (acceptList.indexOf(fileType) === -1) {
        this.$message.error('请上传 .png,.jpg,.jpeg,.xlsx,.xls,pdf,.docx,.doc,.zip,.rar 格式的文件 !')
        return false
      }
    },
    // 点击关闭按钮
    handleClose() {
      this.showTroubleshooting = false
      this.bidsLoading = false
    },
    close() {
      this.showDetals = false
      this.visible = false
    },
    handleAdd(){
      let data = {
        pageBean:{
          page:-1,
          pageSize:-1
        },
        querys:[
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'noticeId',
          relation: 'AND',
          value: this.multipleSelection[0].id,
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'status',
          relation: 'AND',
          value: 1,
        },
        ]
      }
      this.$http.post('${portal}/userParticipation/v1/pageAll',data).then(res=>{
        if(res.data.state){
          this.registrationList = res.data.value.rows
          this.visible = true
        }
      })
    },
    //流标保存
    handleSave() {
      this.flowing = true
      this.showTroubleshooting = false
      let form = this.flowForm
      form.bidStatus = 8
      form.failBidReason = this.markerFlowValue.failBidReason
      this.$http
        .post('${portal}/matApproval/v1/newRoundQuo', form)
        .then((res) => {
          console.log(res, 'res')
          if (res.data.state) {
            this.$message.success(res.data.message)
            this.loadData()
            this.markerFlowValue = {}
            this.showTroubleshooting = false
            this.flowing = false
          } else {
            this.flowing = false
          }
        })
    },
    //流标
    markerFlow(row) {
      this.flowForm = row
      this.showTroubleshooting = true
    },
    //开标保存
    bidsSave() {
      this.bidsSaveing = true
      let form = this.objData
      if (this.bidOpening.length != 0) {
        form.projectEvaluationExpertList = this.bidOpening
      }
      this.$http
        .post('${portal}/matApproval/v1/openingBid', form)
        .then((res) => {
          console.log(res, 'res')
          if (res.data.state) {
            this.$message.success(res.data.message)
            this.loadData()
            this.bidsLoading = false
            this.bidsSaveing = false
          } else {
            this.bidsSaveing = false
          }
        })
    },
    //开标
    bidsBtn(row) {
      let params = {
        querys: [
          {
            group: 'main',
            operation: 'EQUAL',
            parentGroup: '',
            property: 'noticeId',
            relation: 'AND',
            value: row.id,
          },
        ],
      }
      this.$http
        .post('${portal}/bizBiddingQuotation/v1/matBidOpenOrgData', params)
        .then((res) => {
          console.log(res, 'res')
          if (res.data.state) {
            this.objData = res.data.value
            this.manageData = res.data.value.quotationList
            this.bidsLoading = true
          }
        })
    },
    //获取数据
    loadData() {
      this.tableLoading = true
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      this.$http
        .post('${portal}/matApproval/v1/bidOpenData', this.queryParam)
        .then((res) => {
          console.log(res, 'res')
          this.tableLoading = false
          if (res.data.state) {
            this.tableData = res.data.value.rows
            console.log('返回的表格数据', this.tableData)
            this.queryParam.pageBean = {
              page: res.data.value.page,
              pageSize: res.data.value.pageSize,
              total: res.data.value.total,
            }
          }
        })
    },
    handleDetails(row) {
      console.log(row, 'row')
      let params = {
        querys: [
          {
            group: 'main',
            operation: 'EQUAL',
            parentGroup: '',
            property: 'quo.QUOTATION_COMPANY_ID_',
            relation: 'AND',
            value: row.quotationCompanyId,
          },
          {
            group: 'main',
            operation: 'EQUAL',
            parentGroup: '',
            property: 'quo.NOTICE_ID_',
            relation: 'AND',
            value: row.noticeId,
          },
        ],
      }

      this.$http
        .post('${portal}/matApprovalDetails/v1/bidOpenManageData', params)
        .then((res) => {
          console.log(res, 'res')
          if (res.data.state) {
            this.bidDataList = res.data.value.rows
            this.showDetals = true
          }
        })
    },
    clearSearchForm() {
      this.querys[0].value = ['3', '4', '5']
      this.querys[1].value = '1'
      const moment = require('moment')
      this.querys[2].value = moment().format('YYYY-MM-DD HH:mm:ss')
      this.loadData()
    },
    // 顶部搜索
    handleSearch() {
      this.loadData()
    },
    handleSelectionChange(currentData) {
      this.multipleSelection = currentData
      if (this.multipleSelection.length > 1) {
        this.$refs.multipleSelection.toggleRowSelection(
          this.multipleSelection[0],
          false
        )
      }
    },
    handleCurrentChange(page) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, page}
      this.loadData(this.queryParam)
    },
    handleSizeChange(pageSize) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData(this.queryParam)
    },
  },
  computed: {
    SearchPanel() {
      return {
        '--searchPanel': this.searchPanel + 'px',
      }
    },
  },
}
</script>

<style lang="scss" scoped>

.cloumn-btn {
  border: 1px solid #2761ff;
  padding: 3px 10px;
  border-radius: 5px;
  &:nth-child(1) {
    margin-right: 5px;
  }
  &:nth-child(2) {
    margin-right: 5px;
  }
  &:nth-child(1):hover {
    background-color: #2761ff;
    color: #fff;
  }
  &:nth-child(2):hover {
    background-color: #2761ff;
    color: #fff;
  }
  /deep/ i,
  span {
    font-size: 16px;
  }
}

/deep/ .el-tree-node__content {
  padding: 5px 0;
}

/deep/ .el-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
</style>
