<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <!--<span>时间段:</span>-->
        <!--<el-date-picker-->
        <!--v-model="startEndDate"-->
        <!--style="width:250px"-->
        <!--size="small"-->
        <!--type="daterange"-->
        <!--value-format="yyyy-MM-dd"-->
        <!--format="yyyy-MM-dd"-->
        <!--start-placeholder="开始日期"-->
        <!--end-placeholder="结束日期"/>-->
        <el-row>
          <!--<el-select v-model="contractNumValue" placeholder="合同号" size="small" filterable clearable>-->
          <!--<el-option-->
          <!--v-for="item in contractNum"-->
          <!--:key="item.value"-->
          <!--:label="item.label"-->
          <!--:value="item.value">-->
          <!--<div style="float: left">{{ item.label }}</div>-->
          <!--<div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>-->
          <!--</el-option>-->
          <!--</el-select>-->
          <!--<span>供应商:</span>-->
          <el-input v-model="supplierName" placeholder="供应商" size="small" clearable style="width:180px" @keyup.enter.native="total1=0;pageCurrent1=1;pageSize1=30;searchTable1()"/>
          <!--<span>合同编号:</span>-->
          <el-input v-model="contractNumber" placeholder="合同编号" size="small" clearable style="width:200px" @keyup.enter.native="total1=0;pageCurrent1=1;pageSize1=30;searchTable1()"/>
          <!--<span>采购员:</span>-->
          <el-select v-model="personValue" placeholder="采购员" size="small" clearable style="width: 100px">
            <el-option
              v-for="item in person"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
          <!--<span>审核情况:</span>-->
          <el-select v-model="revieweValue" placeholder="审核情况" size="small" style="width: 100px">
            <el-option
              v-for="item in reviewe"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
          <!--<span>审批情况:</span>-->
          <el-select v-model="approvalValue" placeholder="审批情况" size="small" style="width: 100px">
            <el-option
              v-for="item in approval"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
          <el-button
            type="success"
            size="small"
            icon="el-icon-search"
            style="margin-left:10px"
            @click="total1=0;pageCurrent1=1;pageSize1=30;searchTable1()">查询</el-button>
          <el-tooltip :open-delay="1000" effect="dark" content="打印采购合同" placement="top">
            <el-button
              type="warning"
              size="small"
              icon="el-icon-download"
              @click="download()">导出</el-button>
          </el-tooltip>
        </el-row>
      </div>
      <el-table
        v-loading="loading1"
        id="expandTable"
        :data="tableData1"
        size="mini"
        border
        style="width: 100%"
        height="300px"
        highlight-current-row
        @row-click="searchTable2">
        <el-table-column label="详情" type="expand">
          <template slot-scope="scope">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-row>
                <el-form-item label="交货日期">
                  {{ scope.row.交货日期 }}
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="支付日期">
                  {{ scope.row.支付日期 }}
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="支付方式">
                  {{ scope.row.支付方式 }}
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="开票信息">
                  {{ scope.row.开票信息 }}
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="其他说明">
                  {{ scope.row.其他说明 }}
                </el-form-item>
              </el-row>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="供应商" prop="供应商名称" align="center" min-width="150" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <!--        <el-table-column label="采购合同名称" align="center" min-width="150">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.采购合同名称 }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="采购合同号" prop="采购合同编号" align="center" min-width="150" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.采购合同编号 }}
          </template>
        </el-table-column>
        <el-table-column label="合同审核" prop="审核情况内容" align="center" width="70">
          <template slot-scope="scope">
            <span v-if="scope.row.审核情况内容==='未审核'">未审核</span>
            <span v-if="scope.row.审核情况内容==='通过审核'">已通过</span>
            <span v-if="scope.row.审核情况内容==='未通过审核'">未通过</span>
            <!--            <el-tag v-if="scope.row.审核情况内容==='未审核'" type="primary" size="small">未审核</el-tag>-->
            <!--            <el-tag v-if="scope.row.审核情况内容==='通过审核'" type="success" size="small">已通过</el-tag>-->
            <!--            <el-tag v-if="scope.row.审核情况内容==='未通过审核'" type="danger" size="small">未通过</el-tag>-->
          </template>
        </el-table-column>
        <el-table-column label="合同审批" prop="审批情况内容" align="center" width="70">
          <template slot-scope="scope">
            <span v-if="scope.row.审批情况内容==='未审批'">未审批</span>
            <span v-if="scope.row.审批情况内容==='通过审批'">已通过</span>
            <span v-if="scope.row.审批情况内容==='不通过审批'">未通过</span>
            <!--            <el-tag v-if="scope.row.审批情况内容==='未审批'" type="primary" size="small">未审批</el-tag>-->
            <!--            <el-tag v-if="scope.row.审批情况内容==='通过审批'" type="success" size="small">已通过</el-tag>-->
            <!--            <el-tag v-if="scope.row.审批情况内容==='不通过审批'" type="danger" size="small">未通过</el-tag>-->
          </template>
        </el-table-column>
        <el-table-column label="未审核原因" align="center" min-width="90" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.未通过审核原因 ? scope.row.未通过审核原因 : '—' }}
          </template>
        </el-table-column>
        <el-table-column label="未审批原因" align="center" min-width="90" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.未通过原因 ? scope.row.未通过原因 : '—' }}
          </template>
        </el-table-column>
        <el-table-column label="采购员" prop="姓名" align="center" width="60">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="总金额" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.合同总额 }}
          </template>
        </el-table-column>
        <el-table-column label="已付金额" align="center" width="90" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.付款金额 }}
          </template>
        </el-table-column>
        <el-table-column label="欠款金额" align="center" width="90" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.欠款金额 }}
          </template>
        </el-table-column>
        <!--        <el-table-column label="审核时间" align="center" width="80">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.审核时间 ? scope.row.审核时间.substr(0,9) : '—' }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="审批时间" align="center" min-width="80">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.审批时间 ? scope.row.审核时间.substr(0,9) : '—' }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="处理" align="center" width="110">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-tag v-if="Number(scope.row.是否未通过)===1&&Number(scope.row.不通过处理)===1" type="success" size="small">处理完成</el-tag>-->
        <!--            <el-button v-else-if="Number(scope.row.是否未通过)===1&&Number(scope.row.不通过处理)===0" plain type="primary" size="mini" @click="done(scope.row)">点击处理</el-button>-->
        <!--            <span v-else>—</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="到货进度" align="center" min-width="150">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-progress-->
        <!--              :text-inside="true"-->
        <!--              :stroke-width="18"-->
        <!--              :percentage="scope.row.到货进度 = (100*(Number(scope.row.合同到货总数))/scope.row.合同采购总数) > 100 ? 100 : Number((100*(Number(scope.row.合同到货总数))/scope.row.合同采购总数).toFixed(2))"-->
        <!--              status="success"/>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="操作" align="center" width="70">
          <template slot-scope="scope">
            <el-button :disabled="false" type="text" size="mini" icon="el-icon-delete" @click="deleteContract(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent1"
          :page-sizes="[20, 30, 40, 100]"
          :page-size="pageSize1"
          :total="total1"
          style="display:inline-block;width:50%"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
    </el-card>
    <div id="print" class="print" style="background-color:white;min-height:500px;margin: 0px auto;width:800px;border:1px solid black" v-html="returns"/>
    <el-card v-show="searchView">
      <!--将新一般处理页添加至通讯服务器-->
      <div>附件图：</div>
      <img v-if="searchView" :src="searchView">
    </el-card>
  </div>
</template>

<script>
import { initContract, initBuyer, searchTable1, searchTable2, handleDone, deleteContract } from '@/api/PurchasingCenter/PurchaseContractSearch'
import QRCode from 'qrcode'
import $ from 'jquery'
// import { exportExcelMethod } from './exportExcel'

export default {
  data() {
    return {
      contractNumber: '',
      searchView: '', // 图片预览
      approval: [{
        value: 0,
        label: '全部'
      }, {
        value: 1,
        label: '已通过'
      }, {
        value: 2,
        label: '未通过'
      }, {
        value: 3,
        label: '未审批'
      }],
      approvalValue: 0,
      reviewe: [{
        value: 0,
        label: '全部'
      }, {
        value: 1,
        label: '已通过'
      }, {
        value: 2,
        label: '未通过'
      }, {
        value: 3,
        label: '未审核'
      }],
      revieweValue: 0,
      RMB: '',
      TotalAmount: '',
      check1: 0,
      check2: 0,
      check3: 0,
      check4: 0,
      check5: 0,
      startEndDate: '',
      contract: '',
      freight: '',
      contractNumValue: '',
      contractNum: [],
      ProcurementContractNum: '',
      personValue: '',
      person: [],
      supplier: '',
      supplierName: '',
      total1: 0,
      pageCurrent1: 1,
      pageSize1: 30,
      tableData1: [],
      loading1: false,
      returns: '',
      tableData01: [],
      tableData02: [],
      submitDisable: true,
      disable: [], // 删除按钮禁用
      tableData2: [],
      textarea: ``,
      contractValue: '' // 显示表2和变更合同用的变量
    }
  },
  created() {
    this.fetchData()
    this.searchTable1()
  },
  methods: {
    // 处理完成
    done(row) {
      this.$confirm('确定处理完成?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        handleDone(row.采购合同流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('操作成功')
            this.searchTable1()
          } else { this.$message.error('操作失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    deleteContract(row) { // 删除合同
      this.$confirm('此操作将永久删除合同所有内容，确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteContract(row.采购合同流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable1()
            this.returns = ''
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    download() { // 导出打印
      if (this.contractValue) {
        // exportExcelMethod('print', '采购合同', '采购合同sheet1')
        const htmlNode = $('#print').html()
        const body = $('body').attr('style', 'padding: 20px;margin: 0 auto;display: block;width: 800px;background: #fff;')
        body.children().hide()
        const printNode = $(htmlNode)
        body.append(printNode)
        window.print()
        body.removeAttr('style')
        body.children().not('script').show()
        body.children().not('#app').hide()
        printNode.remove()
      } else {
        this.$message.error('请选择合同')
      }
    },
    useqrcode(contractNum) { // 二维码
      const opts = {
        errorCorrectionLevel: 'H',
        type: 'image/jpeg',
        rendererOpts: {
          quality: 0.3
        }
      }
      const str = contractNum
      QRCode.toDataURL(str, opts, function(err, url) {
        if (err) throw err
        const img = document.getElementById('img')
        img.src = url
      })
    },
    fetchData() {
      initContract().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.contractNum.push({
            label: response.data[i].采购合同编号,
            name: response.data[i].采购合同名称,
            value: response.data[i].采购合同流水号
          })
        }
      })
      initBuyer().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.person.push({
            text: response.data[i].姓名,
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
    },
    searchTable1() {
      this.tableData1 = []
      this.tableData2 = []
      this.textarea = ``
      this.loading1 = true
      this.disable = []
      if (this.startEndDate === '' || this.startEndDate === null || this.startEndDate[0] === '' || this.startEndDate[1] === '') {
        this.check1 = 0
        this.startEndDate = ''
      } else { this.check1 = 1 }
      this.contractNumValue ? this.check2 = 1 : this.check2 = 0
      this.personValue ? this.check3 = 1 : this.check3 = 0
      this.supplierName ? this.check4 = 1 : this.check4 = 0
      if (this.approvalValue === '' || this.approvalValue === 0) {
        this.check5 = 0
      } else {
        this.check5 = 1
      }
      let check, check2
      this.contractNumber ? check = 1 : check = 0
      this.revieweValue ? check2 = 1 : check2 = 0
      searchTable1(this.pageCurrent1, this.pageSize1, this.check1, this.startEndDate[0], this.startEndDate[1], this.check2, this.contractNumValue, this.check3, this.personValue, this.check4, this.supplierName, this.check5, this.approvalValue, check, this.contractNumber, check2, this.revieweValue).then(response => {
        for (let i = 0; i < response.data.rows.length; i++) {
          response.data.rows[i].审批情况内容 === '未审批' ? this.disable.push(false) : this.disable.push(true) // 删除按钮禁用
        }
        if (response.data.rows.length !== 0) {
          for (let i = 0; i < response.data.rows.length; i++) {
            this.tableData1.push({
              供应商名称: response.data.rows[i].供应商名称,
              采购合同编号: response.data.rows[i].采购合同编号,
              未通过审核原因: response.data.rows[i].未通过审核原因,
              审核情况内容: response.data.rows[i].审核情况内容,
              审批情况内容: response.data.rows[i].审批情况内容,
              未通过原因: response.data.rows[i].未通过原因,
              合同总额: response.data.rows[i].合同总额,
              付款金额: response.data.rows[i].付款金额,
              是否未通过: response.data.rows[i].是否未通过,
              不通过处理: response.data.rows[i].不通过处理,
              姓名: response.data.rows[i].姓名,
              合同金额: response.data.rows[i].合同金额,
              欠款金额: response.data.rows[i].欠款金额,
              采购合同流水号: response.data.rows[i].采购合同流水号
            })
          }
        }
        this.total1 = response.data.total
        this.loading1 = false
      })
    },
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    searchTable2(row) {
      this.contractValue = parseInt(row.采购合同流水号)
      searchTable2(this.contractValue).then(response => {
        this.returns = response.data[0].合同内容
      }).then(() => {
        $('#contractName').next('td').html(`<img id="img">`)
        this.useqrcode(row.采购合同流水号) // 显示二维码
      })
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .el-date-editor{
    width:200px
  }
  .el-select{
    width:150px;
    margin-right: 10px;
  }
  .el-input{
    width:150px;
    margin-right: 10px;
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .searchForm .el-form-item{
    margin-bottom: 5px;
  }
  .el-tag{
    margin: 0
  }
</style>
<style>
  #expandTable .el-form-item {
    margin: 0;
  }
</style>
