<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;">
      <div>
        <div class="filter">
          <label >{{$t('1950491')}}：</label>
          <Cascader :data="datalevel" :value="leveldata" v-model="sectionid" trigger="click" @on-change="handlelevelchange" change-on-select  class="searchSelect"></Cascader>
        </div>
        <div class="filter">
          <label >{{$t('100307')}}：</label>
          <Select v-model="productid" label-in-value @on-change="routechange" clearable filterable  class="searchSelect" >
              <Option v-for="item in products" :value="item.id" :key="item.id">{{ item.id > 0 ? item.name+"【"+ item.code +"】" : '' }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label >{{$t('920165')}}：</label>
          <Select v-model="materialid" label-in-value clearable filterable  class="searchSelect" >
              <Option v-for="item in materials" :value="item.id" :key="item.id">{{ item.id > 0 ? item.name+"【"+ item.pn +"】" + "【" + item.spec + "】" : '' }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label >{{$t('901048')}}：</label>
          <Select v-model="itemid" label-in-value  clearable  class="searchSelect" >
              <Option v-for="item in items" :value="item.id" :key="item.id">{{ item.id > 0 ? item.item : '' }}</Option>
          </Select>
        </div>
        <!-- 次级搜索条件--隐藏 -->
      <div v-show="!morecondition">
        <div>
          <!-- <div class="filter">
          <label >{{$t('500006')}}:</label>
          <Select v-model="routeid" label-in-value  clearable style="width:200px;" >
              <Option v-for="item in routes" :value="item.id" :key="item.id">{{ item.id > 0 ? item.name : '' }}</Option>
          </Select>
        </div> -->

        <div class="filter">
            <label>{{$t('1046')}}：</label>
            <DatePicker
              id="日期"
              type="datetimerange"
              v-model="daterange"
              confirm
              placeholder=""
              class="searchInput MaxWidth  "
              format="yyyy-MM-dd HH:mm:ss"
              @on-change="selectdate"
            ></DatePicker>
        </div>
      </div>
      </div>

      </div>
      <div class="searchBtn">
        <Button type="primary" class="twoWord" icon="ios-search" :loading="loading1" @click="search(1,pagesize)">
          <span v-if="!loading1">{{$t('1001')}}</span>
          <span v-else>{{$t('1001')}}</span>
        </Button>
        <Button type="warning" class="twoWord" icon="ios-create-outline"  @click="edit()">{{$t('1003')}}</Button>
        <Button type="error" class="twoWord intervalRight" icon="md-close-circle"  @click="dele()">{{$t('1004')}}</Button>
        <Button type="primary" class="fourWord" icon="ios-cloud-download" :loading="loading2" @click="downexcel()">
          <span v-if="!loading2">{{$t('1007')}}</span>
          <span v-else>{{$t('1007')}}</span>
        </Button>
      </div>

      <!-- 更多条件 按钮 -->
      <MoreConbtn @getCon="morecondition = !morecondition" :morecondition="morecondition"></MoreConbtn>

    </div>
    <div class="divborder newTableDiv">
      <Table
         class="tableClass"
        width='100%'
        @on-current-change="handleRowChange"
        highlight-row
        :data="tableData1"
        :columns="tableColumns1"
      ></Table>
      <div class="pageDiv">
        <div class="pageDirection">
          <Page
            :total="total"
            :current="page"
            @on-change="changePage"
            show-elevator
            show-sizer
            show-total
            @on-page-size-change="handlePageSize"
            :page-size-opts="[10,20,30,40,100]"
          ></Page>
        </div>
      </div>
    </div>

    <div class="divborder" v-show="detailshow">
      <label>{{$t('1950494') + ':'}}</label>
      <template>
          <Table :columns="columns1" :data="tableData2">
          </Table>
      </template>
    </div>

    <Modal class="newModalClass" :title='title1' v-model="editmodal" :styles="{width:'1200px'}">
      <div class="modal_body" style="margin-right:40px;height:600px!important;">
        <div class="divboder">
          <label style="font-size:14px;font-weight:700;">{{$t('300018')}}</label>
          <div style="width:1200px;float:left;height:20%;padding-left:5px;">
              <div class="filter">
                <label >{{$t('1950491')}}：</label>
                <Input class="searchInput" type="text" id="input1" v-model="editobj.sectionname" disabled="disabled" />
              </div>
              <div class="filter">
                <label >{{$t('100307')}}：</label>
                <Input class="searchInput" type="text" id="input1" v-model="editobj.product" disabled="disabled" />
              </div>
              <div class="filter">
                <label >{{$t('920165')}}：</label>
                <Input class="searchInput" type="text" id="input1" v-model="editobj.material" disabled="disabled" />
              </div>
              <div class="filter">
                <label >{{$t('1950496')}}：</label>
                <Input class="searchInput" type="text" id="input1" v-model="editobj.checktype_value" disabled="disabled" />
              </div>
              <div class="filter">
                <label >{{$t('950232')}}：</label>
                <Input class="searchInput" type="text" id="input1" v-model="editobj.routename" disabled="disabled" />
              </div>
              <div class="filter">
                <label >{{$t('800008')}}：</label>
                <Input class="searchInput" type="text" id="input1" v-model="editobj.sn" disabled="disabled" />
              </div>
              <div class="filter">
                <label >{{$t('800195')}}：</label>
                <Input class="searchInput" type="text" id="input1" v-model="editobj.result_value" disabled="disabled" />
              </div>
          </div>
        </div>
        <div class="divboder">
          <label style="font-size:14px;font-weight:700;">{{$t('1950508')}}</label>
          <div :class='table_scrollx?"datasearch_table":""' >
              <template>
                  <Table :columns="editcolumns" :data="edittableData" ref="detailTable" :max-height="300">
                      <template slot-scope="{ row, index }" slot="xuhao">
                          <span>{{ row.xuhao }}</span>
                      </template>
                      <template slot-scope="{ row, index }" slot="item">
                          <span>{{ row.item }}</span>
                      </template>
                      <template slot-scope="{ row, index }" slot="standard">
                          <span>{{ row.standard }}</span>
                      </template>
                      <template slot-scope="{ row, index }" slot="min_value">
                          <span>{{ row.min_value }}</span>
                      </template>
                      <template slot-scope="{ row, index }" slot="max_value">
                          <span>{{ row.max_value }}</span>
                      </template>
                      <template slot-scope="{ row, index }" slot="range">
                          <span>{{ row.range }}</span>
                      </template>

                      <template v-for="(item, i) in editobj.number" slot-scope="{ row, index }" :slot="'min_value'+(i+1)">
                        <span v-show="edittableData[index].checktype === 2 && edittableData[index].valuecategory === 2">
                          <inputNumber class="publicinput" :class="{active_class:((edittableData[index]['min_value'+(i+1)])?(edittableData[index]['min_value'+(i+1)]<row.min_value || edittableData[index]['min_value'+(i+1)]>row.max_value):false)}"  type="text" v-model="edittableData[index]['min_value'+(i+1)]" style="width:100px" @blur.native.capture="getrange(index, i+1)"/>
                        </span>
                      </template>
                      <template v-for="(item, i) in editobj.number" slot-scope="{ row, index }" :slot="'max_value'+(i+1)">
                        <span v-show="edittableData[index].checktype === 2 && edittableData[index].valuecategory === 2">
                          <inputNumber class="publicinput" type="text" v-model="edittableData[index]['max_value'+(i+1)]" :class="{active_class:((edittableData[index]['max_value'+(i+1)])?(edittableData[index]['max_value'+(i+1)]<row.min_value || edittableData[index]['max_value'+(i+1)]>row.max_value):false)}"  style="width:100px" @blur.native.capture="getrange(index, i+1)"/>
                        </span>
                      </template>
                      <template v-for="(item, i) in editobj.number" slot-scope="{ row, index }" :slot="'value'+(i+1)">
                        <span v-if="edittableData[index].checktype === 1">
                          <RadioGroup v-model="edittableData[index]['value'+(i+1)]">
                            <Radio label="OK" style="width:30px">OK</Radio>
                            <Radio label="NG" style="width:30px">NG</Radio>
                          </RadioGroup>
                        </span>
                        <span v-else-if="edittableData[index].checktype === 2">
                          <inputNumber :disabled="edittableData[index].checktype === 2 && edittableData[index].valuecategory === 2" :class="{active_class:((edittableData[index]['value'+(i+1)])?(edittableData[index]['value'+(i+1)]<row.min_value || edittableData[index]['value'+(i+1)]>row.max_value):false)}" class="publicinput" type="text" v-model="edittableData[index]['value'+(i+1)]" style="width:100px;"/>
                        </span>
                        <span v-else-if="edittableData[index].checktype === 0">
                          <input class="publicinput" type="text" v-model="edittableData[index]['value'+(i+1)]" style="width:100px"/>
                        </span>
                      </template>
                      <template v-for="(item, i) in editobj.number" slot-scope="{ row, index }" :slot="'range'+(i+1)">
                        <span v-show="edittableData[index].checktype === 2 && edittableData[index].valuecategory === 2">
                          <inputNumber :disabled="true" class="publicinput" type="text" v-model="edittableData[index]['range'+(i+1)]" :class="{active_class:((edittableData[index]['range'+(i+1)]) ? edittableData[index]['range'+(i+1)] > edittableData[index]['range'] : false)}" style="width:100px;font-color:black;"/>
                        </span>
                      </template>
                  </Table>
              </template>
          </div>
        </div>
      </div>
      <div slot="footer">
        <Button type="text" size="large" @click="cancle">{{$t('1033')}}</Button>
        <Button type="primary" :loading="loading3" @click="savedata">
            <span v-if="!loading3">{{$t('1003')}}</span>
            <span v-else>Loading...</span>
        </Button>
      </div>
    </Modal>

    <div>
      <Modal class="newModalClass" :title="$t('1950601')" v-model="ngmodal" :styles="{width:'900px'}">
        <div class="modal_body" style="padding-right:40px;height:400px!important;">
          <div class="divboder">
            <div style="height:100%;padding-left:5px;overflow-y:scroll">
                <Table class="echartsTable" :columns="ngcolumns" :data="ngtableData"></Table>
            </div>
          </div>
        </div>
        <div slot="footer">
          <template>
            <Poptip placement="left" width="300px">
              <Button type="primary" :loading="loading4">
                <span v-if="!loading4">{{$t('1950549')}}</span>
                <span v-else>{{$t('1950549')}}</span>
              </Button>
              <div class="api" slot="content">
                <div class="filter">
                  <label><i>*</i>{{$t('1950626')}}：</label>
                  <InputNumber class="searchInput" v-model="quantity" />
                </div>
                <Button type="primary" :loading="loading4" @click="saveresult1">
                  <span v-if="!loading4">{{$t('900306')}}</span>
                  <span v-else>{{$t('900306')}}</span>
                </Button>
              </div>
            </Poptip>
          </template>
          <!--<Button type="primary" :loading="loading4" @click="saveresult1">
            <span v-if="!loading4">{{$t('1950549')}}</span>
            <span v-else>{{$t('1950549')}}</span>
          </Button>-->
          <Button type="primary" :loading="loading5" @click="saveresult2" style="margin-left:10px;">
            <span v-if="!loading5">{{"NG"}}</span>
            <span v-else>{{"NG"}}</span>
          </Button>
        </div>
      </Modal>
    </div>
  </div>
</template>

<script>
import axios from '@/libs/api.request'
import moment from 'moment'

export default{
    data () {
        return {
            headers: {
                token: localStorage.getItem('token'),
                timestamp: localStorage.getItem('timestamp'),
                nonce: localStorage.getItem('nonce'),
                sign: localStorage.getItem('sign')
            },
            table_scrollx: false,
            morecondition: false, // 条件
            page: 1,
            pagesize: 10,
            total: 0,
            tableData1: [],
            tableData2: [],
            columns1: [
                {
                    type: 'index',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    width: 90
                },
                {
                    title: this.$t('901048'),
                    key: 'item'
                },
                {
                    title: this.$t('950245'),
                    key: 'standard'
                },
                {
                    title: this.$t('200060'),
                    key: 'min_value'
                },
                {
                    title: this.$t('200059'),
                    key: 'max_value'
                },
                {
                    title: this.$t('1950621'),
                    key: 'item_range'
                },
                {
                    title: this.$t('800195'),
                    key: 'result'
                },
                {
                    title: this.$t('8000030'), // 实际最小值
                    key: 'lowerlimit'
                },
                {
                    title: this.$t('8000031'), // 实际最大值
                    key: 'upperlimit'
                },
                {
                    title: this.$t('1950610'), // 单值
                    key: 'value'
                },
                {
                    title: this.$t('1950620'), // 极差
                    key: 'range'
                },
                {
                    title: this.$t('100044'),
                    key: 'description'
                }
            ],
            tableColumns1: [
                {
                    title: this.$t('1008'),
                    key: 'xuhao',
                    slot: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        )
                    },
                    width: 90
                },
                {
                    title: this.$t('100089'),
                    key: 'productcode'
                },
                {
                    title: this.$t('100088'),
                    key: 'productname'
                },
                {
                    title: this.$t('1950491'),
                    key: 'sectionname'
                },
                {
                    title: this.$t('900053'),
                    key: 'workorder'
                },
                {
                    title: this.$t('950232'),
                    key: 'routename'
                },
                {
                    title: this.$t('800008'),
                    key: 'sn'
                },
                {
                    title: this.$t('1950627'),
                    key: 'quantity'
                },
                {
                    title: this.$t('950228'),
                    key: 'reason'
                },
                {
                    title: this.$t('800195'),
                    key: 'result_value'
                },
                {
                    title: this.$t('1950492'),
                    key: 'check_user'
                },
                {
                    title: this.$t('1950493'),
                    key: 'check_time'
                }
            ],
            products: [],
            dtStart: '',
            dtEnd: '',
            daterange: '',
            routeid: 0,
            itemid: 0,
            items: [],
            routes: [],
            host: axios.publicPath,
            materials: [],
            leveldata: [],
            datalevel: [],
            sectionid: [],
            materialid: 0,
            productid: 0,
            title1: this.$t('1003'),
            editmodal: false,
            editobj: {},
            edittableData: [],
            editcolumns: [
                {
                    title: this.$t('1008'),
                    type: 'index',
                    slot: 'xuhao',
                    width: 70,
                    align: 'center'
                },
                {
                    title: this.$t('901048'),
                    slot: 'item',
                    align: 'center'
                },
                {
                    title: this.$t('950245'),
                    slot: 'standard',
                    align: 'center'
                },
                {
                    title: this.$t('200060'),
                    slot: 'min_value',
                    align: 'center'
                },
                {
                    title: this.$t('200059'),
                    slot: 'max_value',
                    align: 'center'
                },
                {
                    title: this.$t('920168'),
                    slot: 'result_value',
                    align: 'center'
                }
            ],
            loading1: false,
            loading2: false,
            loading3: false,
            loading4: false,
            loading5: false,
            ngcolumns: [
                {
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + 1
                        )
                    },
                    width: 70,
                    align: 'center'
                },
                {
                    title: this.$t('901048'),
                    key: 'item',
                    align: 'center'
                },
                {
                    title: this.$t('950245'),
                    key: 'standard',
                    align: 'center'
                },
                {
                    title: this.$t('200060'),
                    key: 'min_value',
                    align: 'center'
                },
                {
                    title: this.$t('200059'),
                    key: 'max_value',
                    align: 'center'
                },
                {
                    title: this.$t('1950619'),
                    key: 'range'
                },
                {
                    title: this.$t('800195'),
                    key: 'result',
                    align: 'center'
                },
                {
                    title: this.$t('8000030'),
                    key: 'lowerlimit'
                },
                {
                    title: this.$t('8000031'),
                    key: 'upperlimit'
                },
                {
                    title: this.$t('1950610'),
                    key: 'value',
                    align: 'center'
                },
                {
                    title: this.$t('1950620'),
                    key: 'actual_range'
                }
            ],
            ngtableData: [],
            ngmodal: false,
            quantity: 1,
            checktypeList: [],
        }
    },
    created () {
        var nowtime = Date.parse(new Date()) / 1000
        var oldtime = nowtime - 86400 * 7
        this.daterange = [this.datetrans(oldtime) + '00:00:00', this.datetrans(nowtime) + '23:59:59']
        this.selectdate(this.daterange)
        this.initlevel()
        this.search(1, 10)
        this.initchecktypeList()
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        initchecktypeList () {
        axios.request({
            url: '/qms/TypeCount/GetTypeCounts?page=1&pagesize=10000',
            method: 'get'
        }).then(res => {
                res.data.data.forEach(ele => {
                    this.checktypeList.push({id:ele.type_code, name:ele.type_name})
                });
            })      
        },
        getrange (index, i) {
            var maxValue = this.edittableData[index]['max_value' + i]
            var minValue = this.edittableData[index]['min_value' + i]
            if (maxValue !== '' && maxValue !== null && maxValue !== undefined && minValue !== '' && minValue !== null && minValue !== undefined) {
                this.edittableData[index]['range' + i] = Math.round((this.edittableData[index]['max_value' + i] - this.edittableData[index]['min_value' + i]) * 10000) / 10000
                this.edittableData[index]['value' + i] = Math.round((this.edittableData[index]['max_value' + i] + this.edittableData[index]['min_value' + i]) / 2 * 10000) / 10000
            } else {
                this.edittableData[index]['range' + i] = ''
            }
        },
        savedata () {
            this.editobj.details = this.edittableData
            this.editobj.result = '1'
            var ngList = []
            var isMaxMin = false
            if (this.edittableData !== undefined && this.edittableData !== null && this.edittableData !== []) {
                var details = this.edittableData
                var spcCheck = false;
                let chectData = [];
                for (var i = 1; i <= this.editobj.number; i++) {
                    details.forEach((ele, index) => {
                        var detail = {
                            index: i,
                            itemid: ele.itemid,
                            checktype: ele.checktype
                        }
                        if (ele.checktype === 2) {
                            detail.value = ele['value' + i]
                            detail.lowerlimit = ele['min_value' + i]
                            detail.upperlimit = ele['max_value' + i]
                            detail.result = ''
                            if (ele.valuecategory === 1) {
                                if ((this.obj.checktype === 6 || this.obj.checktype === '6') && (detail.value === null || detail.value === undefined || detail.value === '')) {
                                    spcCheck = true
                                }
                                if ((detail.value > ele.max_value && ele.max_value !== '') || (detail.value < ele.min_value && ele.min_value !== '')) {
                                    if (detail.value !== null && detail.value !== undefined && detail.value !== '') {
                                        ngList.push({
                                            item: ele.item,
                                            standard: ele.standard,
                                            checktype: ele.checktype,
                                            min_value: ele.min_value,
                                            max_value: ele.max_value,
                                            result: 'NG',
                                            value: detail.value,
                                            upperlimit: '',
                                            lowerlimit: '',
                                            range: '',
                                            actual_range: ''
                                        })
                                    }
                                }
                            } else if (ele.valuecategory === 2) {
                                isMaxMin = true
                                if ((this.obj.checktype === 6 || this.obj.checktype === '6') &&
                  (detail.value === null || detail.value === undefined || detail.value === '' ||
                  detail.upperlimit === null || detail.upperlimit === undefined || detail.upperlimit === '' ||
                  detail.lowerlimit === null || detail.lowerlimit === undefined || detail.lowerlimit === '')) {
                                    spcCheck = true
                                }
                                if ((detail.upperlimit > ele.max_value || detail.lowerlimit < ele.min_value || (detail.upperlimit - detail.lowerlimit) > ele.range ||
                  detail.upperlimit < ele.min_value || detail.lowerlimit > ele.max_value ||
                  (detail.value > ele.max_value && ele.max_value !== '') || (detail.value < ele.min_value && ele.min_value !== '')) &&
                  detail.upperlimit !== null && detail.upperlimit !== undefined && detail.upperlimit !== '' &&
                  detail.lowerlimit !== null && detail.lowerlimit !== undefined && detail.lowerlimit !== '') {
                                    ngList.push({
                                        item: ele.item,
                                        standard: ele.standard,
                                        checktype: ele.checktype,
                                        min_value: ele.min_value,
                                        max_value: ele.max_value,
                                        result: 'NG',
                                        value: detail.value,
                                        range: ele.range,
                                        upperlimit: detail.upperlimit,
                                        lowerlimit: detail.lowerlimit,
                                        range: ele.range,
                                        actual_range: ele['range' + i]
                                    })
                                }
                            }
                        } else if (ele.checktype === 1) {
                            detail.value = null
                            detail.result = ele['value' + i]
                            if (detail.result !== null && detail.result !== undefined && detail.result !== '') {
                                if (detail.result.toUpperCase() === 'NG') {
                                    ngList.push({
                                        item: ele.item,
                                        standard: ele.standard,
                                        checktype: ele.checktype,
                                        min_value: ele.min_value,
                                        max_value: ele.max_value,
                                        result: 'NG',
                                        value: detail.value,
                                        upperlimit: '',
                                        lowerlimit: '',
                                        range: '',
                                        actual_range: ''
                                    })
                                }
                            }
                        }
                        chectData.push(detail)
                    })
                }
                if (spcCheck) {
                    this.$Message.error((this.$t(1950628)))
                    return
                }
                this.editobj.details = chectData
            }
            if (ngList.length > 0) {
                if (isMaxMin) {
                    this.ngcolumns = [
                        {
                            title: this.$t('1008'),
                            key: 'xuhao',
                            render: (h, params) => {
                                return h(
                                    'span',
                                    params.index + 1
                                )
                            },
                            width: 70,
                            align: 'center'
                        },
                        {
                            title: this.$t('901048'),
                            key: 'item',
                            align: 'center'
                        },
                        {
                            title: this.$t('950245'),
                            key: 'standard',
                            align: 'center'
                        },
                        {
                            title: this.$t('200060'),
                            key: 'min_value',
                            align: 'center'
                        },
                        {
                            title: this.$t('200059'),
                            key: 'max_value',
                            align: 'center'
                        },
                        {
                            title: this.$t('1950619'),
                            key: 'range'
                        },
                        {
                            title: this.$t('800195'),
                            key: 'result',
                            align: 'center'
                        },
                        {
                            title: this.$t('8000030'),
                            key: 'lowerlimit'
                        },
                        {
                            title: this.$t('8000031'),
                            key: 'upperlimit'
                        },
                        {
                            title: this.$t('1950610'),
                            key: 'value',
                            align: 'center'
                        },
                        {
                            title: this.$t('1950620'),
                            key: 'actual_range'
                        }
                    ]
                } else {
                    this.ngcolumns = [
                        {
                            title: this.$t('1008'),
                            key: 'xuhao',
                            render: (h, params) => {
                                return h(
                                    'span',
                                    params.index + 1
                                )
                            },
                            width: 70,
                            align: 'center'
                        },
                        {
                            title: this.$t('901048'),
                            key: 'item',
                            align: 'center'
                        },
                        {
                            title: this.$t('950245'),
                            key: 'standard',
                            align: 'center'
                        },
                        {
                            title: this.$t('200060'),
                            key: 'min_value',
                            align: 'center'
                        },
                        {
                            title: this.$t('200059'),
                            key: 'max_value',
                            align: 'center'
                        },
                        {
                            title: this.$t('1950610'),
                            key: 'value',
                            align: 'center'
                        },
                        {
                            title: this.$t('800195'),
                            key: 'result',
                            align: 'center'
                        }
                    ]
                }
                this.showNg(ngList)
                return
            }
            this.saveresult()
        },
        saveresult () {
            var data = this.editobj
            data.quantity = this.quantity
            this.loading3 = true
            this.loading4 = true
            this.loading5 = true
            axios.request({
                url: '/qms/ManualCollection/Update',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.$Message.success(this.$t('800223'))
                    this.ngmodal = false
                    this.editobj = {};
                    this.edittableData = [];
                    this.editmodal = false;
                    this.search(this.page, this.pagesize)
                    this.quantity = 1;
                } else {
                    this.quantity = 1;
                    this.$Message.error(this.$t(res.data.msg))
                }
                this.loading3 = false
                this.loading4 = false
                this.loading5 = false
            }).catch(() => {
                this.editobj = {}
                this.loading3 = false
                this.loading4 = false
                this.loading5 = false
            })
        },
        saveresult1 () {
            this.editobj.result = '2'
            this.saveresult()
        },
        saveresult2 () {
            this.editobj.result = '3'
            this.saveresult()
        },
        showNg (ngList) {
            this.ngmodal = true
            this.ngtableData = []
            ngList.forEach(item => {
                this.ngtableData.push({
                    item: item.item,
                    standard: item.standard,
                    min_value: item.checktype === 2 ? (item.min_value === -999999999 ? '' : item.min_value) : '',
                    max_value: item.checktype === 2 ? (item.max_value === 999999999 ? '' : item.max_value) : '',
                    result: 'NG',
                    value: item.value,
                    upperlimit: item.upperlimit,
                    lowerlimit: item.lowerlimit,
                    range: item.range,
                    actual_range: item.actual_range
                })
            })
        },
        cancle () {
            this.editobj = {};
            this.edittableData = [];
            this.editmodal = false;
            this.quantity = 1;
        },
        edit () {
            this.quantity = 1;
            this.editcolumns = [
                {
                    type: 'index',
                    title: this.$t('1008'),
                    slot: 'xuhao',
                    width: 70,
                    fixed: 'left'
                },
                {
                    title: this.$t('901048'),
                    slot: 'item',
                    minWidth: 120,
                    fixed: 'left'
                },
                {
                    title: this.$t('950245'),
                    slot: 'standard',
                    minWidth: 120,
                    fixed: 'left'
                },
                {
                    title: this.$t('500082'),
                    slot: 'min_value',
                    minWidth: 80,
                    fixed: 'left'
                },
                {
                    title: this.$t('500081'),
                    slot: 'max_value',
                    minWidth: 80,
                    fixed: 'left'
                },
                {
                    title: this.$t('8000032'),
                    slot: 'range',
                    minWidth: 70,
                    fixed: 'left'
                }
            ]
            this.edittableData = []
            this.editobj = {}
            if (this.obj.id === undefined || this.obj.id === null || this.obj.id === 0) {
                this.$Message.error((this.$t('1017')))
                return false
            } else {
                axios
                    .request({
                        url: '/qms/ManualCollection/GetUpdateDetails?id=' + this.obj.id,
                        method: 'get'
                    })
                    .then(res => {
                        this.editobj = this.obj;
                        var details = res.data.data.details
                        var tablelist = res.data.data.itemdetails

                        var isMaxMin = false
                        tablelist.forEach(element => {
                            if (element.checktype === 2 && element.valuecategory === 2) {
                                isMaxMin = true
                            }
                        });

                        var cloumnLength = res.data.data.number
                        this.quantity = this.obj.quantity >= 1 ? this.obj.quantity : 1
                        this.editobj.number = cloumnLength
                        this.editmodal = true;
                        this.editobj.material = (this.editobj.materialname === '' || this.editobj.materialname === undefined || this.editobj.materialname === null) ? ''
                            : (this.editobj.materialname + '【' + this.editobj.materialpn + '】');
                        this.editobj.product = (this.editobj.productname === '' || this.editobj.productname === undefined || this.editobj.productname === null) ? ''
                            : (this.editobj.productname + '【' + this.editobj.productcode + '】');

                        for (var i = 1; i <= cloumnLength; i++) {
                            this.editcolumns.push({
                                title: 'NO.' + i,
                                key: 'NO.' + i,
                                slot: 'NO.' + i,
                                align: 'center',
                                children: isMaxMin ? [
                                    {
                                        title: this.$t('500082'), // 最小值
                                        key: 'min_value' + i,
                                        slot: 'min_value' + i,
                                        align: 'center',
                                        minWidth: 120
                                    },
                                    {
                                        title: this.$t('500081'), // 最大值
                                        key: 'max_value' + i,
                                        slot: 'max_value' + i,
                                        align: 'center',
                                        minWidth: 120
                                    },
                                    {
                                        title: this.$t('1950610'), // 单值
                                        key: 'value' + i,
                                        slot: 'value' + i,
                                        align: 'center',
                                        minWidth: 120
                                    },
                                    {
                                        title: this.$t('8000032'), // 极差
                                        key: 'range' + i,
                                        slot: 'range' + i,
                                        align: 'center',
                                        minWidth: 120
                                    }
                                ] : [
                                    {
                                        title: this.$t('1950610'), // 单值
                                        key: 'value' + i,
                                        slot: 'value' + i,
                                        align: 'center',
                                        minWidth: 120
                                    }
                                ]
                            })
                        }
                        tablelist.forEach((item, index) => {
                            if (item) {
                                let data = {
                                    itemid: item.itemid,
                                    item: item.item,
                                    standard: item.standard,
                                    min_value: item.checktype === 2 ? (item.min_value === -999999999 ? '' : item.min_value) : '',
                                    max_value: item.checktype === 2 ? (item.max_value === 999999999 ? '' : item.max_value) : '',
                                    checktype: item.checktype,
                                    result: item.result,
                                    value: item.value,
                                    valuecategory: item.valuecategory,
                                    range: (item.checktype === 2 && item.valuecategory === 2) ? item.range : ''
                                }
                                if (item.checktype === 2) {
                                    for (var i = 1; i <= cloumnLength; i++) {
                                        data['value' + i] = null
                                        data['max_value' + i] = null
                                        data['min_value' + i] = null
                                        data['range' + i] = null
                                        details.forEach(detail => {
                                            if (detail.index === i && detail.itemid === item.itemid) {
                                                data['value' + i] = detail.value
                                                data['max_value' + i] = detail.upperlimit
                                                data['min_value' + i] = detail.lowerlimit
                                                data['range' + i] = (item.checktype === 2 && item.valuecategory === 2) ? detail.range : null
                                            }
                                        })
                                    }
                                } else {
                                    for (var i = 1; i <= cloumnLength; i++) {
                                        data['value' + i] = 'OK'
                                        details.forEach(detail => {
                                            if (detail.index === i && detail.itemid === item.itemid) {
                                                data['value' + i] = detail.result
                                            }
                                        })
                                    }
                                }
                                this.edittableData.push(data)
                            }
                        })
                        // 处理滚动条样式bug
                        setTimeout(() => {
                            var scrollx = this.$refs.detailTable.$refs.body.classList.contains('ivu-table-overflowX'); // 100px
                            if (scrollx) {
                                this.table_scrollx = true
                            } else {
                                this.table_scrollx = false
                            }
                        }, 500)
                    })
            }
        },
        dele () {
            if (this.obj.id === undefined || this.obj.id === null || this.obj.id === 0) {
                this.$Message.error((this.$t('1017')))
                return false
            } else {
                this.$Modal.confirm({
                    title: this.$t('1018'),
                    content: '',
                    width: '290px',
                    closable: true,
                    okText: this.$t('1004'),
                    onOk: () => {
                        axios.request({
                            url: '/qms/ManualCollection/Delete?id=' + this.obj.id,
                            method: 'get'
                        }).then(res => {
                            if (res.data.ret === 1) {
                                this.page = 1
                                let data = []
                                this.tableData1.forEach(ele => {
                                    if (ele.id !== this.obj.id) {
                                        data.push(ele)
                                    }
                                })
                                this.tableData1 = data;
                                this.search(this.page, this.pagesize)
                                this.$Message.success(this.$t('1058'))
                            } else {
                                this.$Message.error(this.$t(res.data.msg))
                            }
                        })
                    }
                })
            }
        },
        handlelevelchange (value) {
            this.leveldata = value
            var sectionid = value[value.length - 1]
            this.initproducts(sectionid)
        },
        initlevel () {
            axios.request({
                url: '/qms/InspectStandard/GetSectionTree',
                method: 'get'
            }).then(res => {
                const arr = []
                this.datalevel = res.data.data
                if(Array.isArray(res.data.data)&&res.data.data.length){
                    arr.push(res.data.data[0].value)
                }
                this.leveldata = arr
            })
        },
        initmaterials () {
            this.materials = [{ id: 0, name: '', pn: '', spec: '' }]
            axios.request({
                url: '/config/Material/GetSonMaterialsByProduct?productid=' + this.productid,
                method: 'get'
            }).then(res => {
                this.materials = res.data.data
            })
        },
        inititems () {
            this.items = [{ id: 0, item: '' }]
            var sectionid = this.sectionid[this.sectionid.length - 1]
            axios.request({
                url: '/qms/InspectStandard/GetItems?productid=' + this.productid + '&sectionid=' + sectionid,
                method: 'get'
            }).then(res => {
                this.items = res.data.data
            })
        },
        routechange () {
            this.routes = [{ id: 0, name: '' }]
            var sectionid = this.sectionid[this.sectionid.length - 1]
            axios.request({
                url: '/qms/ManualCollection/GetSectionRoutes?productid=' + this.productid + '&sectionid=' + sectionid,
                method: 'get'
            }).then(res => {
                this.routes = res.data.data
            })
            this.inititems()
            this.initmaterials()
        },
        datetrans (date) {
            if (date === '' || date === undefined || date === 0 || date === null) {
                return ''
            }
            date = new Date(date * 1000)
            var Y = date.getFullYear() + '-'
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
            var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' '
            return Y + M + D
        },
        searchdetail () {
            this.tableData2 = []
            if (this.obj.id === undefined || this.obj.id === null || this.obj.id === 0) {
                return false
            } else {
                axios
                    .request({
                        url: '/qms/ManualCollection/GetDetailList?id=' + this.obj.id,
                        method: 'get'
                    })
                    .then(res => {
                        this.mockTableData2(res.data.data)
                    })
            }
        },
        mockTableData2 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        id: item.id,
                        itemid: item.itemid,
                        checktype: item.checktype,
                        item: item.item,
                        standard: item.standard,
                        min_value: item.checktype === 2 ? (item.min_value === -999999999 ? '' : item.min_value) : '',
                        max_value: item.checktype === 2 ? (item.max_value === 999999999 ? '' : item.max_value) : '',
                        result: item.result,
                        value: item.value,
                        description: item.description,
                        lowerlimit: (item.checktype === 2 && item.valuecategory === 2) ? item.lowerlimit : '',
                        upperlimit: (item.checktype === 2 && item.valuecategory === 2) ? item.upperlimit : '',
                        range: (item.checktype === 2 && item.valuecategory === 2) ? item.range : '',
                        item_range: (item.checktype === 2 && item.valuecategory === 2) ? item.item_range : ''
                    })
                }
            })
            this.tableData2 = data
        },
        search (page, pagesize) {
            var params = {
                page: page,
                pagesize: pagesize,
                productid: this.productid,
                sectionid: this.sectionid[this.sectionid.length - 1],
                starttime: this.dtStart,
                endtime: this.dtEnd,
                itemid: this.itemid,
                route: this.routeid,
                materialid: this.materialid,
                usercheck: 1,
                toexcel: 0,
                inspectiontype: 2
            }
            this.loading1 = true
            axios
                .request({
                    url: '/qms/ManualCollection/GetManualPageList',
                    params,
                    method: 'get'
                })
                .then(res => {
                    this.loading1 = false
                    this.page = page
                    this.pagesize = pagesize
                    this.total = res.data.total
                    this.mockTableData1(res.data.data)
                }).catch(() => {
                    this.loading1 = false
                })
            this.obj = {}
            this.tableData2 = []
            this.detailshow = false
        },
        selectdate (e) {
            this.dtStart = e[0]
            this.dtEnd = e[1]
            let last_str = this.dtEnd.split(' ')[1]
            if (last_str === '00:00:00') {
                this.dtEnd = moment(this.dtEnd).format('YYYY-MM-DD') + ' 23:59:59'
                this.daterange = [this.dtStart, this.dtEnd]
            }
        },
        initproducts (sectionid) {
            this.products = [{ id: 0, name: '', code: '' }]
            axios.request({
                url: '/qms/InspectStandard/GetProductsBySectionId?reverselevel=2&sectionid=' + sectionid,
                method: 'get'
            }).then(res => {
                this.products = res.data.data
            })
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    var checktype_value = ''
                    switch (item.checktype) {
                    case 1:
                        checktype_value = this.$t(950367)
                        break
                    case 2:
                        checktype_value = '抽检'
                        break
                    case 3:
                        checktype_value = '末件'
                        break
                    case 4:
                        checktype_value = '二级件校对'
                        break
                    case 5:
                        checktype_value = '首件'
                        break
                    case 6:
                        checktype_value = 'SPC检查'
                        break
                    case 7:
                        checktype_value = '其他'
                        break
                    }
                    data.push({
                        id: item.id,
                        productid: item.productid,
                        productcode: item.productcode,
                        productname: item.productname,
                        sectionid: item.sectionid,
                        sectionname: item.sectionname,
                        workorder: item.workorder,
                        sn: item.sn,
                        result: item.result,
                        result_value: item.result === '1' ? 'OK' : item.result === '2' ? this.$t('1950549') : (item.result === '3' ? 'NG' : ''),
                        check_user: item.check_user,
                        check_time: item.check_time,
                        routeid: item.routeid,
                        routename: item.routename,
                        checktype: item.checktype,
                        checktype_value: checktype_value,
                        materialid: item.materialid,
                        materialname: item.materialname,
                        materialpn: item.materialpn,
                        quantity: item.result === '2' ? item.quantity : '',
                        reason: item.reason
                    })
                }
            })
            this.tableData1 = data
        },
        downexcel () {
            var params = {
                productid: this.productid,
                sectionid: this.sectionid[this.sectionid.length - 1],
                starttime: this.dtStart,
                endtime: this.dtEnd,
                itemid: this.itemid,
                route: this.routeid,
                materialid: this.materialid,
                usercheck: 1,
                toexcel: 1,
                inspectiontype: 2
            }
            this.loading1 = true
            axios.request({
                url: '/qms/ManualCollection/GetManualPageList',
                params,
                method: 'get'
            }).then(res => {
                this.loading1 = false
                if (res.data.ret === 1) {
                    window.location.href = axios.publicPath + '' + res.data.data
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            }).catch(() => {
                this.loading1 = false
            })
        },
        changecondition () {
            this.morecondition = !this.morecondition
        },
        handlePageSize (value) {
            this.pagesize = value
            this.tableData1 = this.search(this.page, this.pagesize)
        },
        changePage (value) {
            this.page = value
            this.tableData1 = this.search(this.page, this.pagesize)
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.obj = currentRow
            this.detailshow = true
            this.searchdetail()
        }
    }
}

</script>
<style lang="less" scoped>

.active_class{
  border:red 1px solid;
}
</style>

<style lang="less">
    .datasearch_table{
        .ivu-table-fixed-body{
          height:auto!important;
          max-height: 211px!important;
      }
    }
</style>
