<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 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 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 id="操作人员">{{this.$t(600531)}}：</label>
          <Select v-model="userid" clearable class="searchSelect">
            <Option v-for="item in users" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label id="结果">{{this.$t(800195)}}：</label>
          <Select v-model="resultdesc" clearable class="searchSelect">
            <Option value="OK" >OK</Option>
            <Option value="NG" >NG</Option>
            <Option value="2" >{{this.$t(1950549)}}</Option>
          </Select>
        </div>
        <div class="filter">
            <label>{{$t('1046')}}：</label>
            <DatePicker
              id="日期"
              type="datetimerange"
              v-model="daterange"
              confirm
              placeholder="Select date"
              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 intervalRight" 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="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">
              <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">
                  <span>{{ row.result }}</span>
              </template>
              <template slot-scope="{ row, index }" slot="value">
                  <span>{{ row.value }}</span>
              </template>
              <template slot-scope="{ row, index }" slot="description">
                  <span>{{ row.description }}</span>
              </template>
          </Table>
      </template>
    </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')
            },
            userid: 0,
            resultdesc: '',
            morecondition: false, // 条件
            page: 1,
            pagesize: 10,
            total: 0,
            tableData1: [],
            tableData2: [],
            columns1: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    slot: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        )
                    },
                    width: 90
                },
                {
                    title: this.$t('901048'),
                    slot: 'item'
                },
                {
                    title: this.$t('100044'),
                    slot: 'description'
                },
                {
                    title: this.$t('950245'),
                    slot: 'standard'
                },
                {
                    title: this.$t('200060'),
                    slot: 'min_value'
                },
                {
                    title: this.$t('200059'),
                    slot: 'max_value'
                },
                {
                    title: this.$t('800195'),
                    slot: 'result'
                },
                {
                    title: this.$t('920168'),
                    slot: 'value'
                }
            ],
            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('500006'),
                //   key: 'routename'
                // },
                {
                    title: this.$t('100088'),
                    key: 'productname'
                },
                {
                    title: this.$t('1950491'),
                    key: 'sectionname'
                },
                {
                    title: this.$t('900053'),
                    key: 'workorder'
                },
                {
                    title: this.$t('800008'),
                    key: 'sn'
                },
                {
                    title: this.$t('800195'),
                    key: 'result'
                },
                {
                    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,
            users: [],
            results: [],
            loading1: false,
            loading2: false
        }
    },
    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.getUsers()
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
        this.columns1 = this.$time.initTableTitle(this.columns1)
    },
    methods: {
        handlelevelchange (value) {
            this.leveldata = value
            var sectionid = value[value.length - 1]
            this.initproducts(sectionid)
        },
        initlevel () {
            axios.request({
                url: '/config/ProjectLevelSection/GetSectionTree',
                method: 'get'
            }).then(res => {
                const arr = []
                this.datalevel = res.data.data
                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/GetTraceDetailList?id=' + this.obj.id,
                        method: 'get'
                    })
                    .then(res => {
                        this.mockTableData2(res.data.data)
                    })
            }
        },
        getUsers (val) {
            var params = { page: '1', pagesize: '1000' }
            axios
                .request({
                    url: '/config/user/GetUsers?page=1&pagesize=999999',
                    params,
                    method: 'get'
                })
                .then(res => {
                    this.users = 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
                    })
                }
            })
            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,
                userid: this.userid,
                result: this.resultdesc,
                toexcel: 0
            }
            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?sectionid=' + sectionid,
                method: 'get'
            }).then(res => {
                this.products = res.data.data
            })
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    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 === '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
                    })
                }
            })
            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,
                userid: this.userid,
                result: this.resultdesc,
                toexcel: 1
            }
            this.loading2 = true
            axios.request({
                url: '/qms/ManualCollection/GetManualPageList',
                params,
                method: 'get'
            }).then(res => {
                this.loading2 = 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.loading2 = 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>
.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>
