<template>
  <div class="testdiv">
    <div class="divborder leftDiv">
      <Tree class="treeDiv" :data="datax" ref="tree" @on-select-change="handlesearch()"></Tree>
    </div>
    <div class="rightDiv">
      <div class="divborder newSearchDiv">
        <div class="filter">
          <label id="SN">SN：</label>
          <Input v-model="sn" placeholder="sn" class="searchInput" />
        </div>
        <!-- <Button type="primary" icon="ios-search" @click="search()">{{$t('1001')}}</Button>
        <Button type="primary" icon="md-download" @click="loadexcel()">{{$t('1005')}}</Button>-->
        <div class="searchBtn">
          <Button
            type="primary"
            class="twoWord intervalRight"
            icon="ios-search"
            @click="search"
          >{{$t('1001')}}</Button>
          <Button
            type="primary"
            class="fourWord"
            icon="md-download"
            @click="loadexcel"
          >{{$t('1007')}}</Button>
        </div>
      </div>
      <div class="divborder newTableDiv">
        <Spin fix v-if="spinShow">
          <Icon type="ios-loading" size="18" class="demo-spin-icon-load"></Icon>
        </Spin>
        <Table class="tableClass" highlight-row :data="tableData" :columns="tableColumns"></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>
  </div>
</template>
<script>
import axios from '@/libs/api.request';
export default {
    data () {
        return {
            datax: [
                {
                    id: 0,
                    title: this.$t(930024),
                    expand: true,
                    category: '3000021',
                    children: [
                        {
                            id: 0,
                            title: 'SN',
                            category: null,
                            content: null,
                            expand: true,
                            children: [
                                {
                                    id: 1,
                                    title: this.$t(300018),
                                    data_url: '/acc/SN/GetData',
                                    export_url: '/acc/SeqDetailByDate/ExportSn',
                                    category: '3000021',
                                    rpt: '3000024',
                                    expand: true
                                },
                                {
                                    id: 2,
                                    title: this.$t(950254),
                                    parentid: 1,
                                    data_url: '/acc/SN/GetData',
                                    export_url: '/acc/SeqDetailByDate/ExportTrace',
                                    category: '3000021',
                                    rpt: '3000025',
                                    expand: true
                                },
                                {
                                    id: 3,
                                    title: this.$t(200223),
                                    data_url: '/acc/SN/GetData',
                                    export_url: '/acc/SeqDetailByDate/ExportTraceDts',
                                    category: '3000021',
                                    rpt: '3000026',
                                    expand: true
                                },
                                // {
                                //     id: 4,
                                //     title: this.$t(200224),
                                //     data_url: '/acc/RptPacking/GetPackingBoxSnData',
                                //     export_url: '/acc/SeqDetailByDate/ExportTraceDts',
                                //     category: '3000021',
                                //     rpt: '3000027',
                                //     expand: true
                                // },
                                // {
                                //     id: 5,
                                //     title: this.$t(200225),
                                //     data_url: '/acc/RptPacking/GetPackingBoxSnData',
                                //     export_url: '/acc/SeqDetailByDate/ExportTraceDts',
                                //     category: '3000021',
                                //     rpt: '3000027',
                                //     expand: true
                                // },
                                // {
                                //     id: 6,
                                //     title: this.$t(200226),
                                //     data_url: '/acc/RptPacking/GetWasteData',
                                //     export_url: '/acc/SeqDetailByDate/ExportTraceDts',
                                //     category: '3000021',
                                //     rpt: '3000028',
                                //     expand: true
                                // },
                                // {
                                //     id: 7,
                                //     title: this.$t(200227),
                                //     data_url: '/acc/Rework/GetReworkData',
                                //     export_url: '/acc/SeqDetailByDate/ExportTraceDts',
                                //     category: '3000022',
                                //     rpt: '3000027',
                                //     expand: true
                                // },
                                {
                                    id: 8,
                                    title: this.$t(5500249),
                                    data_url: '/acc/AppAssemb/CheckAssemble',
                                    export_url: '',
                                    category: '3000022',
                                    rpt: '3000027',
                                    expand: true
                                }
                            ]
                        }
                    ]
                }
            ],
            obj: {},
            sn: '',
            page: 1,
            pagesize: 10,
            total: 0,
            tableData: [],
            tableColumns: [],
            spinShow: false
        };
    },
    methods: {
        handlesearch () {
            this.page = 1;
            this.search();
        },
        search () {
            const urldata = this.$refs.tree.getSelectedNodes();

            if (urldata.length < 1) {
                this.$Message.error(this.$t(200228));
                return false;
            }
            if (urldata[0].id == 0) {
                return false;
            }
            if (this.sn.length < 1) {
                this.$Message.error(this.$t(930025));
                return false;
            }

            switch (urldata[0].id) {
            case 1:
                this.tableColumns = [
                    {
                        type: 'index2',
                        title: this.$t(1008),
                        key: 'xuhao',
                        render: (h, params) => {
                            return h(
                                'span',
                                params.index + (this.page - 1) * this.pagesize + 1
                            );
                        },
                        width: 90
                    },
                    {
                        title: this.$t(800008),
                        key: 'sn'
                    },
                    {
                        title: this.$t(100089),
                        key: 'procutName'
                    },
                    {
                        title: this.$t(100088),
                        key: 'productname'
                    },
                    {
                        title: this.$t(5500100),
                        key: 'productdraw'
                    },
                    {
                        title: this.$t(1025),
                        key: 'lineNo'
                    },
                    {
                        title: this.$t(900053),
                        key: 'wo'
                    },
                    {
                        title: this.$t(900933),
                        key: 'pn'
                    },
                    {
                        title: this.$t(1026),
                        key: 'station'
                    },
                    {
                        title: this.$t(800195),
                        key: 'result'
                    },
                    {
                        title: this.$t(950230),
                        key: 'inputDateTime'
                    },
                    {
                        title: this.$t(900410),
                        key: 'updateTime'
                    }
                ];
                break;
            case 2:
                this.tableColumns = [
                    {
                        type: 'index2',
                        title: this.$t(1008),
                        key: 'xuhao',
                        render: (h, params) => {
                            return h(
                                'span',
                                params.index + (this.page - 1) * this.pagesize + 1
                            );
                        },
                        width: 90
                    },
                    {
                        title: this.$t(800008),
                        key: 'sn'
                    },
                    {
                        title: this.$t(100089),
                        key: 'productID'
                    },
                    {
                        title: this.$t(100088),
                        key: 'productname'
                    },
                    {
                        title: this.$t(5500100),
                        key: 'productdraw'
                    },
                    {
                        title: this.$t(1025),
                        key: 'line'
                    },
                    {
                        title: this.$t(900053),
                        key: 'wo'
                    },
                    {
                        title: this.$t(1026),
                        key: 'station'
                    },
                    {
                        title: this.$t(800195),
                        key: 'result'
                    },
                    {
                        title: this.$t(900993),
                        key: 'user'
                    },
                    {
                        title: this.$t(1046),
                        key: 'transDateTime'
                    }
                ];
                break;
            case 3:
                this.tableColumns = [
                    {
                        type: 'index2',
                        title: this.$t(1008),
                        key: 'xuhao',
                        render: (h, params) => {
                            return h(
                                'span',
                                params.index + (this.page - 1) * this.pagesize + 1
                            );
                        },
                        width: 90
                    },
                    {
                        title: this.$t(1025),
                        key: 'lineTitle'
                    },
                    {
                        title: this.$t(1026),
                        key: 'stationTitle'
                    },
                    {
                        title: this.$t(800008),
                        key: 'sn'
                    },
                    {
                        title: this.$t(1950597),
                        key: 'item'
                    },
                    {
                        title: this.$t(920168),
                        key: 'value',
                        render:(h,params)=>{
                            let url = params.row.value;
                            let code = false
                            if(url!=null&&url!=""&&url!=undefined){
                                if(url.indexOf('.xls')!=-1 || url.indexOf('.xlsx')!=-1){
                                    code = true;
                                }
                            }
                            return h('span',
                                {
                                    style:{
                                        color:code?'#57a3f3':'',
                                        cursor:code?'pointer':''
                                    },
                                    on:{
                                        click:()=>{
                                            if(code){
                                                window.location.href = axios.publicPath + url
                                            }
                                        }
                                    }
                                },params.row.value
                            )
                        }
                    },
                    {
                        title: this.$t(800195),
                        key: 'result'
                    },
                    {
                        title: this.$t(1046),
                        key: 'transDateTime'
                    }
                ];
                break;
            case 4:
                this.tableColumns = [
                    {
                        type: 'index2',
                        title: this.$t(1008),
                        key: 'xuhao',
                        render: (h, params) => {
                            return h(
                                'span',
                                params.index + (this.page - 1) * this.pagesize + 1
                            );
                        },
                        width: 90
                    },
                    {
                        title: this.$t(950251),
                        key: 'boxID'
                    },
                    {
                        title: this.$t(800008),
                        key: 'sn'
                    },
                    {
                        title: this.$t(200229),
                        key: 'deleteFlag'
                    },
                    {
                        title: 'IP',
                        key: 'ip'
                    },
                    {
                        title: this.$t(200230),
                        key: 'user'
                    },
                    {
                        title: this.$t(1046),
                        key: 'transDateTime'
                    }
                ];
                break;
            case 5:
                this.tableColumns = [
                    {
                        type: 'index2',
                        title: this.$t(1008),
                        key: 'xuhao',
                        render: (h, params) => {
                            return h(
                                'span',
                                params.index + (this.page - 1) * this.pagesize + 1
                            );
                        },
                        width: 90
                    },
                    {
                        title: this.$t(950251),
                        key: 'boxID'
                    },
                    {
                        title: this.$t(800008),
                        key: 'sn'
                    },
                    {
                        title: this.$t(200229),
                        key: 'deleteFlag'
                    },
                    {
                        title: 'IP',
                        key: 'ip'
                    },
                    {
                        title: this.$t(200230),
                        key: 'user'
                    },
                    {
                        title: this.$t(1046),
                        key: 'transDateTime'
                    }
                ];
                break;
            case 6:
                this.tableColumns = [
                    {
                        type: 'index2',
                        title: this.$t(1008),
                        key: 'xuhao',
                        render: (h, params) => {
                            return h(
                                'span',
                                params.index + (this.page - 1) * this.pagesize + 1
                            );
                        },
                        width: 90
                    },
                    {
                        title: this.$t(800008),
                        key: 'sn'
                    },
                    {
                        title: this.$t(930027),
                        key: 'line'
                    },
                    {
                        title: this.$t(800283),
                        key: 'wType'
                    },
                    {
                        title: this.$t(100044),
                        key: 'description'
                    },
                    {
                        title: this.$t(1046),
                        key: 'lastUpdate'
                    }
                ];
                break;
            case 7:
                this.tableColumns = [
                    {
                        type: 'index2',
                        title: this.$t(1008),
                        key: 'xuhao',
                        render: (h, params) => {
                            return h(
                                'span',
                                params.index + (this.page - 1) * this.pagesize + 1
                            );
                        },
                        width: 90
                    },
                    {
                        title: this.$t(950251),
                        key: 'boxID'
                    },
                    {
                        title: this.$t(800008),
                        key: 'sn'
                    },
                    {
                        title: this.$t(200229),
                        key: 'deleteFlag'
                    },
                    {
                        title: 'IP',
                        key: 'ip'
                    },
                    {
                        title: this.$t(200230),
                        key: 'user'
                    },
                    {
                        title: this.$t(1046),
                        key: 'transDateTime'
                    }
                ];
                break;
            case 8:
                this.tableColumns = [
                    {
                        type: 'index2',
                        title: this.$t(1008),
                        key: 'xuhao',
                        render: (h, params) => {
                            return h(
                                'span',
                                params.index + (this.page - 1) * this.pagesize + 1
                            );
                        },
                        width: 90
                    },
                    {
                        title: this.$t(800008),
                        key: 'sn'
                    },
                    {
                        title: this.$t(100089),
                        key: 'productID'
                    },
                    {
                        title: this.$t(100088),
                        key: 'productname'
                    },
                    {
                        title: this.$t(5500100),
                        key: 'productdraw'
                    },
                    {
                        title: this.$t(1132),
                        key: 'line'
                    },
                    {
                        title: this.$t(900053),
                        key: 'wo'
                    },
                    {
                        title: this.$t(900933),
                        key: 'pn'
                    },
                    {
                        title: this.$t(800195),
                        key: 'resultStatus'
                    },
                    {
                        title: this.$t(950230),
                        key: 'inputDateTime'
                    },
                    {
                        title: this.$t(900410),
                        key: 'transDateTime'
                    },
                    {
                        title: this.$t(5500233),
                        key: 'parentSN'
                    }
                ];
                break;
            }
            this.tableColumns = this.$time.initTableTitle(this.tableColumns);
            if (urldata[0].id === 8) {
                var data = {
                    page: this.page,
                    pagesize: this.pagesize,
                    sn: this.sn.trim(),
                    posturl: urldata[0].data_url,
                    category: urldata[0].category,
                    rpt: urldata[0].rpt,
                    Offset: this.page,
                    Limit: this.pagesize,
                    excel: 0
                };
            } else {
                var data = {
                    page: this.page,
                    pagesize: this.pagesize,
                    number: this.sn.trim(),
                    posturl: urldata[0].data_url,
                    category: urldata[0].category,
                    rpt: urldata[0].rpt,
                    Offset: this.page,
                    Limit: this.pagesize,
                    excel: 0
                };
            }

            this.axios
                .request({
                    url: urldata[0].data_url,
                    data,
                    method: 'post'
                })
                .then((res) => {
                    this.spinShow = true;
                    if (res.data.ret === 1) {
                        this.total = res.data.total;
                        this.tableData = res.data.data;
                    } else {
                        this.tableData = [];
                        this.$Message.error(this.$t(930026) + this.$t(res.data.msg));
                    }
                    this.spinShow = false;
                });
        },
        handlePageSize (value) {
            this.pagesize = value;
            this.tableData = this.search(this.page, this.pagesize);
        },
        changePage (value) {
            this.page = value;
            this.tableData = this.search(this.page, this.pagesize);
        },
        loadexcel () {
            const urldata = this.$refs.tree.getSelectedNodes();
            if (urldata.length < 1) {
                this.$Message.error(this.$t(200228));
                return false;
            }
            if (this.sn.length < 1) {
                this.$Message.error(this.$t(930025));
                return false;
            }
            const params = {
                number: this.sn,
                excel: 1
            };
            axios
                .request({
                    url: urldata[0].export_url,
                    params,
                    method: 'get'
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        window.location.href = axios.publicPath + '' + res.data.data;
                    } else {
                        this.$Message.error(this.$t(930026) + this.$t(res.data.msg));
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped>
.demo-spin-icon-load {
  animation: ani-demo-spin 1s linear infinite;
}
</style>
