<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">
        <!-- 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="index"
              width="55"
              align="center"
              label="序号"
            >
            </el-table-column>
            <el-table-column
              prop="date"
              min-width="20%"
              label="项目名称"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="name"
              show-overflow-tooltip
              min-width="20%"
              align="center"
              label="销售企业"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="auctionStartDate"
              show-overflow-tooltip
              min-width="15%"
              align="center"
              label="开始时间"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="companyName"
              show-overflow-tooltip
              min-width="15%"
              align="center"
              label="结束时间"
              resizable
            >
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              min-width="20%"
              align="center"
              label="开始倒计时"
              resizable
            >
              <template slot-scope="scope">
                <div class="auction" v-if="scope.row.auctionStatus == 1">
                  <div id="box">
                    <div
                      id="time"
                      :class="scope.row.dateTime.days > 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="scope.row.auctionStatus == 2">
                  {{ (scope.row.dateTime = '进行中') }}
                </div>
                <div v-if="scope.row.auctionStatus == 3">
                  {{ (scope.row.dateTime = '已结束') }}
                </div>
              </template>
            </el-table-column>
            <el-table-column min-width="10%" align="center" label="操作">
              <template v-slot="{row}">
                <template>
                  <el-button
                    icon="el-icon-position"
                    type="primary"
                    plain
                    size="small"
                    @click="goOffer(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>
  </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,
      tableMaxHeight: 400,
      loading: false,
      tableLoading: false,
      tableData: [
        {
          id: 1,
          date: '2016-05-02',
          auctionStatus: 1,
          auctionStartDate: '2030-01-01 16:00.00',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          id: 2,
          date: '2016-05-04',
          auctionStatus: 1,
          auctionStartDate: '2030-01-01 16:00.00',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          id: 3,
          date: '2016-05-01',
          auctionStatus: 2,
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          id: 4,
          date: '2016-05-03',
          auctionStatus: 3,
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ],

      multipleSelection: [],

      params: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0
        }
      },
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0
        }
      },
      searchForm: {}, // 顶部搜索对象
      deleteIds: '', // 是否选中列表解锁删除按钮
      activeCollapse1: ['1', '2'],
      fileList: [], // 文件上传
      edit: false, // 是否是修改功能
      currentDataDel: [], // 删除时排查是否有在审核的数据
      day: 0,
      hr: 0,
      min: 0,
      sec: 0,
      rules: {
        code: {
          required: true,
          message: '请输入路线编号',
          trigger: 'blur'
        },
        name: {
          required: true,
          message: '请选择路线名称',
          trigger: 'blur'
        }
      }
    }
  },
  mounted() {
    this.tableData.forEach(ele => {
      ele.dateTime = {
        dd: '**',
        hh: '**',
        mm: '**',
        ss: '**'
      }
      this.countDownMethod(ele)
    })
  },
  created() {
    if (this.$route.params.pageBean) {
      this.params = this.$route.params.pageBean
      this.pageResult = this.$route.params.pageBean.pageBean
    }
    // 从后端获取的数据，将这一步骤放到异步获取后端数据的地方，对后端返回的数据进行处理
  },
  methods: {
    // 倒计时定时器
    countDownMethod(row) {
      row.countDown = setInterval(() => {
        const date = new Date(row.auctionStartDate) - new Date()
        if (date > 0) {
          row.dateTime = this.formatDate(date)
          this.tableData = [...this.tableData]
        } else {
          clearInterval(row.countDown)
        }
      }, 1000)
    },
    formatDate(time) {
      const date = new Date(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)) //秒
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      const hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
      const mm =
        date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
      const ss =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      return {
        days,
        hours,
        minutes,
        seconds
      }
    },
    addZero(d) {
      return parseInt(d) < 10 ? '0' + d : d
    },
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    //获取数据
    loadData(params) {
      this.tableLoading = true
      routerManagement.queryEventsList(params).then(res => {
        console.log(res, 'res')
        this.tableLoading = false
        const data = (res && res.data) || {}
        this.tableData = data.value.rows || []
        console.log('返回的表格数据', this.tableData)
        this.pageResult = {
          page: data.value.page,
          pageSize: data.value.pageSize,
          total: data.value.total
        }
      })
    },
    // 前往报价
    goOffer(rows) {
      this.$refs.form.clearValidate()
      this.$router.push({
        name: 'biddingRecording',
        query: {editRows: rows, pageBean: this.params}
      })
    },
    //重置
    clearSearchForm() {},
    // 顶部搜索
    handleSearch(searchForm) {
      debugger
      let obj = searchForm
      console.log(obj)
      let querys = []
      for (let i in obj) {
        if (obj[i]) {
          querys.push({
            group: 'main',
            operation:
              i == 'name' || i == 'code' || i == 'companyName'
                ? 'LIKE'
                : 'EQUAL',
            parentGroup: '',
            property: i,
            relation: 'AND',
            value: obj[i]
          })
        }
      }
      console.log(querys, 'querys')
      this.params.querys = querys
      console.log(this.params, 'params')
      this.loadData(this.params)
    },
    // 表单提交保存
    submit(status) {
      this.dialogForm.status = status
      // 删除approvalComments参数确保能够保存
      Reflect.deleteProperty(this.dialogForm, 'approvalComments')
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          routerManagement
            .batchSubmit(this.dialogForm)
            .then(res => {
              console.log('res', res)
              if (res.data.state) {
                this.dialog.visible = false
                this.loadData(this.params)
              }
              if (!res.errorMsg) {
                this.$message({
                  message: !this.edit ? '添加成功' : '修改成功',
                  type: 'success'
                })
                this.editVisible = false
              }
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    handleSelectionChange(currentData) {
      this.currentDataDel = currentData
      if (currentData.length === 0) return (this.deleteIds = '')
      let ids = []
      currentData.forEach(item => {
        ids.push(item.id)
      })
      // this.$message.error(`【${item.demandTopic}】已上报待审核不能删除！`)
      this.deleteIds = ids.join(',')
    },
    handleCurrentChange(page) {
      this.params.pageBean = {...this.params.pageBean, page}
      this.loadData(this.params)
    },
    handleSizeChange(pageSize) {
      this.params.pageBean = {...this.params.pageBean, pageSize}
      this.loadData(this.params)
    }
  },
  computed: {
    SearchPanel() {
      return {
        '--searchPanel': this.searchPanel + 'px'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.common-table-content.btn-pagenation {
  height: calc(100% - 60px);
}
.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-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
</style>
