<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-input v-model="supplierName" placeholder="供应商" size="small" clearable/>
      <span>提前</span>
      <el-input-number v-model="warningValue" :min="1" controls-position="right" size="small" style="width: 90px"/>
      <span>天预警</span>
      <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=""
        :data="tableData1"
        border
        highlight-current-row
        style="width: 100%;max-height: 250px;"
        height="250px"
        size="mini"
        @row-click="searchTable2">
        <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="120">
          <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.规定到货时间.split(' ')[0] }}
          </template>
        </el-table-column>
        <el-table-column label="供应商" align="center" min-width="250">
          <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="70">
          <template slot-scope="scope">
            <el-tag v-if="judge[scope.$index]===1" type="success" size="small">全部到货</el-tag>
            <el-tag v-if="judge[scope.$index]===2" type="warning" size="small">即将拖期</el-tag>
            <el-tag v-if="judge[scope.$index]===3" type="danger" size="small">已拖期</el-tag>
            <el-tag v-if="judge[scope.$index]===4" type="primary" size="small">进度正常</el-tag>
          </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="parseFloat((100*scope.row.合同物料到货总数/scope.row.合同物料采购总数).toFixed(2))"
              status="success"/>
          </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-table v-loading="" :data="tableData" stripe border style="width: 100%;max-height: 300px;margin-top: 10px" height="300px" size="mini">
        <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="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="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>
      <el-table v-loading="" :data="tableData2" stripe border style="width: 100%;max-height: 300px;margin-top: 10px" height="300px" size="mini">
        <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="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="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>
    </el-card>
  </div>
</template>

<script>
import { initArrivalStateContact, searchArrivalState, searchArrivalNum, searchArrivalNum1 } from '@/api/WorkshopProcurement/ArrivalStateSearch'
import { mapGetters } from 'vuex'

export default {
  name: '', // 项目总价查询
  data() {
    return {
      warningValue: 3,
      check1: 0,
      check2: 0,
      contractNumValue: '',
      contractNum: [],
      supplierName: '',
      tableData: [],
      tableData1: [],
      pageSize1: 10,
      pageCurrent1: 1,
      total1: 0,
      tableData2: [],
      now: '',
      arrival: [],
      judge: []
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      initArrivalStateContact().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.warningValue ? this.warningValue : this.warningValue = 1
      this.arrival = [] // 规定到货时间数组
      this.now = new Date()
      this.contractNumValue ? this.check1 = 1 : this.check1 = 0
      this.supplierName ? this.check2 = 1 : this.check2 = 0
      searchArrivalState(this.pageCurrent1, this.pageSize1, this.check1, this.contractNumValue, this.check2, this.supplierName).then(response => {
        for (let i = 0; i < response.data.rows.length; i++) {
          this.arrival.push(new Date(response.data.rows[i].规定到货时间))
          if (parseInt(response.data.rows[i].合同物料到货总数) === parseInt(response.data.rows[i].合同物料采购总数)) {
            this.judge[i] = 1 // 全部到货
          } else if (parseInt(response.data.rows[i].合同物料到货总数) !== parseInt(response.data.rows[i].合同物料采购总数) && (this.arrival[i] - this.now) > 0 && (this.arrival[i] - this.now) < (86400000 * this.warningValue)) {
            this.judge[i] = 2 // 即将拖期
          } else if (parseInt(response.data.rows[i].合同物料到货总数) !== parseInt(response.data.rows[i].合同物料采购总数) && (this.arrival[i] - this.now) < 0) {
            this.judge[i] = 3 // 已拖期
          } else if (parseInt(response.data.rows[i].合同物料到货总数) !== parseInt(response.data.rows[i].合同物料采购总数) && (this.arrival[i] - this.now) > (86400000 * this.warningValue)) {
            this.judge[i] = 4 // 进度正常
          }
        }
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    handleSizeChange1(val) {
      this.pageSize1 = val
      this.pageCurrent1 = 1
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    searchTable2(row) {
      searchArrivalNum(row.采购合同流水号).then(response => {
        this.tableData2 = response.data
      })
      searchArrivalNum1(row.采购合同流水号).then(response => {
        this.tableData = response.data
      })
    }
  }
}
</script>

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