<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>
    <Modal class="newModalClass" :title="$t(950116)" v-model="addmodal" :styles="{width:'720px'}">
      <div class="modal_body">
        <div>
          <div class="filter">
            <label id="分类">
              <i>*</i>{{$t('900514')}}：

            </label>
              <Select v-model="type" label-in-value class="searchSelect">
                <Option v-for="item in typelist" :value="item.id" :key="item.id">{{ item.title }}</Option>
            </Select>
          </div>
          <div class="filter">
             <label id="类别名称">
              <i>*</i>{{$t('920145')}}：

            </label>
              <Input class="searchInput" type="text" v-model="addName"  />
          </div>

        </div>
      </div>
      <div></div>
      <div slot="footer">
        <Button type="text" size="large" @click="cancle()">{{$t(1033)}}</Button>
        <Button type="primary" size="large" @click="asyncSave()">{{$t(1011)}}</Button>
      </div>
    </Modal>
    <Modal class="newModalClass" :title="$t(950116)" v-model="editmodal" :styles="{width:'720px'}">
      <div class="modal_body">
        <div>
          <div class="filter">
            <label id="分类">
               <i>*</i>{{$t('900515')}}：

            </label>

              <Select v-model="detailobj.type" disabled  label-in-value class="searchSelect">
                <Option v-for="item in typelist" :value="item.id" :key="item.id">{{ item.title }}</Option>
            </Select>
          </div>
          <div class="filter">
             <label id="类别名称">
              <i>*</i>{{$t('920145')}}：

            </label>
              <Input class="searchInput" type="text" v-model="detailobj.name"  />
          </div>

        </div>
      </div>
      <div></div>
      <div slot="footer">
        <Button type="text" size="large" @click="cancle()">{{$t(1033)}}</Button>
        <Button type="primary" size="large" @click="asyncEdit()">{{$t(1011)}}</Button>
      </div>
    </Modal>
      <div class="divborder newTableDiv">
      <Table
         class="tableClass"
        @on-current-change="handleRowChange"
         @on-selection-change="selecttrchange"
        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>
  </div>
</template>
<script>
import axios from '@/libs/api.request';
export default {
    data () {
        return {
            total: 0,
            page: 1,
            pagesize: 10,
            type: 1,
            addName: '',
            typelist: [
                { id: 1, title: '返工' },
                { id: 2, title: '报废' }
            ],
            tableData1: [],
            detailobj: {},
            // 克隆对象
            tableRowObj: {},
            delelistobj: [], // 删除的所有对象
            addmodal: false,
            editmodal: false,
            tableColumns1: [
                {
                    type: 'selection',
                    width: 50,
                    align: 'center'
                },
                {
                    type: 'index2',
                    title: this.$t(1008),
                    key: 'xuhao',
                    width: 70,
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    }
                },
                {
                    title: this.$t(900514),
                    key: 'typename'
                },
                {
                    title: this.$t(920145),
                    key: 'name'
                }
            ]
        };
    },
    created () {
        this.search();
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        search () {
            var params = {
                type: this.type == undefined ? 0 : this.type,
                page: this.page,
                pagesize: this.pagesize
            };
            axios
                .request({
                    url: '/qms/ReworkReferenceType/Getlist',
                    params,
                    method: 'get'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        this.total = res.data.total;
                        this.mockTableData1(res.data.data);
                        this.detailobj = {};
                        this.page = res.data.page;
                    }
                });
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        id: item.id,
                        type: item.type,
                        typename: item.type === 1 ? '返工' : '报废',
                        name: item.name
                    })
                }
            })
            this.tableData1 = data
        },
        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.detailobj = currentRow
            this.tableRowObj = this.$time.deepClone(currentRow)
        },
        handleFormatError (file) {
            this.$Message.error(this.$t('950396') + file.name + this.$t('950397'))
        },
        handleSuccess (res, file) {
            if (res.ret === 1) {
                this.page = 1
                this.search(this.page, this.pagesize)
                this.$Message.success(this.$t('950398'))
            } else {
                if ((res.msg + '').indexOf(',') != -1) {
                    inittip(res.msg)
                } else {
                    this.$Message.error(this.$t(res.msg))
                }
            }
        },
        handleError () {
            this.$Message.error(this.$t('950399'))
        },
        selecttrchange (value) {
            this.delelistobj = value
        },
        add () {
            this.addmodal = true;
        },
        edit () {
            this.editmodal = true;
            this.detailobj = this.$time.deepClone(this.tableRowObj)
        },
        cancle () {
            this.addmodal = false;
            this.editmodal = false;
        },
        asyncSave () {
            if (this.addtype == 0 || this.addName == undefined || this.addName == '') {
                this.$Message.error(this.$t(9000136))
                return false
            }
            var data = {
                type: this.type,
                name: this.addName
            }
            axios
                .request({
                    url: '/qms/ReworkReferenceType/Add',
                    data,
                    method: 'post'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        this.$Message.success(this.$t(1055))
                        this.search(1, 10)
                        this.type = ''
                        this.addName = ''
                        this.addmodal = false
                    } else {
                        this.$Message.error(this.$t(res.data.msg))
                    }
                })
        },
        asyncEdit () {
            if (this.detailobj.type == 0 || this.detailobj.name == undefined || this.detailobj.name == '' || this.detailobj.id == undefined || this.detailobj.id == 0) {
                this.$Message.error(this.$t(9000136))
                return false
            }
            var data = {
                id: this.detailobj.id,
                type: this.detailobj.type,
                name: this.detailobj.name
            }
            axios
                .request({
                    url: '/qms/ReworkReferenceType/Modify',
                    data,
                    method: 'post'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        this.$Message.success(this.$t(1060))
                        this.page = 1
                        this.search()
                        this.editmodal = false
                    } else {
                        this.$Message.error(this.$t(res.data.msg))
                    }
                })
        },
        del () {
            if (this.delelistobj.length < 1) {
                this.$Message.error(this.$t('1017'))
                return false
            }
            this.confirm()
        },
        confirm () { // 确认删除
            this.$Modal.confirm({
                title: this.$t('1018'),
                content: '',
                width: '290px',
                closable: true,
                okText: this.$t('1004'),
                onOk: () => {
                    var arr = []
                    this.delelistobj.forEach(item => {
                        arr.push(item.id)
                    })
                    var data = {
                        ids: arr
                    }
                    this.axios.request({
                        url: '/qms/ReworkReferenceType/Del',
                        data,
                        method: 'post'
                    }).then(res => {
                        if (res.data.ret === 1) {
                            this.search(1, this.pagesize)
                            this.$Message.success(this.$t('1'))
                            this.delelistobj = []
                        } else {
                            this.$Message.error(this.$t(res.data.msg))
                        }
                    })
                },
                onCancel: () => {
                    return false
                }
            })
        }
    }
};
</script>
