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

    <div class="head-container" style="margin: 10px 0 10px 0;">
      <!-- 搜索 -->
      <el-input
        v-model="query.title"
        clearable
        placeholder="请输入标题"
        style="width: 200px;"
        size="small"
        class="filter-item"
      />
      <el-button class="filter-item" size="small" type="primary" icon="el-icon-search" plain @click="handleFind">搜索</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="title" label="标题" align="center" :show-overflow-tooltip="true" />

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

      <el-table-column label="结果" width="100" align="center">
        <template slot-scope="scope">
          <!-- {{scope.row.bidding.status}} -->
          <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>
        </template>
      </el-table-column>

      <el-table-column prop="description" width="100" :show-overflow-tooltip="true" 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="remarks" label="备注" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="appendix" width="100" label="附件" align="center">
        <template slot-scope="scope">
          <!-- <el-link type="primary" :href="scope.row.appendix">查看</el-link> -->
          <el-button type="text" @click="onLoad(scope.row.appendix)">下载</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="endTime" width="160" label="截止时间" 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" :visible.sync="dialog" :title="isAdd ? '投标' : '修标'" width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报价方式">
          <!-- <el-radio v-model="form.priceQuotes" disabled :label="1">总价</el-radio>
            <el-radio v-model="form.priceQuotes" disabled :label="2">单价</el-radio> -->
          <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.number="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>
            <!-- <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</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" />
        <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" @click="onLoad(scope.row.appendix)">下载</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'

export default {
  data() {
    const checkTel = (rule, value, callback) => {
      console.log(rule)
      console.log(value)
      console.log(callback)
      if (value == null) {
        callback(new Error('请选择附件'))
      } else {
        callback()
      }
    }
    return {
      form: {
      },
      data: [],
      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' },
          { type: 'number', message: '报价金额必须为数字值' }
        ],
        appendix: [
          // { type:"array",required: true, message: '附件不为空', trigger: 'blur' },
          { validator: checkTel, trigger: 'blur' }
        ]
      },
      query: {
        type: '',
        value: ''
      },
      fileList: [],
      param: {
        page: 1,
        size: 10
      },
      total1: 0,
      Paramdata: []
    }
  },
  created() {
    this.getDictData()
  },
  methods: {
    // 发布时间
    oncustom() {

    },
    // 获取详情
    getDictData: function() {
      this.loading = true
      const params = {
        page: this.currentPage,
        size: this.pageSize,
        title: this.query.title
      }
      getTableList(params).then(res => {
        console.log(res)
        this.data = res.data.data.records
        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.page = val
      this.getParam()
    },
    handleSizeChangeParam(val) {
      this.param.size = val
      this.getParam()
    }
  }
}
</script>
<style scoped>
/* 去掉上传附件时候的过度效果 */
  ::v-deep .el-upload-list__item{transition: none !important;}
  ::v-deep .el-textarea__inner{
  padding-bottom: 36px;
}
</style>

