<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;">
      <div class="filter">
        <label >{{$t('1950645')}}：</label>
        <Select v-model="bussineseid" label-in-value clearable filterable class="searchSelect">
            <Option v-for="item in bussineses" :value="item.id" :key="item.id">{{ item.name }}</Option>
        </Select>
      </div>
      <div class="filter">
        <label >{{$t('950238')}}：</label>
        <Select v-model="userid" label-in-value clearable filterable class="searchSelect">
            <Option v-for="item in users" :value="item.mid" :key="item.mid">{{ item.name }}</Option>
        </Select>
      </div>
      <div class="searchBtn">
        <Button type="primary" class="twoWord" icon="ios-search" @click="tosearch()">{{$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"
        @on-selection-change="handleSelectRow"
      ></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" v-model="addmodal" :styles="{width:'400px'}">
        <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 ><i>*</i>{{$t('600011')}}：</label>
              <Select v-model="addobj.bussineseid" label-in-value clearable filterable  class="searchSelect" >
                  <Option v-for="item in addbussineses" :value="item.id" :key="item.id">{{ item.name }}</Option>
              </Select>
            </div>
            <div class="filter">
              <label ><i>*</i>{{$t('100046')}}：</label>
              <Select v-model="addobj.userid" label-in-value clearable filterable  class="searchSelect" >
                  <Option v-for="item in addusers" :value="item.mid" :key="item.mid">{{ item.name }}</Option>
              </Select>
            </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 {
            headers: {
                token: localStorage.getItem('token'),
                timestamp: localStorage.getItem('timestamp'),
                nonce: localStorage.getItem('nonce'),
                sign: localStorage.getItem('sign')
            },
            obj: [],
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            addmodal: false,
            addobj: {},
            type: 0,
            userid: 0,
            bussineseid: 0,
            users: [],
            addusers: [],
            bussineses: [],
            addbussineses: [],
            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
                        )
                    },
                    width: 70
                },
                {
                    title: this.$t('9800001'),
                    key: 'name',
                    minWidth: 120
                },
                {
                    title: 'Duns',
                    key: 'duns',
                    minWidth: 120
                },
                {
                    title: this.$t('100046'),
                    key: 'username',
                    minWidth: 120
                },
                {
                    title: this.$t('900273'),
                    key: 'mobile',
                    minWidth: 120
                },
                {
                    title: this.$t('950110'),
                    key: 'workcode',
                    minWidth: 120
                },
                {
                    title: this.$t('920139'),
                    key: 'updator',
                    minWidth: 120
                },
                {
                    title: this.$t('920138'),
                    key: 'updatetime',
                    minWidth: 150
                }
            ]
        }
    },
    created () {
        this.initusers()
        this.initsuppliers()
        this.search()
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        initusers () {
            axios
                .request({
                    url: '/config/ProjectUser/GetAllUserByCurrentProjectId',
                    method: 'get'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        this.users = res.data.data
                        this.addusers = res.data.data
                    }
                })
        },
        initsuppliers () {
            axios
                .request({
                    url: '/config/Business/GetSupplierNameList?type=1',
                    method: 'get'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        this.bussineses = res.data.data
                        this.addbussineses = res.data.data
                    }
                })
        },
        tosearch () {
            this.page = 1
            // this.pagesize = 10
            this.search()
        },
        search () {
            var params = {
                userid: this.userid,
                bussineseid: this.bussineseid,
                page: this.page,
                pagesize: this.pagesize
            }
            axios
                .request({
                    url: '/config/BussineseUser/GetList',
                    params,
                    method: 'get'
                })
                .then(res => {
                    this.total = res.data.total
                    this.mockTableData1(res.data.data)
                })
            this.obj = []
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        id: item.id,
                        userid: item.userid,
                        bussineseid: item.bussineseid,
                        name: item.name,
                        username: item.username,
                        workcode: item.workcode,
                        updator: item.updator,
                        updatetime: item.updatetime,
                        duns: item.duns,
                        mobile: item.mobile
                    })
                }
            })
            this.tableData1 = data
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.obj = currentRow
        },
        handlePageSize (value) {
            this.pagesize = value
            this.tableData1 = this.search()
        },
        changePage (value) {
            this.page = value
            this.tableData1 = this.search()
        },
        add () {
            this.addobj = {}
            this.type = 1
            this.addmodal = true
        },
        closeadd () {
            this.addmodal = false
            this.addobj = {}
        },
        adddata (type) {
            let data = this.addobj
            if (type === 1) {
                axios
                    .request({
                        url: '/config/BussineseUser/Add',
                        data,
                        method: 'post'
                    })
                    .then(res => {
                        if (res.data.ret === 1) {
                            this.$Message.success(this.$t('1055'))
                            this.addmodal = false
                            this.search()
                        } else {
                            this.$Message.error(this.$t(res.data.msg))
                        }
                    })
            } else if (type === 2) {
                data.id = this.obj.id
                axios
                    .request({
                        url: '/config/BussineseUser/Update',
                        data,
                        method: 'post'
                    })
                    .then(res => {
                        if (res.data.ret === 1) {
                            this.$Message.success(this.$t('1060'))
                            this.addmodal = false
                            this.search()
                        } else {
                            this.$Message.error(this.$t(res.data.msg))
                        }
                    })
            }
        },
        edit () {
            this.addobj = {}
            if (this.obj === null || this.obj === undefined || JSON.stringify(this.obj) === '[]') {
                this.$Message.error(this.$t(9000396))
                return
            }
            this.addobj = this.obj
            this.addmodal = true
            this.type = 2
        },

        handleFormatError (file) {
            this.$Message.error(this.$t('950396') + file.name + this.$t('950397'))
        },
        handleSuccess (res, file) {
            if (res.ret == 1) {
                this.$Message.success(this.$t('950398'))
                this.search()
            } else {
                if (typeof res.msg !== 'number') {
                    inittip(res.msg)
                } else {
                    this.$Message.error(this.$t('' + res.msg))
                }
            }
        },
        handleError () {
            this.$Message.error(this.$t('2010006') + '!')
        },
        handleSelectRow: function (data) {
            this.selectids = []
            for (var i in data) {
                this.selectids.push(data[i].id)
            }
        },
        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: '/config/BussineseUser/Delete',
                            data: data,
                            method: 'post'
                        }).then(res => {
                            if (res.data.ret === 1) {
                                this.$Message.success(this.$t('1058'))
                                this.page = 1
                                this.pagesize = 10
                                this.search()
                                this.obj = {}
                            } else {
                                this.$Message.error(this.$t(res.data.msg))
                            }
                        })
                    }
                })
            }
        }
    }
}
</script>
<style lang="less" scoped>

</style>
