<template>
  <div class="common-page-shadow">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        :model="searchForm"
        label-width="auto"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="项目名称">
          <el-input
            size="small"
            v-model="searchForm.code"
            clearable
            placeholder="请输入项目名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            size="small"
            v-model="searchForm.date"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </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"
            :disabled="!multipleSelection.length"
            class="btn btn-plus"
            @click="handleDetail"
          >
            查看明细
          </el-button>
          <!-- <el-button
            size="small"
            :loading="exporting"
            type="primary"
            :disabled="!multipleSelection.length"
            icon="el-icon-download"
            @click="handleDown"
          >
            导出明细
          </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="noticeCode"
              min-width="100px"
              label="项目编号"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="noticeTitle"
              show-overflow-tooltip
              min-width="120px"
              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="quotationRoundsNum"
              show-overflow-tooltip
              min-width="90px"
              align="center"
              label="报价轮次"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="isQuo"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="是否报价"
              resizable
            >
              <template slot-scope="scope">
                <el-tag type="success" v-if="scope.row.isQuo == 1"
                  >已报价</el-tag
                >
                <el-tag type="danger" v-if="scope.row.isQuo == 0"
                  >未报价</el-tag
                >
              </template>
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              min-width="130px"
              align="center"
              label="开始倒计时"
              resizable
            >
              <template slot-scope="scope">
                <div class="auction">
                  <div id="box">
                    <div
                      id="time"
                      v-if="Date.now() -
                        new Date(scope.row.biddingStartTime).getTime() <
                        0"
                      :class="scope.row.dateTime.seconds > 0 ? 'red' : ''"
                    >
                      <strong>
                        <span id="day"> {{ scope.row.dateTime.days }}天 </span>
                      </strong>
                      <strong>
                        <span id="hour">
                          {{ scope.row.dateTime.hours }}时
                        </span>
                      </strong>
                      <strong>
                        <span id="minute">
                          {{ scope.row.dateTime.minutes }}分
                        </span>
                      </strong>
                      <strong>
                        <span id="second">
                          {{ scope.row.dateTime.seconds }}秒
                        </span>
                      </strong>
                    </div>
                  </div>
                </div>
                <div v-if="Date.now() -
                        new Date(scope.row.biddingStartTime).getTime()>
                        0">
                  {{ (scope.row.dateTime = '进行中') }}
                </div>
                <div v-if="Date.now() -
                        new Date(scope.row.biddingEndTime).getTime()>
                        0">
                  {{ (scope.row.dateTime = '已结束') }}
                </div>
              </template>
            </el-table-column>
            <el-table-column min-width="100px" align="center" label="操作">
              <template v-slot="{row}">
                <!-- <el-tag v-if="(new Date(row.biddingEndTime) - Date.now())<=0" type="danger">
                  已结束
                </el-tag> -->
                <el-button
                  icon="el-icon-position"
                  type="primary"
                  plain
                  size="small"
                  @click="goOffer(row)"
                  >前往报价</el-button
                >
              </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
      title="明细列表"
      :visible.sync="dialogVisible"
      width="70%"
      top="6vh"
      :close-on-click-modal="false"
      :before-close="dialogBeforeClose"
    >
      <el-table
        ref="singleTable"
        :data="detailsList"
        style="width: 100%"
        :max-height="tableHeight"
        highlight-current-row
        :border="true"
        stripe
        fit
        :cell-style="{'text-align': 'center'}"
        :header-cell-style="{'text-align': 'center'}"
      >
        <el-table-column type="index" width="55" label="序号" />
        <el-table-column label="名称" min-width="110" prop="matName">
        </el-table-column>
        <el-table-column
          label="数量"
          min-width="110"
          prop="matNum"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="服务内容" min-width="140" prop="matBaseExt">
        </el-table-column>
        <el-table-column label="服务范围" min-width="140" prop="matOtherExt">
        </el-table-column>
        <el-table-column label="服务开始时间" min-width="140" prop="serviceStartTime">
        </el-table-column>
        <el-table-column label="服务结束时间" min-width="140" prop="serviceEndTime">
        </el-table-column>
        <el-table-column label="预计总金额（万元）" min-width="140" prop="amount">
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import routerManagement from '@/api/BasicDataManagement/routerManagement.js'
import req from '@/request.js'
import utils from '@/utils.js'
const portal = window.context.portal
export default {
  name: 'DemandEntry',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      currentPage: 1,
      tableHeight: 1000,
      tableMaxHeight: 400,
      loading: false,
      tableLoading: false,
      tableData: [],
      dialogVisible: false,
      exporting: false,
      detailsList: [],
      multipleSelection: [],
      addressList: [],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
        sorter: [
          {
            direction: 'DESC',
            property: 'create_time_',
          },
        ],
      },
      searchForm: {}, // 顶部搜索对象
      deleteIds: '', // 是否选中列表解锁删除按钮
      edit: false, // 是否是修改功能
      currentDataDel: [], // 删除时排查是否有在审核的数据
      day: 0,
      hr: 0,
      min: 0,
      sec: 0,
    }
  },
  mounted() {},
  created() {
    this.getAddress()
    this.loadData()
    // 从后端获取的数据，将这一步骤放到异步获取后端数据的地方，对后端返回的数据进行处理
  },
  methods: {
    // 倒计时定时器
    countDownMethod(row) {
      row.countDown = setInterval(() => {
        const date = new Date(row.biddingStartTime) - new Date()
        if (date > 0) {
          row.dateTime = this.formatDate(date)
          this.tableData = [...this.tableData]
        } else {
          clearInterval(row.countDown)
        }
      }, 1000)
    },
    formatDate(time) {
      let days = this.addZero(Math.floor(time / 1000 / 60 / 60 / 24)) //天
      let hours = this.addZero(Math.floor((time / 1000 / 60 / 60) % 24)) //时
      let minutes = this.addZero(Math.floor((time / 1000 / 60) % 60)) //分
      let seconds = this.addZero(Math.floor((time / 1000) % 60)) //秒
      return {
        days,
        hours,
        minutes,
        seconds,
      }
    },
    addZero(d) {
      return parseInt(d) < 10 ? '0' + d : d
    },
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    // 获取送货地址
    getAddress() {
      let params = {
        pageBean: {
          pageSize: -1,
          page: -1,
        },
      }
      this.$http
        .post('${portal}/bizDeliveryAddress/v1/queryByPageOrg', params)
        .then((res) => {
          this.addressList = res.data.rows
        })
    },
    // 导出明细
    handleDown() {
      this.exporting = true
      let data = {
        querys: [],
      }
      data.querys.push({
        group: 'main',
        operation: 'EQUAL',
        parentGroup: '',
        property: 'noticeId',
        relation: 'AND',
        value: this.multipleSelection[0].id,
      })
      let portal = window.context.portal
      let query = {
        url: `${portal}/projectApprovalDetails/v1/exportDataToExcel`,
        method: 'post',
        data: data,
        headers: {
          'Content-type': 'application/json',
        },
        responseType: 'blob', //将文件流转成blob对象
        noErrorMsg: true,
      }
      this.$http.request(query).then((res) => {
        if (res.status == 200) {
          this.$message.success('导出成功')
          this.exporting = false
        } else {
          this.$message.error('导出失败')
          this.exporting = false
        }
      })
    },
    dialogBeforeClose() {
      this.dialogVisible = false
    },
    // 明细
    handleDetail() {
      let data = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
        querys: [],
      }
      data.querys.push({
        group: 'main',
        operation: 'EQUAL',
        parentGroup: '',
        property: 'noticeId',
        relation: 'AND',
        value: this.multipleSelection[0].id,
      },
      {
        group: 'main',
        operation: 'EQUAL',
        parentGroup: '',
        property: 'isDele',
        relation: 'AND',
        value: '0',
      })
      this.$http
        .post('${portal}/projectApprovalDetails/v1/page', data)
        .then((res) => {
          this.dialogVisible = true
          if (res.data.state) {
            this.detailsList = res.data.value.rows
            this.addressList.forEach((item) => {
              this.detailsList.forEach((val) => {
                if (item.code == val.unloadAddress) {
                  val.unloadAddress = item.enterpriseContactAddress
                }
              })
            })
          }
        })
    },
    //获取数据
    loadData() {
      this.tableLoading = true
      this.$http
        .post('${portal}/projectApproval/v1/quoData', this.queryParam)
        .then((res) => {
          if (res.data.state) {
            this.tableData = res.data.value.rows
            this.queryParam.pageBean = {
              page: res.data.value.page,
              pageSize: res.data.value.pageSize,
              total: res.data.value.total,
            }
            this.tableData.forEach((ele) => {
              this.$set(ele, 'dateTime', null)
              this.countDownMethod(ele)
              ele.dateTime = new Date(ele.biddingStartTime) - new Date()
            })
            
            console.log(this.tableData, 'this.tableData')
          }
          this.tableLoading = false
        })
    },
    // 前往报价
    goOffer(rows) {
      this.$refs.form.clearValidate()
      if (rows.procureType == 3) {
        this.$router.push({
          path: `/negotiationsLimited/${rows.id}/${rows.evaluationMethod}`,
          query:{rows}
        })
      } else {
        this.$router.push({
          path: `/negotiationsRecording/${rows.id}/${rows.evaluationMethod}`,
          query:{rows}
        })
      }
    },
    //重置
    clearSearchForm() {
      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;
  }
  // &:nth-child(2):hover {
  //   background-color: #f56c6c;
  //   color: #fff;
  //   border-color: #f56c6c;
  // }
  /deep/ i,
  span {
    font-size: 16px;
  }
}

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

/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;
}
// /deep/.el-dialog__body {
//   height: 580px;
//   overflow-y: auto;
// }
</style>
