<template>
  <div class="testdiv">
    <div class="divborder" style="margin:0px;">
      <h3>{{$t('300018')}}</h3>
      <div>
        <div class="filter">
          <label >{{$t('1950491')}}<i style="color:red;margin-left: 5px!important;margin-top: -5px!important" >*</i>:</label>
          <Cascader :data="datalevel" :value="leveldata" v-model="sectionid" trigger="click" @on-change="handlelevelchange" change-on-select style="display:inline-block;width:200px;"></Cascader>
        </div>
        <div class="filter">
          <label >{{$t('100307')}}<i style="color:red;margin-left: 5px!important;margin-top: -5px!important" >*</i>:</label>
          <Select v-model="editobj.productid" @on-change="editroutechange" label-in-value clearable style="width:200px;" >
              <Option v-for="item in products" :value="item.id" :key="item.id">{{ item.name+"【"+ item.code +"】" }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label >{{$t('920165')}}:</label>
          <Select v-model="editobj.materialid" label-in-value clearable style="width:200px;" >
              <Option v-for="item in materials" :value="item.id" :key="item.id">{{ item.name+"【"+ item.pn +"】" + "【" + item.spec + "】" }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label >{{$t('1950495')}}:</label>
          <Select v-model="editobj.routeid" label-in-value clearable style="width:200px;" >
              <Option v-for="item in editroutes" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label >{{$t('1950496')}}:</label>
          <Select v-model="editobj.checktype" label-in-value clearable style="width:200px;" >
              <Option :value="1" :key="1">{{$t('950367')}}</Option>
              <Option :value="2" :key="2">{{$t('950368')}}</Option>
              <Option :value="3" :key="3">{{$t('1950497')}}</Option>
              <Option :value="4" :key="4">{{$t('1950498')}}</Option>
              <Option :value="5" :key="5">{{$t('1950581')}}</Option>
          </Select>
        </div>
        <div class="filter">
          <label >{{$t('800008')}}:</label>
          <input class="publicinput" type="text" v-model="editobj.sn" style="width:200px;"/>
        </div>
        <div class="filter">
          <label >{{$t('1950499')}}<i style="color:red;margin-left: 5px!important;margin-top: -5px!important" >*</i>:</label>
          <Select v-model="editobj.result" label-in-value clearable style="width:200px;" >
              <Option :value="1" :key="1">{{"OK"}}</Option>
              <Option :value="2" :key="2">{{$t("1950549")}}</Option>
              <Option :value="3" :key="3">{{"NG"}}</Option>
          </Select>
        </div>
      </div>
    </div>
    <div class="divborder">
      <h3>{{$t('1950500')}}</h3>
      <div>
          <div style="width:100%;float:left;height:100%;padding-left:5px;">
              <template style="overflow-x:scroll;overflow-y:hidden;">
                  <Table :columns="columns" :data="tableData" v-show="detailshow" ref="detailTable">
                      <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="result_value">
                        <span v-show="tableData[index].checktype === 2">
                          <inputNumber class="publicinput" type="text" v-model="tableData[index].value" style="width:100px"/>
                        </span>
                        <span v-show="tableData[index].checktype === 1">
                          <input type="radio" value="OK" v-model="tableData[index].result"/>{{"OK"}}
                          <input type="radio" value="NG" v-model="tableData[index].result"/>{{"NG"}}
                        </span>
                      </template>
                  </Table>
              </template>
          </div>
          <div>
            <Button type="primary" :loading="loading" @click="savedata" style="float:right;margin-top:10px">
                <span v-if="!loading">{{$t('900306')}}</span>
                <span v-else>Loading...</span>
            </Button>
              <!--<Button type="primary" style="float:right;margin-top:10px" size="large" @click="savedata">{{$t('900306')}}</Button>-->
          </div>
        </div>
    </div>
  </div>
</template>

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

export default{
    data () {
        return {
            headers: {
                token: localStorage.getItem('token'),
                timestamp: localStorage.getItem('timestamp'),
                nonce: localStorage.getItem('nonce'),
                sign: localStorage.getItem('sign')
            },
            morecondition: false,
            leveldata: [],
            datalevel: [],
            editobj: {},
            products: [],
            materials: [],
            detailshow: true,
            tableData: [],
            columns: [
                {
                    title: this.$t('1008'),
                    slot: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + 1
                        )
                    },
                    width: 70
                },
                {
                    title: this.$t('901048'),
                    slot: 'item'
                },
                {
                    title: this.$t('950245'),
                    slot: 'standard'
                },
                {
                    title: this.$t('200060'),
                    slot: 'min_value'
                },
                {
                    title: this.$t('200059'),
                    slot: 'max_value'
                },
                {
                    title: this.$t('920168'),
                    slot: 'result_value'
                }
            ],
            host: axios.publicPath,
            editroutes: [],
            sectionid: [],
            loading: false
        }
    },
    created () {
        this.initlevel()
    },
    methods: {
        handlelevelchange (value) {
            this.leveldata = value
            var sectionid = value[value.length - 1]
            this.initproducts(sectionid)
        },
        savedata () {
            this.editobj.sectionid = this.sectionid[this.sectionid.length - 1]
            let data = this.editobj
            data.details = this.$refs.detailTable.data
            this.loading = true
            axios.request({
                url: '/qms/ManualCollection/Add',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.$Message.success(this.$t('800223'))
                    this.editobj = {}
                    this.tableData = []
                    this.editobj.productid = data.productid
                    this.editobj.routeid = data.routeid
                    this.editobj.materialid = data.materialid
                    this.editobj.checktype = data.checktype
                    this.inititems()
                    this.detailshow = false
                    this.detailshow = true
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
                this.loading = false
            }).catch(() => {
                this.loading = false
            })
        },
        initlevel () {
            axios.request({
                url: '/qms/InspectStandard/GetSectionTree',
                method: 'get'
            }).then(res => {
                const arr = []
                this.datalevel = res.data.data
                arr.push(res.data.data[0].value)
                this.leveldata = arr
            })
        },
        initproducts (sectionid) {
            this.products = [{ id: 0, name: '', code: '' }]
            axios.request({
                url: '/qms/InspectStandard/GetProductsBySectionId?sectionid=' + sectionid,
                method: 'get'
            }).then(res => {
                this.products = res.data.data
            })
        },
        initmaterials () {
            this.materials = [{ id: 0, name: '', pn: '', spec: '' }]
            axios.request({
                url: '/config/Material/GetSonMaterialsByProduct?productid=' + this.editobj.productid,
                method: 'get'
            }).then(res => {
                this.materials = res.data.data
            })
        },
        editroutechange () {
            this.editroutes = [{ id: 0, name: '' }]
            var sectionid = this.sectionid[this.sectionid.length - 1]
            axios.request({
                url: '/qms/ManualCollection/GetSectionRoutes?productid=' + this.editobj.productid + '&sectionid=' + sectionid,
                method: 'get'
            }).then(res => {
                this.editroutes = res.data.data
            })
            this.initmaterials()
            this.inititems()
        },
        inititems () {
            this.editobj.sectionid = this.sectionid[this.sectionid.length - 1]
            let data = {
                productid: this.editobj.productid,
                sectionid: this.editobj.sectionid,
                id: 0
            }
            axios.request({
                url: '/qms/ManualCollection/GetDetails?productid=' + data.productid + '&sectionid=' + data.sectionid + '&id=' + data.id,
                method: 'get'
            }).then(res => {
                this.mockTableData1(res.data.data)
            })
        },
        mockTableData1 (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.toUpperCase() === 'OK' || item.result.toUpperCase() === 'NG') ? item.result.toUpperCase() : item.result,
                        value: item.value
                    })
                }
            })
            this.tableData = data
        }
    }
}

</script>
<style lang="less" scoped>
.demo-upload-list{
    display: inline-block;
    width: 120px;
    height: 120px;
    text-align: center;
    line-height: 120px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
}
.demo-upload-list img{
    width: 100%;
    height: 100%;
}
.demo-upload-list-cover{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
}
.demo-upload-list:hover .demo-upload-list-cover{
    display: block;
}
.demo-upload-list-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
}

label {
  width: 80px;
  display: inline-block;
  text-align: right;
}
.testdiv{
   width: 100%;
   height: 100%;
}

.showdetail{
  width: 720px;
  height: 180px;
  // background-color: #515a6e;
  margin-top: 10px;
  border:0.3px solid #ccc;
  div{
    float: left;
    width: 25%;
    height: 100%;
    border:0.3px solid #ccc;
    // background-color: blue;
    margin: -0.3px;
    img{
      width: 100%;
      height: 100%;
    };
    div{
      width: 100%;
      height: 30px;
      margin: 0!important;
      div{
        display: inline-block;
        width: 50%;
        height: 100%;
        border:0.3px solid #ccc;
        text-align: center;
        line-height: 30px;
        margin: -0.3px;
      };
      .specialdiv{
        width: 100%;
        height: 30px;
        text-align: center;
        line-height: 30px;
      }
    }
  }
}
.filter{
  height: 40px;
  label{
    display: inline-block;
    width: 100px;
  }
}
.edit{
  .filter{
  height: 40px;
  label{
    display: inline-block;
    width: 80px;
  };
  .publicinput{
    width: 150px;
  };
}
}
.uploadexcel{
  display: inline-block;
  margin-right: 15px;
  // padding-top: 10px;
}
.file {
  width: 80px;
  height: 32px;
  position: relative;
   color: #fff;
    display: inline-block;
    margin-bottom: 0;
    font-weight: normal;
    text-align: center;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    background-image: none;
    border: 1px solid transparent;
    white-space: nowrap;
    line-height: 1.5;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    padding: 5px 15px 6px;
    font-size: 12px;
    border-radius: 4px;
    background-color: #2d8cf0;
    margin-right: 15px;
    // border-color: #2d8cf0;
    -webkit-appearance: button
    // border-color: #57a3f3;
}
.file input {
    position: absolute;
    // font-size: 100px;
    left: 0;
    top: 0;
    opacity: 0;
    width: 80px;
    height: 30px;
    // border: 1px solid #000;
    // z-index: 99999;
}
.editload,.addload{
  position:absolute;
  img{
    width: 150px;
    height: 300px;
    margin: 10px 0px;
  };
  input{
    position: absolute;
    // font-size: 100px;
    left: 80px;
    top: 10px;
    opacity: 0;
    width: 128px;
    height: 300px;
  }
}
.DeviceQRblock{
  width: 150px;
  height: 200px;
  float: left;
   margin-right: 10px;
  .DeviceQRblockqr{
   width: 150px;
   height: 150px;
  };
  div{
    text-align: center;
  }
}
</style>
