<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" style="margin:0px 10px" clearable @change="machineChange()">
          <el-option
            v-for="item in machineNumber"
            :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>
        <el-select v-model="groupNumberValue" filterable placeholder="组号" style="margin:0px 10px;width: 100px" size="small" clearable>
          <el-option
            v-for="item in groupNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-select v-model="TestValue" filterable placeholder="组件名称" style="margin:0px 10px;width: 150px;" size="small" clearable>
          <el-option
            v-for="item in Test"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-select v-model="qualityInspectionValue" filterable placeholder="质检情况" style="margin:0px 10px;width: 100px;" size="small" clearable>
          <el-option
            v-for="item in qualityInspection"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-button type="primary" size="small" icon="el-icon-search" plain style="margin-left: 15px" @click="pageSize=10; pageCurrent= 1;selecttable()">查询</el-button>
        <el-tooltip :open-delay="1000" effect="dark" content="导出查询数据" placement="top">
          <el-button type="warning" size="small" icon="el-icon-download" plain style="margin-left:10px" @click="exportExcel()">导出</el-button>
        </el-tooltip>
      </el-row>
    </el-card>
    <el-card>
      <el-table :data="tableData1" border size="mini" highlight-current-row stripe style="width: 100%" height="750px">
        <el-table-column align="center" prop="是否合格" label="检验情况" width="80px">
          <template slot-scope="scope">
            <el-tag v-if="parseInt(scope.row.是否合格)===0||scope.row.是否合格===''" type="primary" size="small" @click="approvalPass(scope.row)">未检验</el-tag>
            <el-tag v-if="parseInt(scope.row.是否合格)===1" type="success" size="small">合格</el-tag>
            <el-tag v-if="parseInt(scope.row.是否合格)===2" type="error" size="small">不合格</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="机床图号" prop="机床图号" align="center" width="120px">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column label="机床名称" prop="机床名称" align="center" width="250px">
          <template slot-scope="scope">
            {{ scope.row.机床名称 }}
          </template>
        </el-table-column>
        <el-table-column label="组号" prop="组号" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.组号 }}
          </template>
        </el-table-column>
        <el-table-column label="部件名称" prop="组件名称" align="center" width="150px">
          <template slot-scope="scope">
            {{ scope.row.组件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="检测项" prop="检测项" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.检测项 }}
          </template>
        </el-table-column>
        <el-table-column label="检测项明细" align="center" width="270px">
          <template slot-scope="scope">
            {{ scope.row.检测项明细 }}
          </template>
        </el-table-column>
        <el-table-column label="质检时间" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.质检时间 }}
          </template>
        </el-table-column>
        <el-table-column label="质检员" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
      </el-table>
      <!--      <div class="block">-->
      <!--        <el-pagination-->
      <!--          :current-page="pageCurrent"-->
      <!--          :page-sizes="[10, 20, 30, 40, 100]"-->
      <!--          :page-size="pageSize"-->
      <!--          :total="total"-->
      <!--          layout="total, sizes, prev, pager, next, jumper"-->
      <!--          @size-change="handleSizeChange"-->
      <!--          @current-change="handleCurrentChange"/>-->
      <!--      </div>-->
    </el-card>
  </div>
</template>

<script>import { initProject, searchgroup, selecttable, searchTest, selecttable22 } from '@/api/Assembly/AssemblyQualityInspection'
// import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      machineNumberValue: '',
      machineNumber: [],
      groupNumberValue: '',
      groupNumber: [],
      TestValue: '',
      Test: [],
      qualityInspectionValue: 2,
      qualityInspection: [
        {
          value: 1,
          label: '合格'
        }, {
          value: 2,
          label: '不合格'
        }
      ],
      tableData1: []
      // total: 0, // 总条数
      // pageSize: 10, // 每页显示条数
      // pageCurrent: 1 // 后台获取页
    }
  },
  created() {
  },
  mounted() {
    this.initProject()
    this.fetchData()
  },
  methods: {
    initProject() {
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            name: response.data[i].项目名称,
            value: response.data[i].机床流水号
          })
        }
      })
    },
    fetchData() {
      searchTest().then(response => {
        console.log(response.data)
        for (let i = 0; i < response.data.length; i++) {
          this.Test.push({
            label: response.data[i].检测项,
            value: response.data[i].检测项流水号
          })
        }
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    exportExcel() {
      if (this.machineNumberValue !== '' && this.machineNumberValue !== null) { this.check1 = 1 } else { this.check1 = 0 }
      if (this.groupNumberValue !== '' && this.groupNumberValue !== null) { this.check2 = 1 } else { this.check2 = 0 }
      if (this.TestValue !== '' && this.TestValue !== null) { this.check3 = 1 } else { this.check3 = 0 }
      if (this.qualityInspectionValue !== '' && this.qualityInspectionValue !== null) { this.check4 = 1 } else { this.check4 = 0 }
      selecttable22(this.check1, this.machineNumberValue, this.check2, this.groupNumberValue, this.check3, this.TestValue, this.check4, this.qualityInspectionValue).then(response => {
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['检验情况', '机床图号', '机床名称', '组号', '部件名称', '检测项', '检测项明细', '质检时间', '质检员', '备注']
          const filterVal = ['合格情况', '机床图号', '机床名称', '组号', '组件名称', '检测项', '检测项明细', '质检时间', '姓名', '备注']
          const list = response.data
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '装配质检情况表',
            autoWidth: true,
            bookType: 'xlsx'
          })
        })
      })
    },
    machineChange() {
      this.groupNumberValue = ''
      this.groupNumber = []
      searchgroup(this.machineNumberValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.groupNumber.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
    },
    selecttable() {
      this.tableData1 = []
      if (this.machineNumberValue !== '' && this.machineNumberValue !== null) { this.check1 = 1 } else { this.check1 = 0 }
      if (this.groupNumberValue !== '' && this.groupNumberValue !== null) { this.check2 = 1 } else { this.check2 = 0 }
      if (this.TestValue !== '' && this.TestValue !== null) { this.check3 = 1 } else { this.check3 = 0 }
      if (this.qualityInspectionValue !== '' && this.qualityInspectionValue !== null) { this.check4 = 1 } else { this.check4 = 0 }
      selecttable(this.check1, this.machineNumberValue, this.check2, this.groupNumberValue, this.check3, this.TestValue, this.check4, this.qualityInspectionValue).then(response => {
        this.tableData1 = response.data
      })
    }
    // 分页
    // handleSizeChange(val) {
    //   this.pageCurrent = 1
    //   this.pageSize = val
    //   this.selecttable()
    // },
    // handleCurrentChange(val) {
    //   this.pageCurrent = val
    //   this.selecttable()
    // }
  }
}

</script>

<style scoped>

</style>
