<template>
    <div class="testdiv">
      <div class="divborder newSearchDiv">
          <div>
            <div class="filter">
                <label>{{$t('900337')}}：</label>
                <Input class="searchInput" type="text"   v-model="Work_order" />
            </div>
            <div class="filter">
                <label>{{$t('1950464')}}：</label>
                <Input class="searchInput" type="text"   v-model="code" />
            </div>
            <div class="filter">
                  <label>{{$t('1950465')}}：</label>
                  <Select v-model="repair_type"  label-in-value  clearable  class="searchSelect" >
                      <Option  :value=2 v-text="$t('950457')">自主维修</Option>
                      <Option  :value=3 v-text="$t('950458')">委外维修</Option>
                  </Select>
            </div>
            <div class="filter">
                  <label>{{$t('1950466')}}：</label>
                  <Select v-model="repair_status"  label-in-value  clearable  class="searchSelect" >
                      <Option  :value=0 v-text="$t('200034')">维修中</Option>
                      <Option  :value=1 v-text="$t('1950467')">维修完成</Option>
                  </Select>
            </div>
          </div>
        <div class="searchBtn">
            <!-- <Icon type="ios-checkmark-circle-outline" /> -->
            <Button type="primary" class="twoWord intervalRight" icon="ios-search" @click="search(1,pagesize)" >{{$t('1001')}}</Button>
            <Button type="primary" class="fourWord" icon="ios-checkmark-circle-outline" @click="repair_success()">{{$t('1950467')}}</Button>
            <Button type="primary" class="fourWord" icon="md-arrow-round-down" @click="loadexcel()">{{$t('1007')}}</Button>
        </div>
      </div>
      <div class="divborder newTableDiv">
          <Table
           class="tableClass"
            highlight-row
            border
            @on-current-change="handleRowChange"
            @on-selection-change="handleSelectRow"
            :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>
export default {
    data () { // 基础数据
        return {
            Work_order: '', // 工单号
            Stocking_status: '', // 审核状态
            page: 1, // 页码
            pagesize: 10, // 数据条数
            obj: {}, // 某一行的数据
            tableData1: [], // 表格数据
            total: 0, // 数据总条数
            code: '', // 备件编号/名称
            repair_type: '', // 维修类型
            repair_status: '', // 维修状态
            selectids: '',
            ids: [], // 选中多条数据的id集合
            tableColumns1: [
                {
                    type: 'selection',
                    width: 50,
                    align: 'center'
                },
                {
                    type: 'index2',
                    title: this.$t('1008'), // 序号
                    key: 'xuhao',
                    align: 'center',
                    render: (h, params) => {
                        return h('span', params.index + (this.page - 1) * this.pagesize + 1)
                    },
                    width: 80
                },
                {
                    title: this.$t('900337'), // 工单号
                    align: 'center',
                    key: 'code'
                },
                {
                    title: this.$t('960432'), // 备件编号
                    align: 'center',
                    key: 'location'
                },
                {
                    title: this.$t('300001'), // 备件编号
                    align: 'center',
                    key: 'spcode'
                },
                {
                    title: this.$t('300002'), // 备件名称
                    key: 'spname',
                    align: 'center'
                },
                {
                    title: this.$t('1950465'), // 维修类型
                    key: 'apply_state_name',
                    align: 'center'
                },
                {
                    title: this.$t('1950468'), // 维修次数
                    key: 'repair_count',
                    align: 'center'
                },

                {
                    title: this.$t('950439'), // 维修上限
                    key: 'upper_limit',
                    align: 'center'
                },
                {
                    title: this.$t('1950466'), // 维修状态
                    key: 'repair_status_name',
                    align: 'center'
                }
            ] // 表头
        }
    },
    created () { // 生命周期
        this.search(1, 10)
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        search (page, pagesize) {
            var params = {
                code: this.Work_order,
                sparepart: this.code,
                repair_category: this.repair_type,
                status: this.repair_status,
                page: page,
                pagesize: pagesize,
                toexcel: 0
            }
            this.axios.request({
                url: '/tpm/SparepartAudit/GetRepairPagedList',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.total = res.data.total
                    this.mockTableData1(res.data.data)
                    this.page = res.data.page
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        // 渲染列表开始
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    var repair_status_name = this.$t('1950467')
                    if (item.repair_status === 0) {
                        repair_status_name = this.$t('200034')
                    }
                    var state_name = this.$t('950457')
                    if (item.apply_state === 3) {
                        state_name = this.$t('950458')
                    }
                    data.push({
                        id: item.id,
                        code: item.code,
                        machine_id: item.machine_id,
                        location: item.location,
                        repair_status: item.repair_status,
                        repair_status_name: repair_status_name,
                        apply_state: item.apply_state,
                        apply_time: item.apply_time,
                        user_name: item.user_name,
                        faultmode: item.faultmode,
                        apply_state_name: state_name,
                        spcode: item.spcode,
                        spname: item.spname,
                        repair_count: item.repair_count,
                        upper_limit: item.repair_upper
                    })
                }
            })
            this.tableData1 = data
        },
        handleSelectRow (data) { // 选中table行的变化 checkbox
            var ids = []
            for (var i in data) {
                ids.push(data[i].id)
            }
            this.selectids = ids.join(',')
        },
        repair_success () { // 维修完成按钮事件
            if (this.selectids === undefined || this.selectids === '') {
                this.$Message.error(this.$t('1950469'))
                return false
            }
            this.axios.request({
                url: '/tpm/SparepartAudit/FinishRepairs?ids=' + this.selectids,
                method: 'get'
            }).then(res => {
                if (res.data.ret == 1) {
                    this.$Message.success(this.$t('1'))
                } else {
                    this.$Message.error(this.$t('1950471'))
                }
            })
        },
        loadexcel () { // 导出excel
            var params = {
                code: this.Work_order,
                sparepart: this.code,
                repair_category: this.repair_type,
                status: this.status,
                toexcel: 1
            }
            this.axios.request({
                url: '/tpm/SparepartAudit/GetRepairPagedList',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret == 1) {
                    window.location.href = this.axios.publicPath + res.data.data
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        changePage (value) {
            this.page = value
            // The simulated data is changed directly here, and the actual usage scenario should fetch the data from the server
            this.search(this.page, this.pagesize)
        },
        handlePageSize (value) {
            this.pagesize = value
            this.search(this.page, this.pagesize)
        },
        handleRowChange (currentRow, oldCurrentRow) { // 点击之后将某一行的数据添加到obj的对象中
            this.obj = currentRow
        }
    }
}
</script>
<style lang="less" scoped>

</style>
