<template>
  <div class="testdiv">
    <!-- 按钮 -->
    <div class="divborder newSearchDiv" style="margin:0px;">
      <div class="filter">
        <label>{{$t('600539')}}：</label>
        <Select
          v-model="projectid"
          label-in-value
          clearable
          class="searchSelect"
          @on-change="handdlesearch()"
        >
          <Option v-for="item in projectList" :value="item.id" :key="item.id">{{ item.title }}</Option>
        </Select>
      </div>
      <div class="searchBtn">
        <Button type="primary" class="twoWord" icon="md-add-circle" @click="add">{{$t('1002')}}</Button>
        <!-- TODO 无法修改 -->
        <!-- <Button type="primary" icon="ios-build"  @click="edit">{{$t('1003')}}</Button> -->
        <Button type="error" class="sixWord" icon="md-close-circle" @click="del">{{$t('900601')}}</Button>
      </div>
    </div>
    <!-- 数据展示 -->
    <div class="divborder newTableDiv">
      <Table
        class="tableClass"
        @on-current-change="handleRowChange"
        highlight-row
        :data="tableData"
        :columns="tableColumns"
      ></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 :title="$t(1002)" v-model="addmodal" :styles="{width:'500px'}" class="newModalClass">
      <div class="modal_body">
          <div class="filter">
            <label><i>*</i>{{$t(500175)}}：</label>
            <Select v-model="addobj.projectid" label-in-value clearable class="searchSelect">
              <Option
                v-for="item in projectListWithout"
                :value="item.id"
                :key="item.id"
              >{{ item.title }}</Option>
            </Select>
          </div>
          <div class="filter">
            <label><i>*</i>{{$t(500176)}}：</label>
            <Table
                highlight-row
                :data="tableData1"
                :columns="tableColumns1"
                :max-height="396"
                @on-selection-change="getProjectList"
            ></Table>
            <!-- <CheckboxGroup v-model="addobj.levelarr">
              <Checkbox v-for="item in mainLevel" :key="item.id" :label="item.title"></Checkbox>
            </CheckboxGroup> -->
          </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)" v-text="$t(1002)">添加</Button>
        <Button type="primary" v-if="type===2" size="large" @click="adddata(2)" v-text="$t(1003)">修改</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import axios from '@/libs/api.request'
export default {
    data () {
        return {
            addmodal: false,
            projectid: 1,
            total: 0,
            page: 1,
            pagesize: 10,
            obj: {},
            addobj: {},
            projectList: [],
            projectListWithout: [],
            mainLevel: [],
            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('100044'),
                    key: 'description'
                }
            ],
            tableColumns1:[
                {
                    type: 'index',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + 1
                        )
                    },
                    width: 60
                },
                {
                    type:'selection',
                    width:80
                },
                {
                    title:this.$t('800225'),
                    key:'title',
                    width:250
                }
            ],
            tableData1:[],
            selection:[],
        }
    },
    created () {
        this.search()
        this.initProject()
        // 格式化表格样式
        this.tableColumns = this.$time.initTableTitle(this.tableColumns);
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        getProjectList(e){
            this.selection = e;
        },
        initProject () {
            axios
                .request({
                    url: '/config/Project/getProjectLists?id=1',
                    methods: 'get'
                })
                .then(res => {
                    this.projectList = res.data.data
                })
        },
        initProjectList () {
            axios
                .request({
                    url: '/config/ProjectLevel/getProjectListsWithout',
                    methods: 'get'
                })
                .then(res => {
                    this.projectListWithout = res.data.data
                })
        },
        getMainLevelLists () {
            axios
                .request({
                    url: '/config/ProjectLevel/getMainLevelLists',
                    methods: 'get'
                })
                .then(res => {
                    // this.mainLevel = res.data.data
                    let data = res.data.data;
                    this.tableData1 = [];
                    if(res.data.ret===1 && Array.isArray(data)&& data.length > 0){
                        this.tableData1 = res.data.data;
                    }
                })
        },
        initTableData (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        title: item.title,
                        description: item.description,
                        id: item.id
                    })
                }
            })
            this.tableData = data
        },
        handdlesearch () {
            this.page = 1
            this.search()
        },
        search () {
            var params = {
                page: this.page,
                pageSize: this.pagesize,
                id: this.projectid
            }
            this.selection = []
            axios
                .request({
                    url: '/config/ProjectLevel/getProjectLevelLists',
                    params,
                    methods: 'get'
                })
                .then(res => {
                    this.total = res.data.total
                    this.initTableData(res.data.data)
                })
        },
        add () {
            this.type = 1
            this.addobj = {}
            this.initProjectList()
            this.getMainLevelLists()
            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) {
            var levelarr = []

            if (
                this.addobj.projectid == undefined ||
        this.selection.length < 1
            ) {
                this.$Message.error(this.$t('9000136'))
                return false
            } 

            let data = {
                projectid: this.addobj.projectid,
                levelarr: this.selection
            }

            // 添加
            if (type === 1) {
                axios
                    .request({
                        url: '/config/ProjectLevel/addCustomLevel',
                        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()
                        }
                    })
            }
        },
        del () {
            if (this.projectid === undefined) {
                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.projectid }
                        axios
                            .request({
                                url: '/config/ProjectLevel/deleteCustomLevel',
                                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()
                                }
                            })
                    }
                })
            }
        },
        findidbyname (name) {
            var data = this.mainLevel
            var ret = 0
            data.forEach(element => {
                if (element.title === name) {
                    ret = element.id
                }
            })
            return ret
        },
        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>
