<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="del">{{$t('1004')}}</Button>
      </div>
    </div>
    <!-- 数据展示 -->
    <div class="divborder newTableDiv">
      <Table
        @on-current-change="handleRowChange"
        highlight-row
        :data="tableData"
        :columns="tableColumns"
        class="tableClass"
      ></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" v-model="addmodal" :styles="{width:'600px'}">
      <p slot="header">
            <span v-if="type===1">{{$t('1002')}}</span>
            <span v-if="type===2">{{$t('1003')}}</span>
        </p>
      <div class="modal_body">
          <div class="filter">
            <label id="级别名称"><i>*</i>{{$t("600590")}}：</label>
            <Input v-model="addobj.title" class="searchInput" type="text" id="input1"/>
          </div>
          <div class="filter">
            <label style="width:70px;" id="排序">{{$t("950093")}}
            </label>
            <input v-model="addobj.sorting" class="publicinput" type="number" min='0' id="input1" style="width:165px;" />
          </div>
          <div class="filter" style="display:flex;margin-bottom: 30px;">
            <label>{{$t("600540")}}：</label>
            <Input
              type="textarea"
              v-model="addobj.description"
              class="searchInput MaxWidth"
              :autosize="{minRows: 2,maxRows: 2}"
            />
          </div>
      </div>
      <div slot="footer">
        <Button type="text" size="large" @click="cancle" v-text="$t(1033)">取消</Button>
        <Button type="primary" v-if="type===1" size="large" @click="adddata(1)">{{$t('1002')}}</Button>
        <Button type="primary" v-if="type===2" size="large" @click="adddata(2)">{{$t('1003')}}</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import axios from '@/libs/api.request'
export default {
    data () {
        return {
            addmodal: false,
            total: 0,
            page: 1,
            pagesize: 10,
            obj: {},
            addobj: {},
            levelList: [],
            tableData: [],
            type: 0,
            tableColumns: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        )
                    },
                    width: 60
                },
                {
                    title: this.$t('800225'),
                    key: 'title',
                    width: 250
                },
                 {
                    title: this.$t('950093'),
                    key: 'sorting'
                },
                {
                    title: this.$t('100044'),
                    key: 'description'
                }
            ]
        }
    },
    created () {
        this.search()
        // 格式化表格样式
        this.tableColumns = this.$time.initTableTitle(this.tableColumns)
    },
    methods: {
        initLevel () {
            axios
                .request({
                    url: '/config/Level/getLevel',
                    methods: 'get'
                })
                .then(res => {
                    this.levelList = res.data.data
                })
        },
        initTableData (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        title: item.title,
                        description: item.description,
                        sorting: item.sorting,
                        id: item.id
                    })
                }
            })
            this.tableData = data
        },
        handdlesearch () {
            this.page = 1
            this.search()
        },
        search () {
            var params = { page: this.page, pageSize: this.pagesize }
            axios
                .request({
                    url: '/config/Level/getAllLevel',
                    params,
                    methods: 'get'
                })
                .then(res => {
                    this.total = res.data.total
                    this.initTableData(res.data.data)
                })
        },
        add () {
            this.type = 1
            this.addobj = {}
            this.initLevel()
            this.addmodal = true
        },
        edit () {
            if (JSON.stringify(this.obj) === '{}') {
                this.$Message.error(this.$t(1043))
                return false
            }
            var params = { id: this.obj.id }
            console.log(this.obj.parentid)
            this.addobj.parentid = this.obj.parentid
            axios
                .request({
                    url: '/config/Level/getIdLevel',
                    params,
                    methods: 'get'
                })
                .then(res => {
                    this.addobj = res.data.data
                })
            this.initLevel()
            this.addmodal = true
            this.type = 2
        },
        adddata (type) {
            let data = this.addobj
            // 添加
            if (type === 1) {
                axios
                    .request({
                        url: '/config/level/AddLevel',
                        data,
                        method: 'post'
                    })
                    .then(res => {
                        if (res.data.ret === 1) {
                            this.$Message.success(this.$t(1055))
                            this.page = 1
                            this.search()
                            this.addmodal = false
                        } else {
                            this.$Message.error(this.$t(res.data.msg))
                            this.search()
                        }
                    })
                // 修改
            } else if (type === 2) {
                axios
                    .request({
                        url: '/config/level/EditLevel',
                        data,
                        method: 'post'
                    })
                    .then(res => {
                        if (res.data.ret === 1) {
                            this.$Message.success(this.$t(1060))
                            this.page = 1
                            this.search()
                            this.addmodal = false
                        } else {
                            this.$Message.error(this.$t(res.data.msg))
                            this.search()
                        }
                    })
            }
        },
        del () {
            if (JSON.stringify(this.obj) === '{}') {
                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: () => {
                        var params = { id: this.obj.id }
                        axios
                            .request({
                                url: '/config/level/DeleteLevel',
                                params,
                                method: 'post'
                            })
                            .then(res => {
                                if (res.data.ret === 1) {
                                    this.$Message.success(this.$t('1058'))
                                    this.page = 1
                                    this.search()
                                } else {
                                    this.$Message.error(this.$t(res.data.msg))
                                    this.search()
                                }
                            })
                    }
                })
            }
        },
        cancle () {
            this.addobj = {}
            this.addmodal = false
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.obj = currentRow
        },
        handlePageSize (value) {
            this.pagesize = value
            this.tableData = this.search()
        },
        changePage (value) {
            this.page = value
            this.tableData = this.search()
        }
    }
}
</script>
<style lang="less" scoped>

</style>
