<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;">
        <div class="filter">
          <label>{{$t('920010')}}：</label>
           <Select v-model="materialType" label-in-value clearable  class="searchSelect" >
              <Option v-for="item in materiallist" :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,10)">{{$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 class="divborder newTableDiv">
      <Table
         class="tableClass"
        @on-current-change="handleRowChange"
        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>
      <Modal class="newModalClass" :title="title1" v-model="addmodal" :styles="{width:'600px'}">
        <div class="modal_body">
        <div class="filter">
            <label><i>*</i>{{this.$t('920145')}}：</label>
            <Input class="searchInput" type="text" v-model="addobj.name"/>
        </div>
        <div class="filter">
            <label >{{this.$t('920146')}}：</label>
            <Select v-model="addobj.pid" label-in-value clearable class="searchSelect">
              <Option v-for="item in materiallist" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </div>
        <div class="filter">
            <label><i>*</i>{{this.$t('920147')}}：</label>
            <Input class="searchInput" :disabled="type===1 ? false : 'disabled'" type="text" v-model="addobj.tcode"/>
        </div>
      </div>
      <div slot="footer">
            <Button type="text" size="large" @click="closeadd()">{{$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>
  </div>
</template>
<script>
import axios from '@/libs/api.request'
export default {
    name: 'route',
    data () {
        return {
            materialType: '0',
            materiallist: [],
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            addmodal: false,
            addobj: {},
            detailobj: {},
            title1: '',
            type: 0,
            tableColumns1: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        )
                    },
                    width: '90px'
                },
                {
                    title: this.$t('920145'),
                    key: 'name'
                },
                {
                    title: this.$t('920146'),
                    key: 'pid_value'
                },
                {
                    title: this.$t('920147'),
                    key: 'tcode'
                }
            ]
        }
    },
    created () {
        this.inimateriallist()
        this.search(1, 10)
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        inimateriallist () {
            var data = {
                tb: 'siger_tr_material_type',
                icol: 'id',
                vcol: 'name'
            }
            axios.request({
                url: '/config/Dict/FillOption',
                data,
                method: 'post'
            })
                .then(res => {
                    this.materiallist = res.data.data
                })
        },
        search (page, pagesize) {
            var params = {
                name: this.materialType,
                page: page,
                pagesize: pagesize
            }
            axios.request({
                url: '/config/MaterialType/getList',
                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({
                        tcode: item.tcode,
                        name: item.name,
                        pid_value: item.pid_value,
                        pid: item.pid,
                        id: item.id
                    })
                }
            })
            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.addobj = {}
            this.type = 1
            this.title1 = this.$t('1002')
            this.addmodal = true
        },
        closeadd () {
            this.addmodal = false
        },
        adddata (type) {
            if (this.addobj.tcode === '' || this.addobj.name === '' || this.addobj.parent === '0') {
                this.$Message.error(this.$t('600652'))
                return false
            }
            if (type === 1) {
                this.addobj.id = ''
                let data = this.addobj
                axios.request({
                    url: '/config/MaterialType/Add',
                    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)
                        this.inimateriallist()
                    } else {
                        this.$Message.error(this.$t(res.data.msg))
                    }
                })
            } else if (type === 2) {
                let data = this.addobj
                axios.request({
                    url: '/config/MaterialType/Modify',
                    data,
                    method: 'post'
                }).then(res => {
                    if (res.data.ret === 1) {
                        this.$Message.success(this.$t('1060'))
                        this.addmodal = false
                        this.search(this.page, this.pagesize)
                    } else {
                        this.$Message.error(this.$t(res.data.msg))
                    }
                })
            }
        },
        dele () {
            if (this.detailobj === undefined || this.detailobj.id === 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: () => {
                        axios.request({
                            url: '/config/MaterialType/Delete?id=' + this.detailobj.id,
                            method: 'get'
                        }).then(res => {
                            if (res.data.ret === 1) {
                                this.$Message.success(this.$t('1058'))
                                this.page = 1
                                this.search(this.page, this.pagesize)
                                this.detailobj = {}
                            } else {
                                this.$Message.error(this.$t(res.data.msg))
                            }
                        })
                    }
                })
            }
        },
        edit () {
            this.addmodal = true
            this.type = 2
            this.title1 = this.$t('1003')
            this.addobj = this.detailobj
        }

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

</style>
