<template>
    <div class="testdiv">
        <div class="divborder newSearchDiv">
                <div class="filter">
                    <label>{{$t(8043)}}：</label>
                    <Input v-model="order_number" class="searchInput"/>
                </div>
                <div class="filter">
                    <label>{{$t(8044)}}：</label>
                    <Select v-model="machineId" clearable filterable class="searchSelect">
                        <Option
                            v-for="item in machineList"
                            :value="item.value"
                            :key="item.id"
                        >{{ item.label }}</Option>
                    </Select>
                    <!-- <Input v-model="tool_number" style="width: 160px" /> -->
                </div>
                <div class="filter">
                    <label>{{$t(900630)}}：</label>
                    <Input v-model="tool_number" class="searchInput" />
                </div>
                <div class="filter">
                    <label>{{$t(900410)}}：</label>
                    <DatePicker
                        type="datetimerange"
                        v-model="dateArr"
                        class="searchInput MaxWidth "
                        format="yyyy-MM-dd HH:mm:ss"
                    ></DatePicker>
                </div>
            <div class="searchBtn" >
                <Button type="primary" class="twoWord" icon="ios-search" @click="search(1,10)">{{$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"
            ></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:'1000px'}" :closable="false">
                <p slot="header" style="text-align:center">
                   <span v-show="type==1">{{$t(150009)}}</span>

                    <!-- <span v-if="type===1">{{$t('1002')}}</span>
                    <span v-if="type===2">{{$t('1003')}}</span>-->
                </p>
                <div>
                    <div class="modalbody">
                        <div class="formItem">
                            <label>{{$t(600532)}}：</label>
                            <DatePicker
                                type="datetime"
                                v-model="datetime"
                                class="searchInput MaxWidth  "
                                format="yyyy-MM-dd HH:mm:ss"
                            ></DatePicker>
                        </div>
                        <!-- <div class="flex"> -->
                        <div class="formItem">
                            <label>{{$t(100045)}}：</label>
                            <Input v-model="adddata.shift" class="searchInput" />
                        </div>
                        <!-- <div class="formItem">
                            <label>时间</label>
                            <TimePicker type="time" style="width: 160px"></TimePicker>
                        </div>-->
                        <div class="formItem">
                            <label>{{$t(100000)}}：</label>
                            <Select
                                v-model="adddata.section"
                                clearable
                                filterable
                                @on-change="getUser"
                               class="searchSelect"
                            >
                                <Option
                                    v-for="item in departList"
                                    :value="item.id"
                                    :key="item.id"
                                >{{ item.title }}</Option>
                            </Select>
                        </div>
                        <div class="formItem">
                            <label>{{$t(150010)}}：</label>
                            <Select
                                v-model="adddata.changer"
                                clearable
                                filterable
                                class="searchSelect"
                            >
                                <Option
                                    v-for="item in staffList"
                                    :value="item.id"
                                    :key="item.id"
                                >{{ item.name }}</Option>
                            </Select>
                        </div>
                        <div class="formItem">
                            <label>{{$t(800273)}}：</label>
                            <Input
                                v-model="adddata.productModel"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150011)}}：</label>
                            <Input
                                v-model="adddata.productionBatchNumber"
                                clearable
                                filterable
                               class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150012)}}：</label>
                            <Input
                                v-model="adddata.blankCode"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <!-- <div class="formItem">
                            <label>产线层级</label>
                            <Cascader
                                :data="leveldata"
                                :value="lineid"
                                trigger="click"
                                change-on-select
                                style="display:inline-block;"
                                @on-change="handlelevelchange"
                            ></Cascader>
                        </div>-->
                        <div class="formItem">
                            <label>{{$t(150013)}}：</label>
                            <Select
                                v-model="adddata.machineId"
                                clearable
                                filterable
                                class="searchSelect"
                            >
                                <Option
                                    v-for="item in machineList"
                                    :value="item.value"
                                    :key="item.id"
                                >{{ item.label }}</Option>
                            </Select>
                        </div>
                        <div class="formItem">
                            <label>{{$t(150014)}}：</label>
                            <Input
                                v-model="adddata.jigTower"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150015)}}：</label>
                            <Input
                                v-model="adddata.jigLocation"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150016)}}：</label>
                            <Input
                                v-model="adddata.coolantConcentration"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150017)}}：</label>
                            <Input
                                v-model="adddata.coolantPressure"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150018)}}：</label>
                            <Input
                                v-model="adddata.coolantTemperature"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(900630)}}：</label>
                            <Select
                                v-model="adddata.toolNumber"
                                clearable
                                filterable
                                class="searchSelect"
                            >
                                <Option
                                    v-for="item in cutList"
                                    :value="item.number"
                                    :key="item.id"
                                >{{ item.number }}</Option>
                            </Select>
                        </div>
                        <div class="formItem">
                            <label>{{$t(150019)}}：</label>
                            <Input
                                v-model="adddata.toolLocation"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150020)}}：</label>
                            <Input
                                v-model="adddata.toolStandardLife"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(900372)}}：</label>
                            <Input
                                v-model="adddata.toollifeRest"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150021)}}：</label>
                            <Input
                                v-model="adddata.detectivePartsCount"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150022)}}：</label>
                            <Input
                                v-model="adddata.defectLocation"
                                clearable
                                filterable
                               class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150023)}}：</label>
                            <Input
                                v-model="adddata.standardSize"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150024)}}：</label>
                            <Input
                                v-model="adddata.actualSize"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150025)}}：</label>
                            <Select
                                v-model="adddata.reason"
                                clearable
                                placeholder="请输入"
                                filterable
                                class="searchSelect"
                            >
                                <Option
                                    v-for="(item,index) in reasonList"
                                    :value="item"
                                    :key="index"
                                >{{ item }}</Option>
                            </Select>
                        </div>
                        <div class="formItem" >
                            <label>{{$t(150026)}}：</label>
                            <Input
                                v-model="adddata.otherQuestion"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                    </div>
                    <div class="modalbody">
                        <div class="formItem">
                            <label>{{$t(150027)}}：</label>
                            <Input
                                v-model="adddata.toolBatchNumber"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150028)}}：</label>
                            <Input
                                v-model="adddata.repairMoldCount"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150029)}}：</label>
                            <Input
                                v-model="adddata.dynamicBalanceResult"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem">
                            <label>{{$t(150030)}}：</label>
                            <Input
                                v-model="adddata.measuredSize"
                                clearable
                                filterable
                                class="searchInput"
                            />
                        </div>
                        <div class="formItem" >
                            <label>{{$t(150031)}}：</label>
                            <Input
                                v-model="adddata.technicalReason"
                                clearable
                                placeholder="请输入"
                                filterable
                                class="searchInput"
                            />
                        </div>
                    </div>
                </div>

                <div slot="footer">
                    <Button type="text" class="twoWord" size="large" @click="closeadd()">{{$t('1033')}}</Button>
                    <Button type="primary" class="twoWord"   @click="addPost()">{{$t('1011')}}</Button>
                    <!-- <Button
                        type="primary"
                        v-if="type===2"
                        size="large"
                        @click="editdata()"
                    >{{$t('1003')}}</Button>-->
                </div>
            </Modal>
        </div>
    </div>
</template>
<script>
import axios from '@/libs/api.request';
import moment from 'moment';
// import "view-design/dist/styles/iview.css";

export default {
    name: 'productmanagement', // 组件不刷新
    data () {
        return {
            action:
                axios.publicPath +
                'tlm/Upload/Submit?templateName=ToollifeToolConfig',
            headers: {
                token: localStorage.getItem('token'),
                timestamp: localStorage.getItem('timestamp'),
                nonce: localStorage.getItem('nonce'),
                sign: localStorage.getItem('sign')
            },
            tool_number: '',
            tool_number1: '',
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            addmodal: false,
            editmodal: false,
            detailobj: {},
            editobj: {},
            type: 0,
            tableColumns1: [
                {
                    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(150032),
                    key: 'orderNumber'
                },
                {
                    title: this.$t(1132),
                    key: 'sectionName'
                },
                {
                    title: this.$t(8044),
                    key: 'machineName'
                },
                {
                    title: this.$t(600042),
                    key: 'toolLocation'
                },
                {
                    title: this.$t(900630),
                    key: 'toolNumber'
                },
                {
                    title: this.$t(900410),
                    key: 'editTime'
                },
                {
                    title: this.$t(400020),
                    key: 'creatorName'
                },
                {
                    title: this.$t(1038),
                    key: 'toolNumber1',
                    render: (h, params) => {
                        return h(
                            'a',
                            {
                                on: {
                                    click: val => {
                                        this.adddata = params.row;
                                        this.datetime = this.adddata.datetime
                                            ? moment(
                                                this.adddata.datetime * 1000
                                            ).format('YYYY-MM-DD HH:mm:ss')
                                            : '';
                                        this.type = 2;
                                        this.addmodal = true;
                                    }
                                }
                            },
                            this.$t(900312)
                        );
                    }
                }
            ],
            adddata: {
                id: 0,
                projectid: 0,
                status: 0,
                creator: 0,
                editor: 0,
                createTime: '2020-07-14T09:48:03.951Z',
                editTime: '2020-07-14T09:48:03.951Z',
                datetime: 0,
                changer: 0,
                blankCode: '',
                jigLocation: '',
                coolantTemperature: '',
                defectLocation: '',
                reason: '',
                shift: '',
                productModel: '',
                machineId: 0,
                machineName: '',
                sectionName: '',
                sectionId: 0,
                section: 0,
                toolStandardLife: '',
                coolantConcentration: '',
                toolNumber: '',
                toollifeRest: '',
                standardSize: '',
                otherQuestion: '',
                productionBatchNumber: '',
                jigTower: '',
                coolantPressure: '',
                toolLocation: '',
                detectivePartsCount: '',
                actualSize: '',
                toolBatchNumber: '',
                repairMoldCount: '',
                dynamicBalanceResult: '',
                technicalReason: '',
                measuredSize: '',
                order_number: ''
            },
            cutList: [],
            handleList: [],
            headList: [],
            attachmentList: [],
            leveldata: [],
            lineid: [],
            machineList: [],
            staffList: [],
            departList: [],
            order_number: '',
            datetime: '',
            machineId: 0,
            dateArr: '',
            reasonList: [
                '装夹错误',
                '操作不当',
                '主轴夹屑',
                '机床原因',
                '原材料问题',
                '重复加工',
                '刀具原因',
                '切削参数',
                '冷却液问题'
            ]
        };
    },
    created () {
        this.getToolDataByType(1);
        this.getDepart();
        // this.getToolDataByType(2);
        // this.getToolDataByType(3);
        // this.getToolDataByType(4);
        this.initmachine();
        this.search(1, 10);
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        getToolDataByType (type) {
            this.request(
                '/tlm/ToolConfig/GetToolDataByType?type=' + type,
                '',
                'get'
            ).then(res => {
                // // console.log(res.data.data,type);
                switch (type) {
                case 1:
                    this.cutList = res.data.data;
                    break;
                case 2:
                    this.handleList = res.data.data;
                    break;
                case 3:
                    this.attachmentList = res.data.data;
                    break;
                case 4:
                    this.headList = res.data.data;
                    break;
                }
            });
        },
        getDepart () {
            this.request('/config/ProjectSection/GetSections', '', 'get').then(
                res => {
                    if (res.data.ret == 1) {
                        this.departList = res.data.data;
                    }
                }
            );
        },
        getUser (value) {
            let section = '';
            if (value) {
                section = value;
            }
            let data = { sectionid: section };
            this.request(
                '/config/ProjectUser/getUserListsBySectionId',
                data,
                'get'
            ).then(res => {
                if (res.data.ret == 1) {
                    // // console.log(res.data.data);
                    this.staffList = res.data.data;
                }
            });
        },

        handleSelectRow: function (data) {
            var ids = [];
            for (var i in data) {
                ids.push(data[i].id);
            }
            this.selectids = ids.join(',');
        },
        search (page, pagesize) {
            let start = 0,
                end = 0;

            start = this.dateArr[0] ? Date.parse(this.dateArr[0]) / 1000 : 0;
            end = this.dateArr[0] ? Date.parse(this.dateArr[1]) / 1000 : 0;

            var data = {
                projectId: 0,
                page: page,
                pagesize: pagesize,
                order_number: this.order_number,
                machineid: this.machineId,
                mid: 0,
                tool_number: this.tool_number,
                start: start,
                end: end
            };
            // console.log(this.dateArr);
            axios
                .request({
                    url: '/tlm/ToolExpection/GetToolExpection',
                    data,
                    method: 'post'
                })
                .then(res => {
                    // console.log(res.data);
                    this.total = res.data.total;
                    this.tableData1 = res.data.data;
                    // this.mockTableData1(res.data.data);
                });
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.detailobj = currentRow;
            // console.log(this.detailobj);
        },
        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);
        },
        initData () {
            this.datetime = '';
            this.adddata = {
                id: 0,
                projectid: 0,
                status: 0,
                creator: 0,
                editor: 0,
                createTime: '2020-07-14T09:48:03.951Z',
                editTime: '2020-07-14T09:48:03.951Z',
                datetime: 0,
                changer: 0,
                blankCode: '',
                jigLocation: '',
                coolantTemperature: '',
                defectLocation: '',
                reason: '',
                shift: '',
                productModel: '',
                machineId: 0,
                machineName: '',
                sectionName: '',
                sectionId: 0,
                section: 0,
                toolStandardLife: '',
                coolantConcentration: '',
                toolNumber: '',
                toollifeRest: '',
                standardSize: '',
                otherQuestion: '',
                productionBatchNumber: '',
                jigTower: '',
                coolantPressure: '',
                toolLocation: '',
                detectivePartsCount: '',
                actualSize: '',
                toolBatchNumber: '',
                repairMoldCount: '',
                dynamicBalanceResult: '',
                technicalReason: '',
                measuredSize: '',
                order_number: ''
            };
        },
        add () {
            this.initData();
            this.type = 1;
            this.addmodal = true;
        },
        closeadd () {
            this.$Modal.confirm({
                title: this.$t('1124'),
                content: this.$t(150033),
                onOk: () => {
                    this.addmodal = false;
                    this.initData();
                }
            });
        },
        addPost () {
            let data = {};

            if (this.type == 2) {
                this.adddata.id = this.detailobj.id;
            }
            data = this.adddata;
            this.adddata.datetime = this.datetime
                ? Date.parse(this.datetime) / 1000
                : 0;
            // console.log(this.adddata);

            // return false;
            this.$Modal.confirm({
                title: this.$t('1124'),
                content: this.$t(150034),
                onOk: () => {
                    // return false;
                    this.axios
                        .request({
                            url: '/tlm/ToolExpection/SetToolExpection',
                            data,
                            method: 'post'
                        })
                        .then(res => {
                            // console.log(res.data);
                            if (res.data.ret === 1) {
                                this.$Message.success(this.$t('800223'));
                                this.addmodal = false;
                                this.detailobj = {};
                                this.search(1, 10);
                            } else {
                                this.$Message.error(this.$t(res.data.msg));
                            }
                        });
                }
            });
        },
        dele () {
            if (
                this.detailobj === undefined ||
                this.detailobj.id === undefined ||
                this.detailobj.id === 0
            ) {
                this.$Message.error(this.$t('1017'));
                return false;
            } else {
                // var data = {
                //   Id: this.detailobj.id,
                // };
                this.$Modal.confirm({
                    title: this.$t('1018'),
                    content: '',
                    width: '290px',
                    closable: true,
                    okText: this.$t('1004'),
                    onOk: () => {
                        axios
                            .request({
                                url:
                                    '/tlm/ToolExpection/DelToolExpection?id=' +
                                    this.detailobj.id,

                                method: 'post'
                            })
                            .then(res => {
                                if (res.data.ret === 1) {
                                    this.$Message.success(this.$t('1058'));
                                    //   this.tableData1 = this.search(this.page, this.pagesize);
                                    this.detailobj = {};
                                    this.search(1, 10);
                                } else {
                                    this.$Message.error(this.$t(res.data.msg));
                                }
                            });
                    }
                });
            }
        },
        edit () {
            if (!this.detailobj.id) {
                this.$Message.error(this.$t(200231));
                return false;
            }
            this.adddata = this.detailobj;
            this.datetime = this.adddata.datetime
                ? moment(this.adddata.datetime * 1000).format(
                    'YYYY-MM-DD HH:mm:ss'
                )
                : '';
            if (this.adddata.section) {
                this.getUser(this.adddata.section);
            }
            this.lineid = [this.adddata.sectionid];
            this.initmachine();
            this.type = 2;
            this.addmodal = true;
        },
        handlelevelchange (value, selectedData) {
            this.lineid = value;
            // this.machinedata = [];
            // this.datamachine = [];
            // this.tableData1 = [];
            this.initmachine();
        },
        initlevel () {
            this.axios
                .request({
                    url: 'config/ProjectLevelSection/GetSectionTree',
                    method: 'get'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        const arr = [];
                        this.leveldata = res.data.data;
                        arr.push(res.data.data[0].value);
                        // this.leveldata = arr;
                        // console.log(this.leveldata);
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                });
        },
        levelshow () {
            arr.forEach(ele => {
                if (ele.value == id && ele.children.length == 0) {
                }
                if (
                    ele.children.length != 0 &&
                    ele.children != undefined &&
                    ele.children != null
                ) {
                    ele.children.forEach(item => {
                        if (item.value == this.detailobj.sectionid) {
                            this.lineid.push(ele.children.value);
                        }
                    });
                    // this.l(ele.children);
                }
            });
        },
        initmachine () {
            // var params = { section: this.lineid[this.lineid.length - 1] };
            var params = { section: 0 };

            this.axios
                .request({
                    url:
                        '/config/MachineAttribution/GetAttributionDataBySection',
                    params,
                    method: 'get'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        this.machineList = res.data.data;
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                });
        },

        loadexcel () {
            axios
                .request({
                    url: '/config/download/GetTemplate?name=ToollifeToolConfig',
                    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));
                    }
                });
        },
        Export () {
            let data = {
                product_id: this.routename,
                route: this.lineid,
                toexcel: 1
            };
            axios
                .request({
                    url: '/tlm/Technology/GetPagedList',
                    data,
                    method: 'post'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        window.location.href =
                            axios.publicPath + '' + res.data.data;
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                });
        },
        unloadexcel () {
            let file = this.readyFiles[0];
            const data = new FormData();
            data.append('file', file);
            data.append('type', 2);
            axios
                .request({
                    url: '/acc/Upload/UploadExcel?templateName=toollifetoolconfig',
                    data,
                    method: 'post'
                })
                .then(res => {
                    if (res.ret === 1) {
                        this.$Message.success(this.$t('100378'));
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                })
                .catch(err => {
                    alert(err.msg);
                });
        },

        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 scoped>

#machine .ivu-input[disabled] {
    color: #515a6e !important;
}
.modalbody {
    padding: 25px;
    width: 950px;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
    /* width:800px; */
}
.flex {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}
.m10 {
    margin: 10px auto;
}
.formItem {
    width: 260px;
    align-items: center;
    display: flex;
    justify-content: space-between;
    margin: 10px 15px;
}

</style>
