<template>
    <div class="testdiv" >
      <div class="divborder newSearchDiv">
        <div class="filter">
          <label>{{$t('1132')}}：</label>
          <Cascader :data="datalevel" :value="leveldata" trigger="click" change-on-select class="searchSelect" @on-change="getlevel"></Cascader>
        </div>
        <div class="filter">
          <label>{{$t('300000')}}：</label>
          <Cascader :data="typelevel" :value="typeleveldata" @on-change="typechange" trigger="click" change-on-select class="searchSelect" ></Cascader>
        </div>
        <div class="filter">
          <label>{{$t('1034')}}：</label>
            <Input class="searchInput" type="text" :placeholder="$t('8000051')" v-model="keyword" />
        </div>
        <div class="filter">
                <label>{{$t('300029')}}：</label>
                <Select v-model="Standard_parts" label-in-value  clearable class="searchSelect" >
                    <Option  :value="1" v-text="$t('300030')" :label="$t('300030')">是</Option>
                    <Option  :value="0" v-text="$t('300031')" :label="$t('300031')">否</Option>
                </Select>
              </div>
        <!-- 次级搜索条件 隐藏 -->
        <div v-show="!morecondition">
              <div class="filter">
                <label>{{$t('300014')}}：</label>
                <Select v-model="substitution" label-in-value  clearable class="searchSelect" >
                    <Option  :value="1" v-text="$t('300030')" :label="$t('300030')">是</Option>
                    <Option  :value="0" v-text="$t('300031')" :label="$t('300031')">否</Option>
                </Select>
              </div>
              <div class="filter">
                <label >{{$t('900908')}}：</label>
                <DatePicker type="datetimerange" format="yyyy-MM-dd HH:mm:ss" v-model="index_time" class="searchInput MaxWidth"></DatePicker>
              </div>
             <div class="filter">
                <label>{{$t('300049')}}：</label>
                <Cascader :data="in_out_typelist" :value="typelistdata" trigger="click" @on-change="change" change-on-select class="searchSelect" ></Cascader>
              </div>
              <div class="filter">
                <label>{{$t('300048')}}：</label>
                <Select v-model="Work_order" label-in-value  clearable class="searchSelect" >
                    <Option  :value="1" v-text="$t('300030')" :label="$t('300030')">是</Option>
                    <Option  :value="2" v-text="$t('300031')" :label="$t('300031')">否</Option>
                </Select>
              </div>

        </div>
        <!-- 次级搜索条件  结束 -->
        <div class="searchBtn">
          <Button type="primary" class="twoWord intervalRight" icon="ios-search" @click="search(1,10)" >{{$t('1001')}}</Button>
          <Button type="primary" class="fourWord" icon="md-download" @click="loadexcel()">{{$t('1007')}}</Button>
        </div>

         <!-- 更多条件 按钮 -->
      <MoreConbtn @getCon="morecondition = !morecondition" :morecondition="morecondition"></MoreConbtn>

      </div>

      <div class="divborder newTableDiv">
          <Table
            class="tableClass"
            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>
</template>
<script>
import axios from '@/libs/api.request'
import moment from 'moment'
export default {
    // name:"record",
    data () {
        return {
            datalevel: [], // 产线层级数据
            leveldata: [], // 产线层级value
            keyword: '', // 关键词
            Standard_parts: '', // 是否标准件
            substitution: '', // 是否需要置换
            Work_order: '', // 关联维修工单
            morecondition: false, // 更多条件
            typelistdata: [], // 出入库类型value
            index_time: [], // 初始时间
            total: 0, // 初始数据条数
            page: 1, // 初始页码
            tableData1: [], // 初始列表数据
            dataarr: [], // 根据产线层级获取的数组
            typelevel: [], // 备件类型的数据
            typeleveldata: [], // 备件类型的value
            levelid: '', // 产线层级value
            pagesize: 10, // 初始页码条数
            sparepart: '',
            in_out_typelist: [{ // 输入库数据
                value: '1',
                label: this.$t('300009'), // 出库
                children: [{
                    value: '1',
                    label: this.$t('900688') // 采购提货出库
                }, {
                    value: '2',
                    label: this.$t('900687') // 销售提货出库
                }, {
                    value: '3',
                    label: this.$t('900686') // 应急维修领用出库
                }, {
                    value: '4',
                    label: this.$t('900685') // 借贷出库
                }, {
                    value: '5',
                    label: this.$t('900684') // 借入还出
                }, {
                    value: '6',
                    label: this.$t('900683') // 对账出库
                }, {
                    value: '7',
                    label: this.$t('900682') // 计划维护领用出库
                }]
            }, {
                value: '2',
                label: this.$t('300008'), // 入库
                children: [{
                    value: '1',
                    label: this.$t('300067') // 采购入库
                }, {
                    value: '2',
                    label: this.$t('300069') // 生产入库
                }, {
                    value: '3',
                    label: this.$t('300070') // 领用退回入库
                }, {
                    value: '4',
                    label: this.$t('300071') // 借贷入库
                }, {
                    value: '5',
                    label: this.$t('300072') // 借出还入
                }, {
                    value: '6',
                    label: this.$t('300073') // 对账入库
                }]
            }],
            tableColumns1: [ // table表头
                {
                    type: 'index2',
                    title: this.$t('1008'), // 序号
                    key: 'xuhao',
                    render: (h, params) => {
                        return h('span', params.index + (this.page - 1) * this.pagesize + 1)
                    },
                    width: 60,
                    align: 'center'
                },
                {
                    title: this.$t('900681'), // 出入库编号
                    key: 'record_code',
                    align: 'center'
                },
                {
                    title: this.$t('300050'), // 编号
                    key: 'code',
                    align: 'center'
                },
                {
                    title: this.$t('300080'), // 名称
                    key: 'name',
                    align: 'center'
                },
                {
                    title: this.$t('300062'), // 规格型号/图纸号
                    key: 'param',
                    width: 150,
                    align: 'center'
                },
                {
                    title: this.$t('300081'), // 出入库时间
                    key: 'time',
                    width: 150,
                    align: 'center'
                },
                {
                    title: this.$t('300049'), // 出入库类型
                    key: 'type',
                    align: 'center'
                },
                {
                    title: this.$t('900797'), // 维修工单/入库工单
                    key: 'work_order',
                    width: 200,
                    align: 'center'
                },
                {
                    title: this.$t('900798'), // 使用设备
                    key: 'mtitle',
                    align: 'center'
                },
                {
                    title: this.$t('300083'), // 领用人/入库人
                    width: 150,
                    key: 'Leader',
                    align: 'center'
                },
                {
                    title: this.$t('300084'), // 数量
                    key: 'amount',
                    align: 'center'
                },
                {
                    title: this.$t('300019'), // 单价
                    key: 'Unit_Price',
                    align: 'center'
                },
                {
                    title: this.$t('300085'), // 总价
                    key: 'Total_price',
                    align: 'center'
                },
                {
                    title: this.$t('300016'), // 当前库存
                    key: 'Stock',
                    align: 'center'
                },
                {
                    title: this.$t('300058'), // 经办人
                    key: 'operator',
                    align: 'center'
                },
                {
                    title: this.$t('800041'), // 备注
                    key: 'remark',
                    align: 'center'
                }
            ]

        }
    },
    created () {
        this.initlevel() // 初始化产线层级
        this.inittype() // 初始化备件类型
        // 初始化时间
        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']
        this.search(1, 10)
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
    // 备件类型变化
        typechange (value) {
            this.typeleveldata = value
        },
        // 出入库类型变化
        change (value) {
            this.typelistdata = value
        },
        // 初始化产线层级
        initlevel () {
            axios.request({
                url: 'config/ProjectLevelSection/GetSectionTree',
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    const arr = []
                    this.datalevel = res.data.data
                    // arr.push(res.data.data[0].value)
                    this.leveldata = arr
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        // 通过层级id获得数组
        getlevel: function (value) {
            this.leveldata = value
            this.levelid = this.leveldata[this.leveldata.length - 1]
            var id = (this.leveldata != undefined && this.leveldata.length > 0) ? this.leveldata[this.leveldata.length - 1] : 0
            axios.request({
                url: '/config/UserLevel/ListMachineIdArrEx?id=' + id,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.dataarr = res.data.data
                    // this.dataarr.push(0)
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        // 初始化备件类型
        inittype () {
            axios.request({
                url: '/tpm/SparepartType/SparepartListType',
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.typelevel = res.data.data.children
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        // 查询
        search (page, pagesize) {
            this.levelid = (this.leveldata != undefined && this.leveldata.length > 0) ? this.leveldata[this.leveldata.length - 1] : 0
            var params = {
                id: this.levelid
            }
            axios.request({
                url: '/config/UserLevel/ListMachineIdArrEx',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.dataarr = res.data.data
                    // this.dataarr.push(0)
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }

                //  resolve();
            }).then(res => {
                var value1, value2
                if (this.typelistdata[0] === '1') {
                    value1 = this.typelistdata[1] // 出库
                    value2 = ''// 入库
                } else {
                    value1 = '' // 出库
                    value2 = this.typelistdata[1]// 入库
                }
                // console.log(this.index_time[0],this.index_time[1])
                var data = {
                    keyword: this.keyword,
                    s_is_standard: this.Standard_parts,
                    s_is_substitute: this.substitution,
                    skutype: this.typelistdata[0],
                    inStockType: value2,
                    outStockType: value1,
                    link: this.Work_order,
                    machineIds: this.leveldata.length === 0 ? '' : this.dataarr.join(','),
                    sparepart: this.typeleveldata[this.typeleveldata.length - 1],
                    startTime: moment(this.index_time[0]).format('YYYY-MM-DD HH:mm:ss'),
                    endTime: moment(this.index_time[1]).format('YYYY-MM-DD HH:mm:ss'),
                    page: page,
                    pagesize: pagesize
                }
                axios.request({
                    url: '/tpm/SparepartRecord/SearchSparepartRecord',
                    data,
                    method: 'post'
                }).then(res => {
                    if (res.data.ret === 1) {
                        this.total = res.data.total
                        this.mockTableData1(res.data.data)
                        this.page = res.data.page
                    } else {
                        this.$Message.error(this.$t(res.data.msg))
                    }
                })
            })
        },
        // 查询结束
        // 渲染列表开始
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                var IO_type = ''
                if (item.type === 1) {
                    if (item.iO_type === 1) {
                        IO_type = this.$t('900688')
                    } else if (item.iO_type === 2) {
                        IO_type = this.$t('900687')
                    } else if (item.iO_type === 3) {
                        IO_type = this.$t('900686')
                    } else if (item.iO_type === 4) {
                        IO_type = this.$t('900685')
                    } else if (item.iO_type === 5) {
                        IO_type = this.$t('900684')
                    } else if (item.iO_type === 6) {
                        IO_type = this.$t('900683')
                    } else if (item.iO_type === 7) {
                        IO_type = this.$t('900682')
                    } else {
                        IO_type = ''
                    }
                } else if (item.type === 2) {
                    if (item.iO_type === 1) {
                        IO_type = this.$t('300067')
                    } else if (item.iO_type === 2) {
                        IO_type = this.$t('300069')
                    } else if (item.iO_type === 3) {
                        IO_type = this.$t('300070')
                    } else if (item.iO_type === 4) {
                        IO_type = this.$t('300071')
                    } else if (item.iO_type === 5) {
                        IO_type = this.$t('300072')
                    } else if (item.iO_type === 6) {
                        IO_type = this.$t('300073')
                    } else {
                        IO_type = ''
                    }
                }
                if (item) {
                    data.push({
                        record_code: item.record_code,
                        code: item.code,
                        name: item.stitle,
                        time: moment(item.create_time * 1000).format('YYYY-MM-DD HH:mm:ss'),
                        type: IO_type,
                        work_order: (item.order_code === '' ? '——' : item.order_code),
                        Leader: item.operate_name,
                        amount: item.number,
                        Unit_Price: (item.price === 0 ? '——' : item.price),
                        Total_price: item.number * item.price,
                        Stock: item.last_sku,
                        operator: item.uname,
                        remark: item.remark,
                        id: item.id,
                        mtitle: item.mtitle,
                        param: item.param
                    })
                }
            })
            this.tableData1 = data
        },
        changecondition () {
            this.morecondition = !this.morecondition
        },
        changePage (value) {
            this.page = value
            // The simulated data is changed directly here, and the actual usage scenario should fetch the data from the server
            this.tableData1 = this.search(this.page, this.pagesize)
        },
        handlePageSize (value) {
            this.pagesize = value
            this.tableData1 = this.search(this.page, this.pagesize)
        },
        loadexcel () {
            this.levelid = this.leveldata.slice(-1)
            var params = {
                id: this.levelid
            }
            axios.request({
                url: '/config/UserLevel/ListMachineIdArrEx',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.dataarr = res.data.data
                    this.dataarr.push(0)
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
                //  resolve();
            }).then(res => {
                var value1, value2
                if (this.typelistdata[0] === '1') {
                    value1 = this.typelistdata[1] // 出库
                    value2 = ''// 入库
                } else {
                    value1 = '' // 出库
                    value2 = this.typelistdata[1]// 入库
                }
                var data = {
                    keyword: this.keyword,
                    s_is_standard: this.Standard_parts,
                    s_is_substitute: this.substitution,
                    skutype: this.typelistdata[0],
                    inStockType: value2,
                    outStockType: value1,
                    link: this.Work_order,
                    machineIds: this.dataarr.join(','),
                    // sparepart: this.machineidarr,
                    startTime: this.index_time[0],
                    endTime: this.index_time[1]
                }
                axios.request({
                    url: '/tpm/SparepartRecord/ExportSparepartRecord',
                    data,
                    method: 'post'
                }).then(res => {
                    if (res.data.ret === 1) {
                        // console.log(1111)
                        window.location.href = axios.publicPath + '' + res.data.data
                    } else {
                        this.$Message.error(this.$t(res.data.msg))
                    }
                })
            })
        }

    }
}
</script>
<style lang="less" scoped>

</style>
