<template>
  <div class="app-container">

 <div class="head-container" style="margin: 10px 0 10px 0;">
      <!-- 搜索 -->
      <span style="fontSize:13px">标题</span>
      <el-input
        v-model="query.value"
        clearable
        placeholder="请输入标题"
        style="width: 200px;"
        size="small"
        class="filter-item"
        @keyup.enter.native="handleFind"
         @clear="handleFind"
      />
      <span style="fontSize:13px;marginLeft:20px">招标类型</span>
      <el-select v-model="query.tenderType" style="width: 200px;" @keyup.enter.native="handleFind" @clear="handleFind" size="small" clearable  value-key="areaId" filterable placeholder="请选择招标类型">
        <el-option
          v-for="item in tenderType"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>

      <el-button class="filter-item" size="small" type="primary" icon="el-icon-search" plain @click="handleFind">搜索</el-button>
      <el-button
        class="filter-item"
        size="small"
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd"
      >发布新标
      </el-button>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" style="width: 100%;">
      <el-table-column label="序号" fixed="left" width="60" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="tenderTitle" label="标题" :show-overflow-tooltip="true" width="250" align="center" />

      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-tag effect="plain" v-if="scope.row.status ==1">进行中</el-tag>
          <el-tag effect="plain" v-if="scope.row.status ==2" type="info">已结束</el-tag>
          <el-tag effect="plain" v-if="scope.row.status ==3" type="danger">已作废</el-tag>
          <el-tag effect="plain" v-if="scope.row.status ==4" type="warning">未开始</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="description" :show-overflow-tooltip="true" label="招标类型" width="100" align="center">
        <template slot-scope="scope"> 
          <el-tag v-if="scope.row.tenderType == 1"  >物流</el-tag>
          <el-tag v-if="scope.row.tenderType == 2" >原料</el-tag>
          <el-tag v-if="scope.row.tenderType == 3" >销售</el-tag>
          <el-tag v-if="scope.row.tenderType == 4" >加工承揽</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" :show-overflow-tooltip="true" label="招标规则" width="100" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.tenderRule == 1" type="success" effect="plain">价低者得</el-tag>
          <el-tag v-if="scope.row.tenderRule == 2" effect="plain" >价高者得</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="description" :show-overflow-tooltip="true" label="是否作废" width="100" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isInvalid == 1"  >正常</el-tag>
          <el-tag v-if="scope.row.isInvalid == 2" type="danger" >已作废</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column prop="fileUrl" label="附件" align="center" width="200">
        <template slot-scope="scope">
          <!-- <el-button type="text" @click="onLoad(scope.row.appendix)">下载</el-button> -->
          <el-link :href="'http://view.officeapps.live.com/op/view.aspx?src='+scope.row.fileUrl" type="primary" target="_target"><i class="el-icon-view"></i> 查看</el-link>
          <el-link :href="scope.row.fileUrl" type="primary" target="_target"><i class="el-icon-download"></i> 下载</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="startTime" label="开始时间" align="center" width="160" />
      <el-table-column prop="endTime" label="截止时间" align="center" width="160" />
      <el-table-column prop="tenderRemark" label="备注" :show-overflow-tooltip="true" align="center" />


      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.tenderStatus == 1 && scope.row.status == null" slot="reference" type="text" icon="el-icon-edit" @click="handleAdd(scope.row)">投标</el-button>
          <el-button v-if="scope.row.tenderStatus == 1 && scope.row.status == 0" slot="reference" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">改标</el-button>
          <el-button v-if="scope.row.tenderStatus == 2 || scope.row.tenderStatus == 3" slot="reference" icon="el-icon-view" type="text" @click="handleView(scope.row)">投标详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div class="pagination">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 投标详情 -->
    <el-dialog :append-to-body="true" fullscreen :visible.sync="dialog" title="投标详情" center>
        <!-- <el-steps :active="active" align-center finish-status="success">
          <el-step title="选择标" ></el-step>
          <el-step title="投递标"></el-step>
        </el-steps> -->
      <ckTable ref="ckTable" @click="handleSelectionChange" v-show="active == 0"></ckTable>

    <div style="height:20px"></div>

       <table border="1px" cellpadding="10" cellspacing="0" class="ckTable" v-show="active == 1">
        <tr>
            <td colspan="4" style="font-weight: 600;fontSize:20px">物 流 报 价 单</td>
        </tr>
        <tr>
            <td style="width:15%;color: #909399;fontWeight: 600;">发起单位：</td>
            <td style="width:30%">上海国林源诚物流发展有限公司</td>
            <td style="width:15%;color: #909399;fontWeight: 600;">招标方式：</td>
            <td style="width:30%">邀请招标</td>
        </tr>
    </table>
      <!-- <el-table  :data="dialogTableData" border v-show="active == 1">
            <el-table-column label="货物" width="275px" fixed="left" align="center" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span>散稻</span>
              </template>
            </el-table-column>

             <el-table-column label="数量（吨）" width="250" align="center" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <span> 10</span>
                </template>
              </el-table-column>

      <el-table-column label="预估运输距离（公里/海里）" width="300" align="center" >
        <template slot-scope="scope">
            <el-input v-model="input"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="运输单价含税价（元/吨）" width="275" align="center" >
        <template slot-scope="scope">
            <el-input v-model="input"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="合计运输金额（元）" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>10000.2002</span>
        </template>
      </el-table-column>

      </el-table> -->

    <!-- <div style="height:10px"></div> -->

        <table border="1px" cellpadding="10" cellspacing="0" class="ckTable">
        <tr>
            <td style="width:25%;color: #909399;fontWeight: 600;">项目要求</td>
            <td style="width:75%;textAlign:left">
                1、报价必须完整，报价单一经发出，投标方不得随意更改信息及报价表格式（联运需尽量提供分段分解价格），不得插入、删除行或列（联运分段标注除外），不得空报，一旦更改报价表格式，将视为废标处理；<br/>
                2、投标方有能力完成物流及相关运输服务、储存监管照片等资料的上传、梳理归档；有能力获取储存监控设备后自行安装和回收管理，办理相关影像资料的录入；<br/>
                3、运输过程中投标方需按国林源诚要求提供固定车队，所有车辆/船舶均安装GPS定位系统，主动配合完成车（船）载摄像头安装，并按照国林源诚相关要求完成物流监管系统的监管任务，自愿接受国林源诚统一运输管理；<br/>
                4、如需转存、倒仓等操作需提前三个工作日以书面形式上报至国林源诚审批备案。作业价格为包干单价，含提送货费、保险费、税金以及市场风险全包等一切费用，报价一旦发出，视为报价有效，不做任何更改说明。
                5、中标方需提供的增值税专用发票为 9  %；若提供其它税率的发票，按中标价折扣运费税差。<br/>
                6、如发生运输作业，运输损耗标准按照：按每车计算，省内不得超过每车货物数量的1 ‰，省外不得超过每车货物数量的2‰。<br/>
                7、在储存库点作业时，中标公司应有能力提供翻转平台或吊装机至储存库点作业<br/>
                8、中标方需按照国林源诚要求保证运输货物完好、不撒漏、防偷盗、防雨湿，如发生任何质量问题，将追究投标方违约责任；
            </td>
        </tr>
        <tr>
            <td style="width:25%;color: #909399;fontWeight: 600;">开标时间</td>
            <td>以系统实际开标为准</td>
        </tr>
        <tr>
            <td style="width:25%;color: #909399;fontWeight: 600;">保证金</td>
            <td>中标公司按照10元/吨的保证金于中标后3个工作日支付至上海国林源诚物流发展有限公司指定收款银行</td>
        </tr>
        <tr>
            <td style="width:25%;color: #909399;fontWeight: 600;">投标方服务承诺</td>
            <td>本公司承诺以上内容真实有效</td>
        </tr>
    </table>
    <div style="height:100px"></div>

      <div class="btn_fixed">
        <el-button style="margin-top: 12px;" type="primary" plain @click="dialog = false">取消</el-button>
         <el-button style="margin-top: 12px;" type="primary" plain @click="active--" v-show="active == 1">上一步</el-button>
        <el-button style="margin-top: 12px;" type="primary" plain @click="next" v-show="active == 0">下一步</el-button>
      </div>

    </el-dialog>


    <!--新增-->
    <!-- <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '投标' : '修标'" width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报价方式">
          <el-tag v-if="form.priceQuotes == 1">总价</el-tag>
          <el-tag v-if="form.priceQuotes == 2">单价</el-tag>
        </el-form-item>
        <el-form-item label="报价" prop="price">
          <el-input v-model="form.price" style="width: 250px;" placeholder="请输入金额"><template slot="append">元</template></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="form.remarks"
            type="textarea"
            style="width: 370px;"
            :autosize="{ minRows: 2, maxRows: 10}"
            maxlength="500"
            show-word-limit
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="附件" :required="true" prop="appendix">
          <el-upload
            class="upload-demo"
            drag
            :limit="1"
            :on-preview="handlePreview"
            :on-change="handleChange"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-exceed="handleExceed"
            :action="BASE_API+'/bidding/upload'"
            list-type="text"
            :file-list="fileList"
            multiple
          >
            <i class="el-icon-upload" />
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog> -->

    <!-- 详情 -->
    <el-dialog :append-to-body="true" :visible.sync="viewDialog" title="投标详情" width="90%" center>
      <el-table center :data="Paramdata" style="width: 100%;">

        <el-table-column prop="customerName" label="客户名称" align="center" />

        <el-table-column label="报价方式" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.priceQuotes == 1" color="#5783E6" style="border-color:#5783E6" effect="dark">总价</el-tag>
            <el-tag v-if="scope.row.priceQuotes == 2" effect="dark" color="#82C497" style="border-color:#82C497">单价</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="price" :show-overflow-tooltip="true" label="报价金额" align="center" >
          <template slot-scope="scope">
            <div>
              {{ scope.row.price | toThousandFilter }}
              <!-- {{ 0.0000 | toThousandFilter }} -->
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注" :show-overflow-tooltip="true" align="center" />
        <el-table-column prop="appendix" label="附件" align="center" width="100">
          <template slot-scope="scope">
            <!-- <el-link type="primary" :href="scope.row.appendix">查看</el-link> -->
            <el-button type="text" v-if="scope.row.appendix" @click="onLoad(scope.row.appendix)">下载</el-button>
            <el-button type="text" v-else disabled>下载</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="投极时间" align="center" />
        <el-table-column prop="userName" label=" 投标帐号" align="center" />
        <el-table-column prop="status" label=" 投标结果" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == null" type="info">未参与</el-tag>
            <el-tag v-if="scope.row.status == 0" type="warning">已提交</el-tag>
            <el-tag v-if="scope.row.status == 1" type="success">已中标</el-tag>
            <el-tag v-if="scope.row.status == 2" type="danger">未中标</el-tag>
          <!-- <span v-if="scope.row.status == 0">已提交</span>
          <span v-if="scope.row.status == 1">中标</span>
          <span v-if="scope.row.status == 2">未中标</span> -->
          </template>
        </el-table-column>

      </el-table>
      <div class="pagination">
        <el-pagination
          :current-page.sync="param.page"
          :page-size="param.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total1"
          :page-sizes="[10, 20, 30, 50]"
          background
          @size-change="handleSizeChangeParam"
          @current-change="handleCurrentChangeParam"
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewDialog = false">取消</el-button>
        <el-button :loading="loading" type="primary" @click="viewDialog = false">确认</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import { getTableList, getQueryAdd, getQueryEdit, getQueryId } from '@/api/tender'
import { Notification, MessageBox } from 'element-ui'
import ckTable from './table.vue'

export default {
  components:{
    ckTable
  },
  data() {
    const checkTel = (rule, value, callback) => {
      if (value == null) {
        callback(new Error('请选择附件'))
      } else {
        callback()
      }
    }
    return {
      form: {
      },
      data: [{tenderStatus:1}],
      active:0,
      BASE_API: process.env.BASE_API,
      dialog: false,
      loading: false,
      delLoading: false,
      viewDialog: false,
      isAdd: false,
      currentPage: 1,
      pageSize: 10,
      total: 0, // 总数量
      rules: {

        price: [
          { required: true, message: '报价金额不为空', trigger: 'blur' },
        ],
        appendix: [
          // { type:"array",required: true, message: '附件不为空', trigger: 'blur' },
          { validator: checkTel, trigger: 'blur' }
        ]
      },
      query: {
        type: '',
        value: '',
        tenderType:""
      },
      fileList: [],
      param: {
        current: 1,
        size: 10
      },
      total1: 0,
      Paramdata: [],
      dialogTableData:[]
    }
  },
  created() {
    this.getDictData()
  },
  methods: {
    next() {
      console.log(this.$refs.ckTable)
        if (this.active++ > 2) this.active = 0;
        this.dialogTableData = this.$refs.ckTable.SelectionList
      },
      handleSelectionChange(row){
        
      },
    // 获取详情
    getDictData: function() {
      this.loading = true
        const params = new URLSearchParams()
      params.append('current', this.currentPage)
      params.append('size', this.pageSize)
      params.append('tenderType', this.query.tenderType)
      params.append('tenderTitle', this.query.value)
      getTableList(params).then(res => {
          console.log(res)
        // this.data = res.data.data.records
        let data = res.data.data.records
        console.log(new Date().valueOf(),"当前时间")
        data.map((res)=>{
          // 开始时间
          let startTime = res.startTime
          // 结束时间
          let endTime = res.endTime

          const date = new Date(startTime)
          const modify = new Date(endTime)

          const start = date.valueOf()         //开始时间
          const end = modify.valueOf()        //结束时间
          const time = new Date().valueOf()   //当前时间
          console.log(start,"start")
          console.log(end,"end")
         if(res.isInvalid == 2){
             res.status = 3
            console.log("作废了")
          }else if(time < start){
            res.status = 4
            console.log("当前时间小于开始时间，未开始")
          }else if(time > start && time < end){
            res.status = 1
            console.log("当前时间大于开始时间并且小于结束时间，进行中")
          }else if(time > end){
            res.status = 2
            console.log("当前时间大于结束时间，已结束")
          }

          return res
        })
        this.data = data
        this.total = res.data.data.total
        this.loading = false
      })
    },

    // 搜索
    handleFind: function() {
      this.currentPage = 1
      this.getDictData()
    },

    // 文件移除的时候
    handleRemove(file, fileList) {
      this.form.appendix = ''
      console.log(file, fileList, this.fileList, '移出')
    },
    // 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    handleChange(file, fileList) {
      console.log(file, fileList, '上传')
      if (file.response) {
        console.log(file.response, 'file')
        if (file.response.code == '200') {
          this.form.appendix = file.response.data
        } else {
          Notification.error({
            title: file.response.msg,
            duration: 1000
          })
          this.fileList = []
        }
      }
    },
    // 点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
      console.log(file, 'file1')
      // window.location.href = file.url
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个附件，本次选择了 ${files.length} 个文件`)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    // 下载
    onLoad(row) {
      // const params = new URLSearchParams()
      // params.append('path', row)
      const a = document.createElement('a')
      // a.href= /bidding/load
      const url = window.location.origin + process.env.BASE_API + '/bidding/load?path=' + encodeURIComponent(row)
      a.href = url
      a.click()
    },

    // 字典名称提交动作
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            // 新增字典
            this.loading = true
            getQueryAdd(this.form).then((res) => {
              if (res.data.code === 200) {
                this.$message({ message: '操作成功', type: 'success' })
              } else {
                this.$message({ message: res.data.msg, type: 'error' })
              }
              this.dialog = false
              this.loading = false
              this.getDictData()
              this.$refs['form'].resetFields()
            }).catch(err => {
              this.loading = false
            })
          } else {
            // 更新字典
            getQueryEdit(this.form).then((res) => {
              if (res.data.code === 200) {
                this.$message({ message: '操作成功', type: 'success' })
              } else {
                this.$message({ message: res.data.msg, type: 'error' })
              }
              this.dialog = false
              this.getDictData()
            })
          }
        }
      })
    },
    // 添加字典
    handleAdd: function(row) {
      this.dialog = true
      this.isAdd = true
      this.form = {}
      this.fileList = []
      this.form.tenderId = row.id
      this.form.priceQuotes = row.priceQuotes
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
    },
    // 编辑字典
    handleEdit: function(row) {
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
      this.dialog = true
      this.isAdd = false
      this.form = row.bidding
      this.form.priceQuotes = row.priceQuotes
      if (this.form.appendix) {
        let name = this.form.appendix.split('bidding/')
        name = name.pop()
        // let name = data.appendix.replace(/(.*\/)*([^.]+).*/ig, "$2");
        this.fileList = [{ name: name, url: this.form.appendix }]
      }
    },
    handleView(row) {
      this.param.tenderId = row.id
      this.param.page = 1
      this.getParam()
      this.viewDialog = true
    },
    getParam() {
      const data = this.param
      getQueryId(data).then((res) => {
        console.log(res.data.data.records, '')
        this.Paramdata = res.data.data.records
        this.total1 = res.data.data.total
      })
    },

    // 换页
    handleCurrentChange: function(val) {
      this.currentPage = val
      this.getDictData()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getDictData()
    },
    handleCurrentChangeParam(val) {
      this.param.current = val
      this.getParam()
    },
    handleSizeChangeParam(val) {
      this.param.size = val
      this.getParam()
    }
  }
}
</script>
<style scoped lang="scss">
/* 去掉上传附件时候的过度效果 */
  ::v-deep .el-upload-list__item{transition: none !important;}
  ::v-deep .el-textarea__inner{
  padding-bottom: 36px;
}
.btn_fixed{
  position: fixed;
  bottom: 0;
  left: 0;
  padding: 15px 50px;
  width: 100%;
  text-align: right;
  background: #f5f5f5;

}
.ckTable{
        width: 100%;
        border: #eee;
        text-align: center;
        tr,td{
        border: 0.5px solid#dfe6ec;
        line-height: 23px;
        }
    }
</style>

