<template>
  <div class="entryWrap" id="entry">
    <div class="expand" id="full" @click="handleFullScreen">
      <Icon type="md-expand" size="50" />
    </div>
    <div class="entryItem">
      <div class="choose">
        <p class="selectTitle">1.{{$t('90000016')}}</p>
        <div class="row">
          <p
            class="options"
            v-for="(item,index) in lineList"
            @click="linechange(index)"
            :class="{active:a==index}"
          >{{item.name}}</p>
        </div>
      </div>
      <div class="choose">
        <p class="selectTitle">2.{{$t('1052')}}</p>
        <div class="row">
          <p
            class="options"
            v-for="(item,index) in stationList"
            @click="stationchange(index)"
            :class="{active:b==index}"
          >{{item.name}}</p>
        </div>
      </div>
    </div>
    <div class="entryItem">
      <div class="choose">
        <p class="selectTitle">3.{{$t('100217')}}</p>
        <div class="row">
          <p
            class="options"
            v-for="(item,index) in productList"
            @click="productchange(index)"
            :class="{active:c==index}"
          >{{item.name+"【"+item.code+"】"}}</p>
        </div>
      </div>
      <div class="choose">
        <p class="selectTitle">4.{{$t('1950603')}}</p>
        <div class="row">
          <p
            class="options"
            v-for="(item,index) in materialList"
            @click="materialchange(index)"
            :class="{active:d==index}"
          >{{item.name+"【"+item.pn+"】"}}</p>
        </div>
      </div>
    </div>
    <div class="entryItem">
      <div class="choose">
        <p class="selectTitle">5.{{$t('90000017')}}</p>
        <div class="row">
          <p
            class="options"
            v-for="(item,index) in checktypeList"
            @click="checktypechange(index)"
            :class="{active:e==index}"
          >{{item.name}}</p>
        </div>
        <div class="row1" v-show="this.checktype.id === 7">
          <div class="number row1">
            <p class="inputlabel">{{$t('1950614') + ':'}}</p>
          </div>
          <input class="erweima" type="text" v-model="reason"/>
        </div>
      </div>
    </div>
    <div class="entryItem">
      <div class="choose">
        <p class="selectTitle">6.{{$t('90000018')}}</p>
        <div class="row">
          <p
            class="options"
            v-for="(item,index) in testroomList"
            @click="testroomchange(index)"
            :class="{active:f==index}"
          >{{item.value}}</p>
        </div>
      </div>
    </div>
    <div class="entryItem">
      <div class="choose">
        <p class="selectTitle">7.{{$t('1950615')}}</p>
        <div class="row">
          <template v-for="(item, i) in parameters">
            <div class="filter" v-show="item.category === 1">
              <label >{{item.name}}<i v-show="item.is_required === 1" style="color:red;margin-left: 5px!important;margin-top: -5px!important" >*</i>:</label>
              <input class="publicinput" type="text" v-model="parameters[i].result" style="width:160px"/>
            </div>
            <div class="filter" v-show="item.category === 2">
              <label >{{item.name}}<i v-show="item.is_required === 1" style="color:red;margin-left: 5px!important;margin-top: -5px!important" >*</i>:</label>
              <RadioGroup v-model="parameters[i].result">
                <Radio v-for="(item1, i1) in item.parameters" :key="i1" :label="item1.key" style="width:70px">{{item1.value}}</Radio>
              </RadioGroup>
            </div>
          </template>
        </div>
      </div>
    </div>
    <div class="entryItem">
      <div class="choose">
        <p class="selectTitle">8.{{$t('90000019')}}</p>
        <div class="row1">
          <div class="number row1">
            <p class="minus" @click="reduce">-</p>
            <input class="digit" v-model="number">
            <p class="plus" @click="add">+</p>
          </div>
          <input class="erweima" type="text" v-model="qrcode" @keyup.enter="savedata1"/>
        </div>
      </div>
    </div>
    <div class="entryItem pr">
      <span class="tip" v-text="message"></span>
      <!--<button class="save" @click="savedata">保存</button>-->
      <Button type="primary" :loading="loading" @click="savedata" class="save">
        <span v-if="!loading">{{$t('900306')}}</span>
        <span v-else></span>
      </Button>
    </div>
    <div>
  </div>
  </div>
</template>

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

export default {
    data () {
        return {
            lineList: [],
            stationList: [],
            productList: [],
            materialList: [],
            materidArray: [],
            checktypeList: [
                /*{ id: 5, name: this.$t(930033) },
                { id: 2, name: this.$t(930034) },
                { id: 3, name: this.$t(930035) },
                { id: 6, name: this.$t(930036) },
                { id: 1, name: this.$t(950367) },
                { id: 4, name: this.$t(930037) },
                { id: 7, name: this.$t(600233) }*/
            ],
            testroomList: [],
            line: {},
            station: {},
            product: {},
            material: {},
            checktype: {},
            testroom: {},
            number: 1,
            number1: 0,
            qrcode: '',
            a: null,
            b: 0,
            c: 0,
            d: 0,
            e: 0,
            f: 0,
            loading: false,
            resultData: {},
            message: '',
            reason: '',
            parameters: []
        }
    },
    created () {
        this.initlines()
        this.inittestroom()
        this.initchecktype()
    },
    methods: {
        initparameters (sectionid) {
            axios.request({
                url: '/qms/SendTestParameter/GetParameters?sectionid=' + sectionid,
                method: 'get'
            }).then(res => {
                this.parameters = res.data.data
            })
        },
        initlines () {
            axios.request({
                url: '/qms/InspectStandard/GetItemLevelSection?reverselevel=2&parentid=0',
                method: 'get'
            }).then(res => {
                this.lineList = res.data.data
            })
        },
        linechange (i) {
            this.a = i
            this.line = this.lineList[i]
            this.initstations(this.line.id)
            // option[i].classList.add('active')
        },
        initstations (lineid) {
            this.stationList = []
            this.station = {}
            this.b = 0
            if (lineid > 0) {
                axios.request({
                    url: '/qms/InspectStandard/GetItemLevelSection?reverselevel=1&parentid=' + lineid,
                    method: 'get'
                }).then(res => {
                    this.stationList = res.data.data
                    this.station = this.stationList[0]
                    this.initproduct(this.station.id)
                    this.initparameters(this.station.id)
                })
            }
        },
        initproduct (stationid) {
            this.productList = []
            this.product = {}
            this.c = 0
            if (stationid > 0) {
                axios.request({
                    url: '/qms/InspectStandard/GetProductsBySectionId?sectionid=' + stationid,
                    method: 'get'
                }).then(res => {
                    this.productList = res.data.data
                    this.product = this.productList[0]
                    this.initmaterial(this.product.id, stationid)
                })
            }
        },
        stationchange (i) {
            this.b = i
            this.station = this.stationList[i]
            this.initproduct(this.station.id)
            this.initparameters(this.station.id)
        },
        productchange (i) {
            this.c = i
            this.product = this.productList[i]
            this.initmaterial(this.product.id, this.station.id)
        },
        initmaterial (productid, sectionid) {
            this.materialList = []
            this.material = {}
            this.d = null
            if (productid > 0) {
                axios.request({
                    url: '/qms/ManufacturingMaterial/GetMaterialByCell?productId=' + productid + '&section=' + sectionid,
                    method: 'get'
                }).then(res => {
                    if (res.data.ret === 1) {
                        this.materialList = res.data.data
                    }
                })
            }
        },
        materialchange (i) {
            if (this.materidArray.indexOf(i) > -1) {
                this.materidArray = []
                this.material = {}
                this.d = -1
            } else {
                this.materidArray.push(i)
                this.material = this.materialList[i]
                this.d = i
            }
        },
        initchecktype () {
          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})
            });
            this.checktypechange(0)
          }) 
        },
        checktypechange (i) {
            this.checktype = this.checktypeList[i]
            this.e = i
            axios.request({
                url: '/qms/SendTestCollection/GetCheckTypeCount?checktype=' + this.checktype.id,
                method: 'get'
            })
                .then(res => {
                    this.number = res.data.data
                })
        },
        inittestroom () {
            axios.request({
                url: '/qms/SendTestCollection/GetTestRoomDict',
                method: 'get'
            })
                .then(res => {
                    this.testroomList = res.data.data
                    this.testroomchange(0)
                })
        },
        testroomchange (i) {
            this.testroom = this.testroomList[i]
            this.f = i
        },
        savedata1 () {
            var resultData = {}
            resultData.productid = this.product.id
            resultData.materialid = this.material.id
            resultData.sectionid = this.station.id
            resultData.sn = this.qrcode
            resultData.checktype = this.checktype.id
            resultData.testroom = this.testroom.key
            resultData.number = 1
            this.number = 1
            resultData.reason = this.reason
            resultData.parameters = this.parameters
            this.saveresult(resultData)
        },
        savedata () {
            var resultData = {}
            resultData.productid = this.product.id
            resultData.materialid = this.material.id
            resultData.sectionid = this.station.id
            resultData.sn = this.qrcode
            resultData.checktype = this.checktype.id
            resultData.testroom = this.testroom.key
            resultData.number = this.number
            resultData.reason = this.reason
            resultData.parameters = this.parameters
            this.saveresult(resultData)
        },
        saveresult (data) {
            this.loading = true
            axios.request({
                url: '/qms/SendTestCollection/SendTest',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    // this.$Message.success(this.$t('800223'))
                    this.number1++
                    this.message = this.$t(930045)// ✚"+this.number1
                    this.qrcode = ''
                    this.reason = ''
                } else {
                    this.message = this.$t(930046) + this.$t(res.data.msg)
                }
                this.loading = false
            }).catch(() => {
                this.loading = false
            })
        },
        reduce () {
            if (this.number > 1) {
                this.number--
            } else {
                this.number = 1
            }
        },
        add () {
            this.number++
        },
        // 全屏事件
        handleFullScreen () {
            let element = document.getElementById('entry')
            // 判断是否已经是全屏
            // 如果是全屏，退出
            if (this.fullscreen) {
                if (document.exitFullscreen) {
                    document.exitFullscreen()
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen()
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen()
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen()
                }
            } else { // 否则，进入全屏
                if (element.requestFullscreen) {
                    element.requestFullscreen()
                } else if (element.webkitRequestFullScreen) {
                    element.webkitRequestFullScreen()
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen()
                } else if (element.msRequestFullscreen) {
                    // IE11
                    element.msRequestFullscreen()
                }
            }
            // 改变当前全屏状态
            this.fullscreen = !this.fullscreen
        },
        requestFullScreen (element) {
            var requestMethod =
        element.requestFullScreen ||
        element.webkitRequestFullScreen ||
        element.mozRequestFullScreen ||
        element.msRequestFullScreen
            if (requestMethod) {
                requestMethod.call(element)
            } else if (typeof window.ActiveXObject !== 'undefined') {
                var wscript = new ActiveXObject('WScript.Shell')
                if (wscript !== null) {
                    wscript.SendKeys('{F11}')
                }
            }
        }
    }
}
</script>

<style scoped>
.row {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
}
.row1 {
  display: flex;
  flex-direction: row;
}
.entryWrap {
  position: relative;
  background-color: #F5F7F9;
  overflow-y: auto;
  overflow-x: hidden;
}
.expand {
  position: absolute;
  top: 10px;
  /*left: 95%;*/
  right:50px;
}
.entryItem {
  background-color: #fff;
  border-radius: 10px;
  margin-bottom: 20px;
  padding: 20px;
  width: 100%;
}
.selectTitle {
  font-size: 18px;
}
.options {
  font-size: 12px;
  padding: 0 10px;
  /* width: 100px; */
  height: 30px;
  line-height: 30px;
  text-align: center;
  border-radius: 5px;
  background-color: #f2f2f2;
  margin: 10px 0;
  margin-right: 50px;
  cursor: pointer;
}
.active {
  background-color: #2d8cf0;
  color: #fff;
}
.number {
  margin: 10px 55px 10px 0;
}
.minus {
  width: 55px;
  height: 42px;
  line-height: 42px;
  font-size: 20px;
  text-align: center;
  color: #fff;
  background-color: #b8741a;
  border-radius: 5px;
  cursor: pointer;
}
.inputlabel {
  width: 100px;
  height: 42px;
  line-height: 42px;
  font-size: 20px;
  text-align: center;
  cursor: pointer;
}
.plus {
  width: 55px;
  height: 42px;
  line-height: 42px;
  font-size: 20px;
  text-align: center;
  color: #fff;
  background-color: #4b7902;
  border-radius: 5px;
  cursor: pointer;
}
.digit {
  display: block;
  outline: none;
  width: 180px;
  height: 42px;
  line-height: 42px;
  text-align: center;
  font-size: 20px;
  border: 1px solid #d8d8d8;
  background-color: #f4f7f9;
  margin: 0 5px;
  color: #000;
}
.erweima {
  display: block;
  outline: none;
  width: 100%;
  height: 42px;
  line-height: 42px;
  font-size: 20px;
  color: #000;
  text-align: center;
  border: 1px solid#d8d8d8;
  margin: 10px 0;
}

.save {
  width: 95px;
  height: 55px;
  background-color: #347ab8;
  color: #fff;
  /* line-height: 55px; */
  text-align: center;
  font-size: 20px;
  /* cursor: pointer; */
  outline: none;
  border: none;
  border-radius: 10px;
  /* position: absolute;
  right: 20px; */
  /* margin-left: 80%; */
  position:absolute;
  right:100px;
}
.pr {
  position: relative;
  height:95px;
}
.tip{
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 16px;
  /* border: 1px solid #d2d2d2;
   */
  border-radius: 5px;
}
</style>
