<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>{{$t(600036)}}：</label>
                <Input v-model="tool_name"  class="searchInput" />
            </div>
            <div class="filter">
                <label>{{$t(8020)}}：</label>
                <Input v-model="tool_number"  class="searchInput" />
            </div>
            <div class="filter">
                <label>{{$t(950050)}}：</label>
                <Input v-model="tool_type"  class="searchInput" />
            </div>

              <div class="searchBtn">
                <Button type="primary" class="twoWord" @click="search(1,10)"  icon="ios-search" >{{$t('1001')}}</Button>
                <Button type="primary" class="twoWord" icon="ios-create-outline" @click="check()">{{$t(1950506)}}</Button>
              </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                @on-current-change="handleRowChange"
                highlight-row
                :data="tableData1"
                :columns="columns1"
            ></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="$t(1950506)" v-model="modal" :styles="{width:'940px'}">
            <div class="modal_body" style="padding-right:40px;">
                <Table
                    class="tableClass"
                    :data="defendList"
                    :columns="defendColumns"
                    :max-height="900"
                ></Table>
            </div>
            <div slot="footer">
                <Button type="text" size="large" @click="modal=false" v-text="$t('1033')">取消</Button>
                <Button type="primary" size="large" @click="submitList" v-text="$t('1011')">提交</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import axios from '@/libs/api.request';

export default {
    data () {
        return {
            action:
                axios.publicPath +
                'tlm/Upload/Submit?templateName=TechToolList',
            headers: {
                token: localStorage.getItem('token'),
                timestamp: localStorage.getItem('timestamp'),
                nonce: localStorage.getItem('nonce'),
                sign: localStorage.getItem('sign')
            },
            tool_name: '',
            tool_number: '',
            tool_type: '',
            checkState: '',
            columns1: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    },
                    width: '70px'
                },
                {
                    title: this.$t(600648),
                    key: 'category_name',
                    width: 100
                },
                {
                    title: this.$t(600036),
                    key: 'material_name'
                },
                {
                    title: this.$t(8020),
                    key: 'tool_number'
                },
                {
                    title: this.$t(600011),
                    key: 'supplier_name'
                },
                {
                    title: this.$t(8021),
                    key: 'state',
                    render: (h, params) => {
                        let str = '';
                        if (params.row.state == 1) {
                            str = this.$t(1950503);
                        }
                        if (params.row.state == 2) {
                            str = this.$t(1950504);
                        }
                        return h('span', str);
                    }
                },
                {
                    title: this.$t(8114),
                    key: 'in_time'
                },
                {
                    title: this.$t(8033),
                    key: 'need_test',
                    render: (h, params) => {
                        let str = '';
                        if (params.row.need_test == 1) {
                            str = this.$t(300030);
                        }
                        if (params.row.need_test == 2) {
                            str = this.$t(300031);
                        }
                        return h('span', str);
                    }
                }
            ],
            modal: false,
            defendList: [],
            defendColumns: [
                {
                    title: this.$t(8025),
                    key: 'seq',
                    align: 'center'
                },
                {
                    title: this.$t(8026),
                    key: 'type',
                    align: 'center'
                },
                {
                    title: this.$t(8027),
                    key: 'tools',
                    align: 'center'
                },
                {
                    title: this.$t(8028),
                    key: 'standardValues',
                    align: 'center'
                },
                {
                    title: this.$t(8029),
                    align: 'center',
                    children: [
                        {
                            title: this.$t(200059),
                            key: 'max',
                            align: 'center',
                            render: (h, params) => {
                                let str = ''
                                if (params.row.type == 'OK/NOK' && params.row.max == 0) {
                                    str = ''
                                } else {
                                    str = params.row.max
                                }
                                return h('div', str)
                            }
                        },
                        {
                            title: this.$t(200060),
                            key: 'mini',
                            align: 'center',
                            render: (h, params) => {
                                let str = ''
                                if (params.row.type == 'OK/NOK' && params.row.mini == 0) {
                                    str = ''
                                } else {
                                    str = params.row.mini
                                }
                                return h('div', str)
                            }
                        }
                    ]
                },
                {
                    title: this.$t(8030),
                    key: 'actualValue',
                    align: 'center',
                    render: (h, params) => {
                        if (params.row.type == 'OK/NOK') {
                            return h(
                                'Select',
                                {
                                    props: {
                                        value: params.row.actualValue,
                                        size: 'small',
                                        transfer: true
                                    },
                                    on: {
                                        'on-change': (val) => {
                                            this.defendList[
                                                params.index
                                            ].actualValue = val;
                                        }
                                    }
                                },
                                [
                                    [
                                        h(
                                            'Option',
                                            {
                                                props: {
                                                    value: 'OK'
                                                }
                                            },
                                            'OK'
                                        ),
                                        h(
                                            'Option',
                                            {
                                                props: {
                                                    value: 'NOK'
                                                }
                                            },
                                            'NOK'
                                        )
                                    ]
                                ]
                            );
                        } else {
                            return h('Input', {
                                props: {
                                    value: params.row.actualValue,
                                    size: 'small',
                                    min: 0
                                },
                                on: {
                                    input: val => {
                                        this.defendList[
                                            params.index
                                        ].actualValue = val;
                                    }
                                }
                            });
                        }
                    }
                }
            ],
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            detailobj: {}
        };
    },
    created () {
        this.search(1, 10);
        // 格式化表格样式
        this.defendColumns = this.$time.initTableTitle(this.defendColumns)
        this.columns1 = this.$time.initTableTitle(this.columns1)
    },
    methods: {
        search (page, pagesize) {
            let data = {
                Page: page,
                PageSize: pagesize,
                ProjectId: 0,
                state: this.checkState,
                states: '1',
                tool_number: this.tool_number,
                tool_name: this.tool_name,
                tool_type: this.tool_type
            };
            this.request('/tlm/ToolTest/GetTestData', data, 'post').then(
                res => {
                    // console.log(res.data);
                    if (res.data.ret == 1) {
                        this.total = res.data.total;
                        this.tableData1 = res.data.data;
                    }
                }
            );
        },
        getDefendList () {
            this.defendList = [];
            let data = {
                Page: 0,
                PageSize: 0,
                Id: this.detailobj.tool_id,
                ProjectId: 0,
                ToolType: this.detailobj.toolType
            };
            this.request('/tlm/ToolTest/GetDetection', data, 'post').then(
                res => {
                    if (res.data.ret == 1) {
                        this.defendList = res.data.data;
                        this.defendList.forEach(ele => {
                            ele.orderNumber = this.detailobj.orderNumber;
                        })
                    }
                }
            );
        },
        check () {
            if (!this.detailobj.tool_id) {
                this.$Message.error(this.$t(1043));
                return false;
            }
            if (this.detailobj.state == 2) {
                this.$Message.error(this.$t(8116));
                return false;
            }
            this.getDefendList();
            this.modal = true;
        },
        submitList () {
            let data = [];
            var bool = false;
            this.defendList.forEach(ele => {
                if (
                    ele.actualValue == '' ||
                    ele.actualValue == undefined ||
                    ele.actualValue == null
                ) {
                    bool = true;
                }
            });
            // console.log(this.defendList);
            // return ;
            if (bool) {
                this.$Message.error(this.$t(8115));
                return false;
            }
            this.request('/tlm/ToolTest/SetTestData', this.defendList, 'post').then(
                res => {
                    if (res.data.ret == 1) {
                        this.$Message.success(this.$t(900345));
                        this.modal = false;
                        this.search(1, 10);
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                }
            );
        },
        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);
        },
        request (url, data, type) {
            if (type == 'get') {
                return this.axios
                    .request({
                        url: url,
                        params: data,
                        method: type
                    })
                    .then(res => {
                        return res;
                    })
                    .catch(error => error);
            } else {
                return this.axios
                    .request({
                        url: url,
                        data,
                        method: type
                    })
                    .then(res => {
                        return res;
                    })
                    .catch(error => error);
            }
        }
    }
};
</script>

<style>
</style>
