<template>
    <div>
        <div class="divborder newSearchDiv" style="margin:0px;">
            <div class="filter">
                <label >{{$t('1132')}}：</label>
                <Cascader :data="datalevel" :value="leveldata" @on-change="handlelevelchange" trigger="click" change-on-select class="searchSelect middleWidth"></Cascader>
            </div>
            <div class="searchBtn">
                <Button type="primary" class="twoWord" icon="ios-search" @click="tosearch()">{{$t('1001')}}</Button>
                <Button type="success" class="twoWord" icon="md-add-circle" @click="add()">{{$t('1002')}}</Button>
                <Button type="warning" class="twoWord" icon="ios-create-outline" @click="edit()">{{$t('1003')}}</Button>
                <Button
                    type="error"
                    class="twoWord intervalRight"
                    icon="md-close-circle"
                    @click="del()"
                >{{$t('1004')}}
                </Button>
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                @on-current-change="handleRowChange"
                @on-selection-change="handleSelectRow"
                highlight-row
                :data="tableData1"
                :columns="tableColumns1"
            ></Table>
            <div class="pageDiv">
                <div class="pageDirection">
                <Page
                    :total="total"
                    :current="page"
                    :page-size="pagesize"
                    @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>

        <!-- 添加弹窗 -->
        <Modal class="newModalClass" :title="modaltitle" v-model="addmodal" :styles="{width:'500px'}">
            <div  class="modal_body">
                <div class="filter">
                    <label><i>*</i>{{$t('1132')}}：</label>
                    <Cascader :data="datalevel" :value="addobj.leveldata" @on-change="handlelevelchange1" trigger="click" change-on-select class="searchSelect middleWidth"></Cascader>
                </div>
                <div class="filter">
                    <label><i>*</i>{{$t('5500296')}}：</label>
                    <RadioGroup v-model="addobj.printtype">
                        <Radio label="COM"></Radio>
                        <Radio label="IP"></Radio>
                        <Radio label="USB"></Radio>
                        <Radio label="LPT"></Radio>
                    </RadioGroup>
                </div>
                <div class="filter">
                    <label><i>*</i>{{$t('5500297')}}：</label>
                    <Input class="searchInput" type="text" v-model="addobj.partnumber"/>
                </div>
                <div class="filter">
                    <label>{{$t('5500298')}}：</label>
                    <Input class="searchInput" type="text" v-model="addobj.printname"/>
                </div>
            </div>
            <div slot="footer">
                <Button type="text" size="large" @click="addmodal=false">{{$t('1033')}}</Button>
                <Button type="primary" size="large" @click="doPost()">{{$t('1011')}}</Button>
            </div>
        </Modal>

    </div>
</template>

<script>
import axios from "@/libs/api.request";
export default {
    data() {
        return {
            modaltitle:this.$t('1002'),
            addmodal:false,
            addobj:{
                leveldata:[],
                printtype:'COM',
                partnumber:'',
                printname:''
            },
            leveldata: [],
            datalevel: [],
            sectionid:0,
            tableColumns1: [
                {
                    type: "selection",
                    width: 50,
                    align: "center",
                },
                {
                    type: "index2",
                    title: this.$t(1008),
                    key: "xuhao",
                    width: 70,
                    render: (h, params) => {
                        return h(
                        "span",
                        params.index + (this.page - 1) * this.pagesize + 1
                    );
                },
                },
                {
                    title: this.$t('1132'),
                    key: "station_value",
                    render:(h,params)=>{
                        let station = params.row.station_value
                        let station_str = ''
                        if(station!=""&&station!=null&&station!=undefined&&station.length > 0){
                            station_str = station.join('-')
                        }
                        return h('span',
                            station_str
                        )
                    }
                },
                {
                    title: this.$t('5500296'),
                    key: "printtype",
                },
                {
                    title: this.$t('5500297'),
                    key: "port",
                },
                {
                    title: this.$t('5500298'),
                    key: "printername"
                },
                {
                    title: this.$t('920139'),
                    key: "uid_value",
                },
                {
                    title: this.$t('920138'),
                    key: "transdatetime",
                },
            ],
            tableData1: [],
            page: 1,
            pagesize: 10,
            total: 0,
            funtype:1,//1添加 2编辑
            selectids:[]
        }
    },
    created(){
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
        this.initlevel()
        this.search()
    },
    methods:{
        initlevel () {
            axios.request({
                url: '/config/ProjectLevelSection/GetSectionTree',
                method: 'get'
            }).then(res => {
                const arr = []
                this.datalevel = res.data.data
                this.leveldata = arr
            })
        },
        handlelevelchange (value) {
            this.leveldata = value
        },
        handlelevelchange1 (value) {
            this.addobj.leveldata = value
        },
        search(){
            this.page = 1
            this.pagesize = 10
            this.tosearch()
        },
        tosearch(){
            let params={
                station:this.leveldata.length===0?0:this.leveldata[this.leveldata.length - 1],
                page:this.page,
                pagesize:this.pagesize
            };
            this.detailobj = {}
            this.selectids = []
            axios.request({
                url: '/acc/PrintSetting/getList',
                params,
                method: 'get'
            }).then(res => {
                this.total = 0
                this.tableData1 = []
                if(res.data.ret===1 && res.data.data!=null && res.data.data.length >0){
                    this.total = res.data.total
                    this.tableData1 = res.data.data
                }
            })
        },
        add(){
            this.addmodal = true
            this.modaltitle = this.$t('1002')
            this.funtype = 1 //添加
            this.addobj = {
                leveldata:[],
                printtype:'COM',
                partnumber:'',
                printname:''
            }
        },
        edit(){
            if(this.detailobj===undefined ||this.detailobj.id===undefined){
                this.$Message.error(this.$t('1017'))
                return false;
            }
            this.addmodal = true
            this.modaltitle = this.$t('1003')
            this.funtype = 2 //编辑
            let section = []
            let station = this.detailobj.parentids
            if(station!=null&&station!=""&&station!=undefined){
                section = station
            }
            this.addobj = {
                leveldata:section,
                printtype:this.detailobj.printtype,
                partnumber:this.detailobj.port,
                printname:this.detailobj.printername!=null?this.detailobj.printername:''
            }
        },
        doPost(){
            if(this.addobj.leveldata===undefined||this.addobj.leveldata.length ===0){
                this.$Message.error(this.$t('1037'))
                return false;
            }
            if(this.addobj.partnumber===""||this.addobj.partnumber===undefined||this.addobj.partnumber===null){
                this.$Message.error(this.$t('5500299'))
                return false;
            }
            var url = ''
            if(this.funtype ===1){
                url = '/acc/PrintSetting/add'
            }
            else{
                url = '/acc/PrintSetting/Modify'
            }
            var sectionid = this.addobj.leveldata[this.addobj.leveldata.length -1];
            var data = {
                id:this.funtype===1?"":this.detailobj.id,
                station:sectionid,
                printtype:this.addobj.printtype,
                port:this.addobj.partnumber,
                printername:this.addobj.printname
            }
            axios.request({
                url: url,
                data,
                method: 'post'
            }).then(res => {
                if(this.funtype ===1){
                    if(res.data.ret === 1){
                        this.addmodal = false
                        this.$Message.success(this.$t('1055'))
                        this.search()
                    }
                    else
                    {
                        this.$Message.success(this.$t(res.data.msg))
                    }
                }
                else
                {
                    if(res.data.ret === 1){
                        this.addmodal = false
                        this.$Message.success(this.$t('1060'))
                        this.tosearch()
                    }
                    else
                    {
                        this.$Message.success(this.$t(res.data.msg))
                    }
                }
            })
        },
        del(){
            if(this.selectids.length === 0){
                this.$Message.error(this.$t('1017'));
                return false;
            }
            this.$Modal.confirm({
                title: this.$t('1018'),
                content: '',
                width: '290px',
                closable: true,
                okText: this.$t('1004'),
                onOk: () => {
                axios
                    .request({
                        url: '/acc/PrintSetting/Delete?id='+this.selectids.join(),
                        method: 'get'
                    })
                    .then(res => {
                        if (res.data.ret === 1) {
                            this.$Message.success(this.$t('1058'))
                            this.search()
                        } else {
                            this.$Message.error(this.$t(res.data.msg))
                        }
                    })
                }
            })
        },
        handleRowChange(currentRow, oldCurrentRow) {
            this.detailobj = currentRow;
        },
        handleSelectRow: function (data) {
            this.selectids = [];
            for (var i in data) {
                if (this.selectids.indexOf(data[i].id) < 0) {
                this.selectids.push(data[i].id);
                }
            }
        },
        changePage(value) {
            this.page = value;
            this.tosearch();
        },
        handlePageSize(value) {
            this.pagesize = value;
            this.tosearch();
        },
    }
}
</script>

<style lang="less" scoped>

</style>
