<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;">
      <div>
        <div class="filter">
          <label >{{$t('1950491')}}：</label>
          <Cascader
            :data="datalevel"
            :value="leveldata"
            trigger="click"
            change-on-select
            @on-change="handlelevelchange"
            v-model="sectionid"
            class="searchSelect"
          ></Cascader>
        </div>
        <div class="filter">
          <label >{{$t('1950541')}}：</label>
          <Select v-model="machineid" label-in-value clearable class="searchSelect" v-show="showMachine">
            <Option v-for="item in machines" :value="item.id" :key="item.id">{{ item.title }}</Option>
          </Select>
        </div>
      </div>
      <div class="searchBtn">
          <Button type="primary" class="twoWord" icon="ios-search" @click="search(1,pagesize)">{{$t('1001')}}</Button>
          <Button type="warning" class="twoWord" icon="ios-create-outline"  @click="edit()">{{$t('1003')}}</Button>
          <Button type="error" class="twoWord" icon="ios-bulb-outline"  @click="dele()">{{$t('1950542')}}</Button>
          <Button type="warning" class="twoWord intervalRight" icon="md-bulb"  @click="enable()">{{$t('800212')}}</Button>
          <Button type="primary" class="fourWord" icon="ios-cloud-download" @click="downexcel()">{{$t('1007')}}</Button>
      </div>
    </div>
    <div class="divborder newTableDiv">
      <Table
       class="tableClass"
        width='100%'
        @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"
            @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='title1' v-model="editmodal" :styles="{width:'400px'}">
      <div class="modal_body" >
          <div class="filter">
              <label style="width:120px!important;"><i>*</i>{{$t('1950543')}}：</label>
              <InputNumber class="searchInput" type="text" v-model="editobj.frequency" />
          </div>
      </div>
      <div slot="footer">
        <Button type="text" size="large" @click="cancle">{{$t('1033')}}</Button>
        <Button type="primary" size="large" @click="editdata">{{$t('1003')}}</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import axios from '@/libs/api.request'

export default{
    data () {
        return {
            headers: {
                token: localStorage.getItem('token'),
                timestamp: localStorage.getItem('timestamp'),
                nonce: localStorage.getItem('nonce'),
                sign: localStorage.getItem('sign')
            },
            title1: this.$t('1002'),
            tableData1: [],
            editmodal: false,
            page: 1,
            pagesize: 10,
            total: 0,
            tableColumns1: [
                {
                    type: 'selection',
                    width: 50,
                    align: 'center'
                },
                {
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        )
                    },
                    width: 70
                },
                {
                    title: this.$t('1950541'),
                    key: 'machinename'
                },
                {
                    title: this.$t('1950543'),
                    key: 'frequency_value'
                },
                {
                    title: this.$t('920139'),
                    key: 'updator'
                },
                {
                    title: this.$t('920138'),
                    key: 'updatetime'
                },
                {
                    title: this.$t('200042'),
                    key: 'status_value'
                }
            ],
            editobj: {},
            machines: [],
            machineid: 0,
            datalevel: [],
            leveldata: [],
            sectionid: [],
            showMachine: true,
            selectids: []
        }
    },
    created () {
        this.search(1, 10)
        this.initlevel()
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        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
            })
        },
        search (page, pagesize) {
            var params = {
                page: page,
                pagesize: pagesize,
                machineid: this.machineid,
                toexcel: 0
            }
            axios
                .request({
                    url: '/qms/StandardParts/GetPageList',
                    params,
                    method: 'get'
                })
                .then(res => {
                    this.page = page
                    this.pagesize = pagesize
                    this.total = res.data.total
                    this.mockTableData1(res.data.data)
                })

            this.obj = {}
        },
        cancle () {
            this.editobj = {}
            this.editmodal = false
            this.search(this.page, this.pagesize)
        },
        edit () {
            if (JSON.stringify(this.obj) === '{}' || this.obj === null || this.obj === undefined) {
                this.$Message.error(this.$t('1043'))
                return false
            }
            this.title1 = this.$t('1003')
            this.editobj.frequency = this.obj.id > 0 ? this.obj.frequency : null
            this.editmodal = true
        },
        editdata () {
            let data = {
                machineid: this.obj.machineid,
                frequency: this.editobj.frequency
            }
            axios.request({
                url: '/qms/StandardParts/Update',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.$Message.success(this.$t('1060'))
                    this.search(this.page, this.pagesize)
                    this.editmodal = false
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        dele () {
            if (this.selectids === undefined || this.selectids === null || this.selectids === []) {
                this.$Message.error((this.$t('1017')))
                return false
            } else {
                this.$Modal.confirm({
                    title: this.$t('1124'),
                    content: this.$t('1950544'),
                    onOk: () => {
                        let parameter = {
                            machineIds: this.selectids
                        }
                        axios.request({
                            url: '/qms/StandardParts/Delete',
                            data: parameter,
                            method: 'post'
                        }).then(res => {
                            if (res.data.ret === 1) {
                                this.selectids = []
                                this.search(this.page, this.pagesize)
                                this.$Message.success(this.$t('1'))
                            } else {
                                this.$Message.error(this.$t(res.data.msg))
                            }
                        })
                    }
                })
            }
        },
        handleSelectRow: function (data) {
            this.selectids = []
            for (var i in data) {
                if (this.selectids.indexOf(data[i].machineid) < 0) {
                    this.selectids.push(data[i].machineid)
                }
            }
        },
        enable () {
            if (this.selectids === undefined || this.selectids === null || this.selectids === []) {
                this.$Message.error((this.$t('1017')))
                return false
            } else {
                this.$Modal.confirm({
                    title: this.$t('1124'),
                    content: this.$t('1950545'),
                    onOk: () => {
                        let parameter = {
                            machineIds: this.selectids
                        }
                        axios.request({
                            url: '/qms/StandardParts/Enable',
                            data: parameter,
                            method: 'post'
                        }).then(res => {
                            if (res.data.ret === 1) {
                                this.selectids = []
                                this.search(this.page, this.pagesize)
                                this.$Message.success(this.$t('1'))
                            } else {
                                this.$Message.error(this.$t(res.data.msg))
                            }
                        })
                    }
                })
            }
        },
        initmachines (sectionid) {
            axios.request({
                url: '/qms/StandardParts/GetAllMachines?sectionid=' + sectionid,
                method: 'get'
            }).then(res => {
                this.machines = res.data.data
                showMachine = false
                showMachine = true
            })
        },
        handlelevelchange (value) {
            this.leveldata = value
            var sectionid = value[value.length - 1]
            this.initmachines(sectionid)
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        id: item.id,
                        machineid: item.machineid,
                        machinename: item.machinename,
                        frequency: item.frequency,
                        frequency_value: item.id > 0 ? item.frequency : '',
                        updator: item.updator,
                        updatetime: item.updatetime,
                        status: item.status,
                        status_value: item.status == 1 ? this.$t('300030') : this.$t('300031')
                    })
                }
            })
            this.tableData1 = data
        },
        downexcel () {
            var params = {
                machineid: this.machineid
            }
            axios.request({
                url: '/qms/StandardParts/GetPageList?toexcel=1',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    window.location.href = axios.publicPath + '' + res.data.data
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            })
        },
        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
        }
    }
}

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

</style>
