<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;">
      <div class="searchBtn">
          <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" icon="md-close-circle" @click="dele()">{{$t('1004')}}</Button>
      </div>
    </div>
    <div class="divborder newTableDiv">
      <Table
         class="tableClass"
        @on-current-change="handleRowChange"
        highlight-row
        :data="tableData1"
        @on-selection-change="handleSelectRow"
        :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>
      <Modal class="newModalClass" :title="$t('1002')" v-model="addmodal" :styles="{width:'700px'}">
        <div class="modal_body" >
        <div class="filter">
            <label><i>*</i>{{this.$t('300050')}}：<span></span></label>
            <Input class="searchInput" type="number" min="1" v-model="addcode" />
        </div>
        <div class="filter">
            <label><i>*</i>{{this.$t('1950496')}}：<span></span></label>
            <Input class="searchInput" type="text" v-model="addname" />
        </div>
        <div class="filter">
            <label><i>*</i>{{this.$t('1950518')}}：<span></span></label>
            <Input class="searchInput" type="number" min="0" v-model="addcount" />
        </div>
      </div>
      <div slot="footer">
            <Button type="text" size="large" @click="closeadd()">{{$t('1033')}}</Button>
            <Button type="primary" size="large" @click="adddata()">{{$t('1011')}}</Button>
        </div>
      </Modal>
      <Modal class="newModalClass" :title="$t('1003')" v-model="editmodal" :styles="{width:'700px'}" @on-ok="editdata">
        <div class="modal_body">
        <div class="filter">
            <label ><i>*</i>{{this.$t('1950496')}}：<span></span></label>
            <Input class="searchInput" type="text" id="input1" v-model="editobj.editname" />
        </div>
        <div class="filter">
            <label ><i>*</i>{{this.$t('1950518')}}：<span></span></label>
            <Input class="searchInput" type="text" id="input1" v-model="editobj.editcount" />
        </div>
      </div>
      </Modal>
    </div>
  </div>
</template>
<script>
import axios from '@/libs/api.request'
export default {
    name: 'productmanagement', // 组件不刷新
    data () {
        return {
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            addmodal: false,
            detailobj: {},
            editmodal: false,
            editobj: {},
            addcode: '',
            addname: '',
            addcount: '',
            editcode: '',
            editname: '',
            editcount: '',
            selectids: [],
            tableColumns1: [
                {
                    type: 'selection',
                    width: 50,
                    align: 'center'
                },
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        )
                    }
                },
                {
                    title: this.$t('300050'),
                    key: 'type_code'
                },
                {
                    title: this.$t('1950496'),
                    key: 'type_name'
                },
                {
                    title: this.$t('1950518'),
                    key: 'check_count'
                }
            ]
        }
    },
    created () {
        this.search(1, 10)
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        handleSelectRow: function (data) {
            this.selectids = []
            for (var i in data) {
                this.selectids.push(data[i].id)
            }
        },
        search (page, pagesize) {
            var params = {
                page: page,
                pagesize: pagesize
            }
            axios
                .request({
                    url: '/qms/TypeCount/GetTypeCounts',
                    params,
                    method: 'get'
                })
                .then(res => {
                    this.total = res.data.total
                    this.mockTableData1(res.data.data)
                })
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        type_code: item.type_code,
                        type_name: item.type_name,
                        id: item.id,
                        check_count: item.check_count
                    })
                }
            })
            this.tableData1 = data
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.detailobj = currentRow
        },
        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)
        },
        add () {
            this.addmodal = true
            this.addcode = ''
            this.addname = ''
            this.addcount = ''
        },
        closeadd () {
            this.addmodal = false
        },
        adddata () {
            var code = this.addcode
            var name = this.addname.trim()
            var count = this.addcount

            if (code === '' || name === '' || count === '') {
                this.$Message.error(this.$t('600652'))
                return false
            }
            let data = {
                code: code,
                name: name,
                count: count
            }
            axios.request({
                url: '/qms/TypeCount/AddTypeCount',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.$Message.success(this.$t('1055'))
                    this.addmodal = false
                    this.search(this.page, this.pagesize)
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        dele () {
            if (this.selectids === undefined || this.selectids === '' || this.selectids === [] || this.selectids.length == 0) {
                this.$Message.error((this.$t('1017')))
                return false
            } else {
                var ids = []
                this.selectids.forEach(ele => {
                    ids.push(ele)
                })
                var data = {
                    ids: ids
                }
                this.$Modal.confirm({
                    title: this.$t('1018'),
                    content: '',
                    width: '290px',
                    closable: true,
                    okText: this.$t('1004'),
                    onOk: () => {
                        axios.request({
                            url: '/qms/TypeCount/DeleteTypeCounts',
                            data: data,
                            method: 'post'
                        }).then(res => {
                            if (res.data.ret === 1) {
                                this.$Message.success(this.$t('1058'))
                                this.page = 1
                                this.search(1, this.pagesize)
                                this.detailobj = {}
                            } else {
                                this.$Message.error(this.$t(res.data.msg))
                            }
                        })
                    }
                })
            }
        },
        edit () {
            if (this.detailobj === undefined || this.detailobj.id === undefined) {
                this.$Message.error((this.$t('1017')))
                return false
            }
            this.editmodal = true
            this.editobj.editcode = this.detailobj.type_code
            this.editobj.editname = this.detailobj.type_name
            this.editobj.editcount = this.detailobj.check_count
        },
        editdata () {
            var name = this.editobj.editname
            var count = this.editobj.editcount
            if (name === '' || count === '') {
                this.$Message.error(this.$t('600652'))
                return false
            }
            let data = {
                name: name,
                count: count,
                id: this.detailobj.id
            }
            axios.request({
                url: '/qms/TypeCount/UpdateTypeCount',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.$Message.success(this.$t('1060'))
                    this.editmodal = false
                    this.search(this.page, this.pagesize)
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        }
    }
}
</script>
<style lang="less" scoped>

</style>
