<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;">
      <div>
        <div class="filter">
          <label >{{$t('600648')}}：</label>
          <Select v-model="categoryid" label-in-value clearable class="searchSelect" >
              <Option v-for="item in categorys" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </div>
         <div class="searchBtn">
        <Button type="primary" class="twoWord" icon="ios-search" @click="search(1,pagesize)">{{$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" icon="md-close-circle"  @click="dele()">{{$t('1004')}}</Button>
         </div>
      </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"
            @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>
            <div class="filter">
                <label >{{$t('920146')}}：</label>
                <Select v-model="editobj.parentid" label-in-value clearable class="searchSelect" >
                    <Option v-for="item in categorys" :value="item.id" :key="item.id">{{ item.name }}</Option>
                </Select>
            </div>
            <div class="filter">
                <label><i>*</i>{{$t('1950531')}}：</label>
                <Input class="searchInput" type="text" v-model="editobj.name" />
            </div>
        </div>
      </div>
      <div slot="footer">
        <Button type="text" size="large" @click="cancle">{{$t('1033')}}</Button>
        <Button type="primary" v-if="type===1" size="large" @click="adddata">{{$t('1002')}}</Button>
        <Button type="primary" v-if="type===2" 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
                        )
                    }
                },
                {
                    title: this.$t('1950531'),
                    key: 'name'
                },
                {
                    title: this.$t('920146'),
                    key: 'pname'
                }
            ],
            editobj: {},
            categorys: [],
            categoryid: 0,
            type: 0,
            selectids: []
        }
    },
    created () {
        this.search(1, 10)
        this.initcategorys()
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        search (page, pagesize) {
            var params = {
                page: page,
                pagesize: pagesize,
                id: this.categoryid
            }
            axios
                .request({
                    url: '/qms/InspectCategory/GetList',
                    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.type = 0
            this.search(this.page, this.pagesize)
        },
        add () {
            this.title1 = this.$t('1002')
            this.type = 1
            this.editobj = {}
            this.editmodal = true
            this.editobj.parentid = 0
        },
        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 = {}
            this.editobj = this.obj
            this.editmodal = true
            this.type = 2
            this.editobj = this.obj
        },
        adddata () {
            var data = this.editobj
            axios
                .request({
                    url: '/qms/InspectCategory/Add',
                    data,
                    method: 'post'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        this.$Message.success(this.$t('1055'))
                        this.search(this.page, this.pagesize)
                        this.editmodal = false
                        this.initcategorys()
                    } else {
                        this.$Message.error(this.$t(res.data.msg))
                    }
                })
        },
        editdata () {
            let data = this.editobj
            data.id = this.obj.id
            axios.request({
                url: '/qms/InspectCategory/Modify',
                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
                    this.initcategorys()
                } 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('1018'),
                    content: '',
                    width: '290px',
                    closable: true,
                    okText: this.$t('1004'),
                    onOk: () => {
                        let parameter = {
                            ids: this.selectids
                        }
                        axios.request({
                            url: '/qms/InspectCategory/Delete',
                            data: parameter,
                            method: 'post'
                        }).then(res => {
                            if (res.data.ret === 1) {
                                this.initcategorys()
                                this.page = 1
                                this.selectids = []
                                this.search(this.page, this.pagesize)
                                this.$Message.success(this.$t('1058'))
                            } else {
                                this.$Message.error(this.$t(res.data.msg))
                            }
                        })
                    }
                })
            }
        },
        initcategorys () {
            axios.request({
                url: '/qms/InspectCategory/GetNameList',
                method: 'get'
            }).then(res => {
                this.categorys = res.data.data
            })
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        id: item.id,
                        pname: item.pname,
                        parentid: item.pid,
                        name: item.name
                    })
                }
            })
            this.tableData1 = data
        },
        changecondition () {
            this.morecondition = !this.morecondition
        },
        handlePageSize (value) {
            this.pagesize = value
            this.tableData1 = this.search(1, this.pagesize)
        },
        changePage (value) {
            this.page = value
            this.tableData1 = this.search(this.page, this.pagesize)
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.obj = 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)
                }
            }
        }
    }
}

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

</style>
