<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;">
        <div class="filter">
          <label >{{$t('100307')}}：</label>
          <Select v-model="productid" label-in-value clearable filterable class="searchSelect" >
              <Option v-for="item in products" :value="item.id" :key="item.id">{{ item.name+"【"+ item.code +"】" }}</Option>
          </Select>
        </div>
        <div class="filter">
          <label >{{$t('920165')}}：</label>
          <Select v-model="materialid" label-in-value clearable filterable class="searchSelect" >
              <Option v-for="item in materials" :value="item.id" :key="item.id">{{ item.name+"【"+ item.pn +"】" + "【" + item.spec + "】" }}</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 intervalRight" icon="md-close-circle"  @click="dele()">{{$t('1004')}}</Button>
            <Button type="primary" class="fourWord" icon="md-download"  @click="downexcel()">{{$t('1007')}}</Button>
        </div>
    </div>
    <div class="divborder newTableDiv">
      <Table
         class="tableClass"
        width='100%'
        @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 class="filter">
              <label><i >*</i>{{$t('800008')}}：</label>
              <Input class="searchInput" type="text" v-model="editobj.sn" :disabled="type===2" />
          </div>
          <div class="filter">
              <label><i>*</i>{{$t('100307')}}：</label>
              <Select v-model="editobj.productid" label-in-value clearable class="searchSelect" >
                  <Option v-for="item in editproducts" :value="item.id" :key="item.id">{{ item.name+"【"+ item.code +"】" }}</Option>
              </Select>
          </div>
          <div class="filter">
              <label><i>*</i>{{$t('920165')}}：</label>
              <Select v-model="editobj.materialid" label-in-value clearable class="searchSelect" >
                  <Option v-for="item in editmaterials" :value="item.id" :key="item.id">{{ item.name+"【"+ item.pn +"】" + "【" + item.spec + "】" }}</Option>
              </Select>
          </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
                        )
                    },
                    width: 70
                },
                {
                    title: this.$t('100089'),
                    key: 'productcode'
                },
                {
                    title: this.$t('100088'),
                    key: 'productname'
                },
                {
                    title: this.$t('100390'),
                    key: 'materialpn'
                },
                {
                    title: this.$t('100391'),
                    key: 'materialname'
                },
                {
                    title: this.$t('100392'),
                    key: 'materialspec'
                },
                {
                    title: this.$t('800008'),
                    key: 'sn'
                },
                {
                    title: this.$t('920139'),
                    key: 'updator'
                },
                {
                    title: this.$t('920138'),
                    key: 'updatetime'
                }
            ],
            editobj: {},
            products: [],
            materials: [],
            editproducts: [],
            editmaterials: [],
            type: 0,
            productid: 0,
            materialid: 0,
            selectids: []
        }
    },
    created () {
        this.search(1, 10)
        this.initproducts()
        this.initmaterials()
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        search (page, pagesize) {
            var params = {
                page: page,
                pagesize: pagesize,
                productid: this.productid,
                materialid: this.materialid,
                sn: this.sn,
                toexcel: 0
            }
            axios
                .request({
                    url: '/qms/StandardPartsSn/GetPageList',
                    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.getproducts()
            this.getmaterials()
            this.editmodal = true
        },
        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.type = 2
            this.getproducts()
            this.getmaterials()
            this.editobj = this.obj
            this.editmodal = true
        },
        adddata () {
            var data = this.editobj
            axios
                .request({
                    url: '/qms/StandardPartsSn/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
                    } else {
                        this.$Message.error(this.$t(res.data.msg))
                    }
                })
        },
        editdata () {
            let data = this.editobj
            data.id = this.obj.id
            axios.request({
                url: '/qms/StandardPartsSn/Update',
                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
                } 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/StandardPartsSn/Delete',
                            data: parameter,
                            method: 'post'
                        }).then(res => {
                            if (res.data.ret === 1) {
                                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))
                            }
                        })
                    }
                })
            }
        },
        handleSelectRow: function (data) {
            this.selectids = []
            for (var i in data) {
                if (this.selectids.indexOf(data[i].id) < 0) {
                    this.selectids.push(data[i].id)
                }
            }
        },
        initproducts () {
            axios.request({
                url: '/config/projectproduct/GetProducts',
                method: 'get'
            }).then(res => {
                this.products = res.data.data
            })
        },
        initmaterials () {
            axios.request({
                url: '/config/Material/GetMaterialList',
                method: 'get'
            }).then(res => {
                this.materials = res.data.data
            })
        },
        getproducts () {
            axios.request({
                url: '/config/projectproduct/GetProducts',
                method: 'get'
            }).then(res => {
                this.editproducts = res.data.data
            })
        },
        getmaterials () {
            axios.request({
                url: '/config/Material/GetMaterialList',
                method: 'get'
            }).then(res => {
                this.editmaterials = res.data.data
            })
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        id: item.id,
                        productid: item.productid,
                        productcode: item.productcode,
                        productname: item.productname,
                        materialid: item.materialid,
                        materialpn: item.materialpn,
                        materialname: item.materialname,
                        materialspec: item.materialspec,
                        sn: item.sn,
                        updator: item.updator,
                        updatetime: item.updatetime
                    })
                }
            })
            this.tableData1 = data
        },
        downexcel () {
            var params = {
                productid: this.productid,
                materialid: this.materialid,
                sn: this.sn,
                toexcel: 1
            }
            axios.request({
                url: '/qms/StandardPartsSn/GetPageList',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    window.location.href = axios.publicPath + '' + res.data.data
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            })
        },
        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)
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.obj = currentRow
        }
    }
}

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

</style>
