<template>
  <div class="app-container">
    <el-card>
      <span>合同号:</span>
      <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-select v-model="personValue" placeholder="采购员" clearable size="small" style="width: 100px">
        <el-option
          v-for="item in person"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>供应商:</span>
      <el-input v-model="supplierName" placeholder="供应商" size="small" clearable style="width:180px"/>
      <el-button
        type="success"
        size="small"
        icon="el-icon-search"
        style="margin-left:10px"
        @click="total1=0;pageCurrent1=1;pageSize1=10;searchTable1()">查询</el-button>
    </el-card>

    <el-card>
      <el-table
        v-loading="loading1"
        :data="tableData1"
        size="mini"
        border
        style="width: 100%;max-height: 300px;"
        height="300px"
        highlight-current-row
        @row-click="searchTable2">
        <el-table-column label="采购合同号" align="center" min-width="150">
          <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="预到货时间" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.预到货时间 }}
          </template>
        </el-table-column>
        <el-table-column label="规定到货时间" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.规定到货时间 }}
          </template>
        </el-table-column>
        <el-table-column label="供应商" align="center" min-width="300">
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column label="采购员" align="center" min-width="70">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="总金额" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.总金额 }}
          </template>
        </el-table-column>
        <el-table-column label="付款方式" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.付款方式内容 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="100">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit-outline" @click="Open(scope.row)">审批</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent1"
          :page-sizes="[10, 20, 30, 40]"
          :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>

    <el-card>
      <div style="width:900px;margin-left: 350px">
        <el-card>
          <table id="1" cellspacing="0px" align="center" border width="100%" style="">
            <tr style="height: 40px">
              <td colspan="1" align="center" style="font-weight: bold">GAOJING</td>
              <td colspan="3" align="center" style="font-weight: bold">产品销售合同</td>
            </tr>
            <tr style="height: 40px">
              <td colspan="1" align="center">需方</td>
              <td colspan="1" align="center">江苏高精机电装备有限公司</td>
              <td colspan="1" align="center">合同编号</td>
              <td colspan="1" align="center">{{ contract }}</td>
            </tr>
            <tr style="height: 40px">
              <td colspan="1" align="center">供方</td>
              <td colspan="1" align="center">{{ supplier }}</td>
              <td colspan="1" align="center">签订地点</td>
              <td colspan="1" align="center">江苏盐城</td>
            </tr>
          </table>
          <div>一、 产品名称、型号、数量、单价、品牌：</div>
          <table id="2" cellspacing="0px" align="center" border width="100%" style="">
            <tbody id="4">
              <tr id="tableHead" style="height: 40px">
                <td colspan="1" align="center">物料名称</td>
                <td colspan="1" align="center">物料规格</td>
                <td colspan="1" align="center">物料型号</td>
                <td colspan="1" align="center">发货天数</td>
                <td colspan="1" align="center">数量</td>
                <td colspan="1" align="center">单价</td>
                <td colspan="1" align="center">总价</td>
              </tr>
            </tbody>
            <tfoot>
              <tr style="height: 40px">
                <td colspan="6">运费：</td>
                <td colspan="1" align="center">{{ freight }}</td>
              </tr>
              <tr style="height: 40px">
                <td colspan="6">总价:  (人民币){{ RMB }}(含0.17增值税) </td>
                <td colspan="1" align="center">{{ TotalAmount }}</td>
              </tr>
            </tfoot>
          </table>
          <div id="23"/>
        </el-card>
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogVisible3" title="审批" center style="min-height: 300px;" width="20%">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-form-item label="审批是否通过" prop="审批是否通过" center>
          <el-select v-model="form2.审批是否通过" size="small" @change="selectChange">
            <el-option
              v-for="item in approval"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="未通过原因" prop="未通过原因">
          <el-input v-model="form2.未通过原因" :disabled="disabled" size="small"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible3=false">取消</el-button>
        <el-button type="primary" size="small" @click="submitApproval('form2')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { initContract, initBuyer, searchTable1, searchTable2, submitApproval } from '@/api/PurchasingCenter/PurchaseContractApproval'

export default {
  data() {
    return {
      person: [],
      personValue: '',
      RMB: '',
      TotalAmount: '',
      check1: 0,
      check2: 0,
      check3: 0,
      check4: 0,
      startEndDate: '',
      contract: '',
      freight: '',
      contractNumValue: '',
      contractNum: [],
      ProcurementContractNum: '',
      supplier: '',
      supplierName: '',
      total1: 0,
      pageCurrent1: 1,
      pageSize1: 10,
      tableData1: [],
      loading1: false,
      dialogVisible3: false,
      returns: [],
      tableData01: [],
      submitDisable: true,
      approval: [
        {
          value: 1,
          label: '是'
        },
        {
          value: 2,
          label: '否'
        }
      ],
      disabled: true,
      form2: {
        审批是否通过: 1,
        未通过原因: ''
      },
      rules2: {},
      tableData2: [],
      textarea: ``,
      contractValue: '' // 显示表2和变更合同用的变量
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      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].人员编号
          })
        }
      })
      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].采购合同流水号
          })
        }
      })
    },
    searchTable1() {
      this.tableData2 = []
      this.textarea = ``
      this.loading1 = true
      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
      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).then(response => {
        this.loading1 = false
        for (let j = 0; j < response.data.rows.length; j++) {
          if (response.data.rows[j].预到货时间 !== null) {
            response.data.rows[j].预到货时间 = response.data.rows[j].预到货时间.substring(0, response.data.rows[j].预到货时间.indexOf(' '))
          }
          if (response.data.rows[j].规定到货时间 !== null) {
            response.data.rows[j].规定到货时间 = response.data.rows[j].规定到货时间.substring(0, response.data.rows[j].规定到货时间.indexOf(' '))
          }
        }
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    Open(row) {
      this.dialogVisible3 = true
      this.ProcurementContractNum = row.采购合同流水号
    },
    selectChange() {
      if (this.form2.审批是否通过 === 2) {
        this.disabled = false
      }
    },
    submitApproval(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          submitApproval(this.ProcurementContractNum, this.form2.审批是否通过, this.form2.未通过原因).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('审批成功')
              this.dialogVisible3 = false
              this.searchTable1()
            } else { this.$message.error('审批失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    searchTable2(row) {
      this.textarea = row.合同文本
      this.supplier = row.供应商名称
      this.contract = row.采购合同编号
      this.freight = parseInt(row.总金额) - parseInt(row.总金额_小计)
      this.RMB = row.总金额_文本
      this.TotalAmount = row.总金额
      document.getElementById('23').innerText = this.textarea
      this.contractValue = parseInt(row.采购合同流水号)
      searchTable2(this.contractValue).then(response => {
        this.returns = response.data
        const children = document.getElementsByClassName('tableData')
        const parent = document.getElementById('4')
        if (children.length !== 0) {
          for (let i = children.length - 1; i >= 0; i--) {
            parent.removeChild(children[i])
          }
        }
        const tr = []
        for (let i = 0; i < this.returns.length; i++) {
          tr[i] = document.createElement('tr')
          tr[i].setAttribute('class', 'tableData')
          tr[i].innerHTML = '<td colspan="1" align="center"></td>' + '<td colspan="1" align="center"></td>' + '<td colspan="1" align="center"></td>' + '<td colspan="1" align="center"></td>' +
              '<td colspan="1" align="center"></td>' + '<td colspan="1" align="center"></td>' + '<td colspan="1" align="center"></td>'
          document.getElementById('tableHead').after(tr[i])
        }
        for (let j = 0; j < this.returns.length; j++) {
          document.getElementsByClassName('tableData')[j].children[0].innerHTML = response.data[j].物料名称
          document.getElementsByClassName('tableData')[j].children[1].innerHTML = response.data[j].物料规格
          document.getElementsByClassName('tableData')[j].children[2].innerHTML = response.data[j].物料型号
          document.getElementsByClassName('tableData')[j].children[3].innerHTML = response.data[j].发货天数
          document.getElementsByClassName('tableData')[j].children[4].innerHTML = response.data[j].数量
          document.getElementsByClassName('tableData')[j].children[5].innerHTML = response.data[j].单价
          document.getElementsByClassName('tableData')[j].children[6].innerHTML = response.data[j].金额
        }
      })
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .el-date-editor{
    width:200px
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .searchForm .el-form-item{
    margin-bottom: 5px;
  }
</style>
<style>
  .tableData{
    height: 30px;
  }
</style>
