<template>
  <div class="testdiv">
    <div class="divborder leftDiv">
      <Tree :data="datax" ref="tree" @on-select-change="handlesearch()"></Tree>
    </div>
    <div class="rightDiv">
      <div class="divborder newSearchDiv" style="margin:0px">
        <div v-show="showsearch" class="filter">
            <label>{{$t('1132')}}：</label>
            <Cascader
            :data="datalevel"
            :value="leveldata"
            trigger="click"
            @on-change="handlelevelchange"
            change-on-select
            class="searchSelect"
            ></Cascader>
        </div>
        <div class="filter">
          <label>{{$t("900910")}}</label>
          <Select
            v-model="datetype"
            label-in-value
            clearable
            class="searchSelect"
            @on-change="timechange"
          >
            <Option :value="1" id="今天">{{$t("900911")}}</Option>
            <Option :value="2" id="昨天">{{$t("900390")}}</Option>
            <Option :value="3" id="本周">{{$t("900912")}}</Option>
            <Option :value="4" id="上周">{{$t("900913")}}</Option>
            <Option :value="5" id="本月">{{$t("900914")}}</Option>
            <Option :value="6" id="上月">{{$t("900915")}}</Option>
            <Option :value="7" id="本年">{{$t("900916")}}</Option>
            <Option :value="8" id="上年">{{$t("900917")}}</Option>
          </Select>
        </div>
        <div class="filter">
          <label>{{$t('1035')}}</label>
          <DatePicker
            type="datetimerange"
            format="yyyy-MM-dd HH:mm:ss"
            v-model="index_time"
            class="searchInput MaxWidth"
          ></DatePicker>
        </div>
        
        <div v-show="showsearch" class="filter">
          <label>{{$t('100089')}}：</label>
          <Select
            v-model="productname"
            label-in-value
            clearable
            filterable
            remote
            :remote-method="remoteMethod1"
            :loading="loading1"
            class="searchSelect"
          >
            <Option
              v-for="(item,index) in products"
              :value="item.code"
              :key="index"
            >{{ item.name }}({{item.code}})</Option>
          </Select>
        </div>
        
        <!-- <div v-show="showsearch" class="filter">
            <label >{{$t('900933')}}：</label>
            <Select v-model="material" label-in-value clearable class="searchSelect">
              <Option
                v-for="(item,index) in materials"
                :value="item.pn"
                :key="index"
              >{{ item.name }}({{item.pn}})</Option>
            </Select>
        </div>-->
        <div class="searchBtn">
          <Button
            type="primary"
            icon="ios-search"
            class="twoWord intervalRight"
            @click="search"
          >{{$t('1001')}}</Button>
          <Button
            type="primary"
            icon="md-download"
            class="fourWord"
            @click="loadexcel"
            v-show="isShowExport"
          >{{$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';
import moment from 'moment';
export default {
    data () {
        return {
            datax: [
                {
                    id: 0,
                    title: '页面',
                    expand: true,
                    category: '3000021',
                    disabled: true,

                    children: [
                        {
                            id: 1,
                            title: 'SN',
                            category: null,
                            content: null,
                            expand: true,
                            disabled: 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/CheckAssembleByDate',
                                    export_url: '',
                                    category: '3000022',
                                    rpt: '3000027',
                                    expand: true
                                }
                            ]
                        }
                    ]
                }
            ],
            obj: {},
            isShowExport: false,
            sn: '',
            datetype: 0,
            page: 1,
            pagesize: 10,
            total: 0,
            tableData: [],
            tableColumns: [],
            spinShow: false,
            index_time: [], // 初始时间
            productname: '',
            loading1: false,
            products: [],
            leveldata: [],
            datalevel: [],
            material: '', // 物料料号
            materials: [], // 料号列表
            showsearch: false
        };
    },
    created () {
        this.iniproducts('');
        this.initlevel();
        this.getCodeList();
        this.index_time = [
            moment(new Date().getTime() - 3600 * 1000 * 24 * 1).format(
                'YYYY-MM-DD '
            ) + '00:00:00',
            moment(new Date().getTime()).format('YYYY-MM-DD ') + '23:59:59'
        ];
    },
    methods: {
        getCodeList () {
            var data = {
                pn: '', // 物料编号
                name: '', // 供应商id
                count: 50 // 显示数量
            };
            axios
                .request({
                    url: '/config/Material/GetMaterialData',
                    data,
                    methods: 'get'
                })
                .then((res) => {
                    if (res.data.ret == 1) {
                        this.materials = res.data.data;
                    }
                });
        },
        handlelevelchange (value) {
            this.leveldata = value;
        },
        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;
                });
        },
        iniproducts (name) {
            var params = {
                name: name,
                count: 50
            };
            axios
                .request({
                    url: '/config/ProjectProduct/GetProductsByCode',
                    params,
                    method: 'get'
                })
                .then((res) => {
                    if (res.data.ret === 1 && res.data.data.length > 0) {
                        this.products = res.data.data;
                    }
                });
        },
        remoteMethod1 (name) {
            if (name !== '') {
                this.loading1 = true;
                setTimeout(() => {
                    this.loading1 = false;
                    this.iniproducts(name);
                }, 200);
            } else {
                this.iniproducts('');
            }
        },
        timechange () {
            this.index_time = this.$time.changetime(this.datetype);
        },
        handlesearch () {
            let node = this.$refs.tree.getSelectedNodes();
            const id = node.length!=0?this.$refs.tree.getSelectedNodes()[0].id:'';
            this.productname = '';
            if (id == 1 || id == 2 || id == 3) {
                this.isShowExport = true;
            } else {
                this.isShowExport = false;
            }
            if (id == 1 || id == 2 || id == 3 || id == 7 || id == 8) {
                this.showsearch = true;
            } else {
                this.showsearch = false;
            }
            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 (this.index_time.length < 2) {
                this.$Message.error(this.$t(1143));
                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: 'index',
                        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);
            const data = {
                page: this.page,
                pagesize: this.pagesize,
                ProductId: this.productname,
                Station: this.leveldata.length===0?0:this.leveldata[this.leveldata.length - 1],
                DtStart: moment(this.index_time[0]).format('YYYY-MM-DD HH:mm:ss'),
                DtEnd: moment(this.index_time[1]).format('YYYY-MM-DD HH:mm:ss'),
                posturl: urldata[0].data_url,
                category: urldata[0].category,
                rpt: urldata[0].rpt,
                Offset: this.page,
                Limit: this.pagesize,
                excel: 0
            };
            axios
                .request({
                    url: data.posturl,
                    data,
                    method: 'post'
                })
                .then((res) => {
                    this.spinShow = true;
                    this.total = 0
                    this.tableData = [];
                    if (res.data.ret === 1) {
                        this.total = res.data.total;
                        this.tableData = res.data.data;
                    } else {
                        this.$Message.error(this.$t(930026) + this.$t(res.data.msg));
                    }
                    this.spinShow = false;
                });
        },
        handlePageSize (value) {
            this.pagesize = value;
            this.tableData = this.search();
        },
        changePage (value) {
            this.page = value;
            this.tableData = this.search();
        },
        loadexcel () {
            const urldata = this.$refs.tree.getSelectedNodes();
            if (urldata.length < 1) {
                this.$Message.error(this.$t(200228));
                return false;
            }
            if (this.index_time.length < 2) {
                this.$Message.error(this.$t(1143));
                return false;
            }
            const params = {
                ProductId: this.productname,
                Station: this.leveldata.length===0?0:this.leveldata[this.leveldata.length - 1],
                DtStart: moment(this.index_time[0]).format('YYYY-MM-DD HH:mm:ss'),
                DtEnd: moment(this.index_time[1]).format('YYYY-MM-DD HH:mm:ss'),
                posturl: urldata[0].data_url,
                category: urldata[0].category,
                rpt: urldata[0].rpt,
                Offset: this.page,
                Limit: this.pagesize,
                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>
