<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>产线层级：</label>
                <Cascader
                    :data="datalevel"
                    :value="leveldata"
                    trigger="hover"
                    style="display:inline-block;width:200px;"
                    @on-change="changelevel"
                ></Cascader>
            </div>
            <div class="filter">
                <label>程序号：</label>
                <Input class="searchInput" type="text" v-model="filter_program"  clearable/>
                <!-- <Select
                    v-model="filter_program"
                    label-in-value
                    clearable
                    style="width:160px;"
                    filterable
                    remote
                    :remote-method="getProgram"
                    :loading="loading1"
                >
                    <Option
                        v-for="item in program_list"
                        :value="item.programno"
                        :key="item.programno"
                    >{{ item.programno }}</Option>
                </Select> -->
            </div>
            <div class="searchBtn">
                <Button
                    type="primary"
                    class="twoWord"
                    icon="ios-search"
                    @click="search(1,10)"
                >{{$t('1001')}}</Button>
                <!-- <Button type="primary" class="fourWord" @click="clear_filter()">清除查询</Button> -->
                <Button type="success" class="twoWord" icon="md-add-circle" @click="add()">添加</Button>
                <Button type="warning" class="twoWord" icon="ios-create-outline" @click="edit()">修改</Button>
                <Button
                    type="error"
                    class="twoWord intervalRight"
                    icon="md-close-circle"
                    @click="del()"
                >删除</Button>
                <!-- <Button type="primary" class="fourWord" @click="clear_filter()">清除查询</Button> -->
                <Button type="primary" class="fourWord" icon="md-download" @click="loadexcel()">下载模板</Button>
                <!-- <a
                    type="button"
                    href="javascript:;"
                    class="file fourWord"
                    style="margin-bottom:10px"
                >
                    <Icon type="md-cloud-upload" />
                    {{$t('1006')}}
                    <input type="file" name ref="fileInt" id @change="unloadexcel" />
                </a> -->
                <Upload
                    style="display:inline-block"
                    ref="upload"
                    :action="action"
                    name="excel-file"
                    :headers="headers"
                    :show-upload-list="false"
                    :on-format-error="handleFormatError"
                    :on-success="handleSuccess"
                    :on-error="handleError"
                    :format="['xlsx']"
                >
                    <Button type="primary" class="fourWord" icon="md-cloud-upload">{{$t('1006')}}</Button>
                </Upload>
                <Button
                    type="primary"
                    class="fourWord"
                    icon="ios-cloud-download"
                    @click="toExcel()"
                >导出Excel</Button>
            </div>
        </div>

        <div class="divborder">
            <Table
                style="margin-top:10px"
                @on-current-change="handleRowChange"
                highlight-row
                :data="tableData1"
                :columns="tableColumns1"
            ></Table>
            <div style="margin: 10px;overflow: hidden">
                <div style="float: right;">
                    <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
            title="添加刀位"
            v-model="addModal"
            :styles="{top: '40px',width:'900px'}"
        >
            <div class="modal_body" style="width:100%!important;padding:0 20px;">
                <Form ref="addObj" :model="addObj" label-position="top" :rules="addModalRule">
                    <Row type="flex" justify="space-between" class="code-row-bg">
                        <Col span="4">
                            <FormItem label="选择设备" prop="machine_id">
                                <Machine @activeName="outMachineChild" v-model="addObj.machine_id"></Machine>
                            </FormItem>
                        </Col>
                        <Col span="4">
                            <FormItem label="设备名称" prop="machine_name">
                                <Input type="text" disabled v-model="addObj.machine_name"></Input>
                            </FormItem>
                        </Col>
                        <Col span="4">
                            <FormItem label="设备编号" prop="machine_code">
                                <Input type="text" disabled v-model="addObj.machine_code"></Input>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="主轴号" prop="spindle_name">
                                <Input type="text" v-model="addObj.spindle_name"></Input>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-between" class="code-row-bg">
                        <Col span="4">
                            <FormItem label="选择产品" prop="product_id">
                                <Product @activeName="outProductChild" v-model="addObj.product_id"></Product>
                            </FormItem>
                        </Col>
                        <Col span="4">
                            <FormItem label="产品名称">
                                <Input type="text" disabled v-model="addObj.product_name"></Input>
                            </FormItem>
                        </Col>
                        <Col span="4">
                            <FormItem label="产品图纸号">
                                <Input type="text" disabled v-model="addObj.product_drawingcode"></Input>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="加工程序号" prop="programno">
                                <Input type="text" v-model="addObj.programno"></Input>
                            </FormItem>
                        </Col>
                    </Row>
                    <div style="height: 150px; overflow: hidden;overflow-y: scroll;">
                        <Row
                            v-for="(item, index) in addObj.items"
                            v-if="item.status"
                            :key="index"
                            type="flex"
                            justify="space-between"
                            class="code-row-bg"
                        >
                            <Col span="3">
                                <FormItem
                                    label="刀位号"
                                    :prop="'items.'+index+'.cutter_location_name'"
                                    :rules="addModalRule.cutter_location_name"
                                >
                                    <Input
                                        type="text"
                                        v-model="item.cutter_location_name"
                                        placeholder="最多六位数字"
                                        maxlength="6"
                                    ></Input>
                                </FormItem>
                            </Col>
                            <Col span="3">
                                <FormItem
                                    label="刀具选择"
                                    :prop="'items.'+index+'.tool_id'"
                                    :rules="addModalRule.tool_id"
                                >
                                    <Tool
                                        @activeName="outToolChild($event,index)"
                                        v-model="item.tool_id"
                                    ></Tool>
                                </FormItem>
                            </Col>
                            <Col span="3">
                                <FormItem label="刀具类型">
                                    <Select v-model="item.type">
                                        <i-option :value="1">刀片类</i-option>
                                        <i-option :value="0">非刀片类</i-option>
                                    </Select>
                                </FormItem>
                            </Col>
                            <Col span="3">
                                <FormItem label="刀具名称">
                                    <Input type="text" disabled v-model="item.tool_name"></Input>
                                </FormItem>
                            </Col>
                            <Col span="3">
                                <FormItem label="刀具图纸号">
                                    <Input type="text" disabled v-model="item.tool_drawingcode"></Input>
                                </FormItem>
                            </Col>
                            <Col span="2">
                                <FormItem
                                    label="额定寿命"
                                    :prop="'items.'+index+'.ratedlife'"
                                    :rules="addModalRule.ratedlife"
                                >
                                    <Input type="text" v-model="item.ratedlife" min="1"></Input>
                                </FormItem>
                            </Col>
                            <Col span="2">
                                <FormItem
                                    label="寿命系数"
                                    :prop="'items.'+index+'.coefficent'"
                                    :rules="addModalRule.coefficent"
                                >
                                    <Input type="number" v-model="item.coefficent" :min="1"></Input>
                                </FormItem>
                            </Col>
                            <Col span="1">
                                <Icon
                                    type="md-close-circle"
                                    @click="handleRemove(index)"
                                    style="font-size:20px"
                                />
                                <!-- <Button type="primary" size="small" @click="handleRemove(index)">删除</Button> -->
                            </Col>
                        </Row>
                        <FormItem>
                            <Row type="flex" justify="start" class="code-row-bg">
                                <Col span="2">
                                    <Button type="dashed" @click="handleAdd" icon="plus-round">添加</Button>
                                </Col>
                                <Col span="2">
                                    <!-- <Button type="primary" @click="handleReset('addObj')" style="margin-left: 8px">重置</Button> -->
                                </Col>
                            </Row>
                        </FormItem>
                    </div>
                </Form>
            </div>
            <div slot="footer">
                <Button type="text" size="large" @click="addModal=false">{{$t('1033')}}</Button>
                <Button type="primary" size="large" :loading="loading" @click="handleAddSubmit('addObj')">{{$t('1011')}}</Button>
            </div>
        </Modal>

        <Modal
            title="修改刀位"
            v-model="editModal"
            @on-ok="handleEditSubmit('editObj')"
            :loading="loading"
            :styles="{top: '40px',width:'900px'}"
        >
            <div class="modal_body" style="width:100%!important;padding:0 20px;">
                <Form ref="editObj" :model="editObj" label-position="top" :rules="editModalRule">
                    <Row type="flex" justify="space-between" class="code-row-bg">
                        <Col span="5">
                            <FormItem label="设备名称" prop="machine_name">
                                <Input type="text" disabled v-model="editObj.machine_name"></Input>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="设备编号" prop="machine_code">
                                <Input type="text" disabled v-model="editObj.machine_code"></Input>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="主轴号" prop="spindle_name">
                                <Input type="text" v-model="editObj.spindle_name" disabled></Input>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="刀位号" prop="cutter_location_name">
                                <Input type="text" v-model="editObj.cutter_location_name" disabled></Input>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-between" class="code-row-bg">
                        <Col span="5">
                            <FormItem label="选择产品" prop="product_id">
                                <Product @activeName="outProductChild" v-model="editObj.product_id"></Product>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="产品名称">
                                <Input type="text" disabled v-model="editObj.product_name"></Input>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="产品图纸号">
                                <Input type="text" disabled v-model="editObj.product_drawingcode"></Input>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="加工程序号" prop="is_processing_program">
                                <Select
                                    v-model="editObj.is_processing_program"
                                    @on-change="isProcessingProgramChange"
                                >
                                    <Option
                                        v-for="item in programnoList"
                                        :value="item.is_processing_program"
                                        :data-value="item.programno"
                                        :key="item.is_processing_program"
                                    >{{ (item.is_processing_program===1?'':'子')+'程序号:'+item.programno }}</Option>
                                </Select>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-between" class="code-row-bg">
                        <Col span="5">
                            <FormItem label="选择刀具" prop="tool_id">
                                <Tool @activeName="outToolChildEdit" v-model="editObj.tool_id"></Tool>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="刀具名称">
                                <Input type="text" disabled v-model="editObj.tool_name"></Input>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="刀具图纸号">
                                <Input type="text" disabled v-model="editObj.tool_drawingcode"></Input>
                            </FormItem>
                        </Col>
                        <Col span="5">
                            <FormItem label="刀具类型">
                                <Select v-model="editObj.type">
                                    <i-option :value="1">刀片类</i-option>
                                    <i-option :value="0">非刀片类</i-option>
                                </Select>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row type="flex" class="code-row-bg">
                        <Col span="5">
                            <FormItem label="额定寿命" prop="ratedlife">
                                <Input type="text" v-model="editObj.ratedlife"></Input>
                            </FormItem>
                        </Col>
                        <Col span="5" push="2">
                            <FormItem label="寿命系数" prop="coefficent">
                                <Input type="text" v-model="editObj.coefficent"></Input>
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </div>
        </Modal>
    </div>
</template>
<script>
import Product from '_c/product/product.vue';
import Machine from '_c/machine/machine.vue';
import Tool from '_c/tool/tool.vue';
import axios from '@/libs/api.request';
export default {
    name: 'configured',
    components: {
        Product,
        Machine,
        Tool
    },
    data () {
        const validateMachine = (rule, value, callback) => {
            if (value === 0) {
                callback(new Error(this.$t(100150)));
            } else {
                callback();
            }
        };
        const validateProduct = (rule, value, callback) => {
            if (value === 0) {
                callback(new Error(this.$t(100217)));
            } else {
                callback();
            }
        };
        const validateTool = (rule, value, callback) => {
            if (value === 0) {
                callback(new Error('请选择刀具'));
            } else {
                callback();
            }
        };
        return {
            action:
                axios.publicPath + 'tools/upload/submit?templateName=ToolsLocationInfo',
            headers: {
                token: localStorage.getItem('token'),
                timestamp: localStorage.getItem('timestamp'),
                nonce: localStorage.getItem('nonce'),
                sign: localStorage.getItem('sign')
            },
            leveldata: [], // 产线层级插件所需数据
            datalevel: [], // 产线层级选中数据
            levelid: 0, // 产线层级选中的section_id

            loading1: false,
            filter_program: '', // 程序号下来菜单，选中的programno
            program_list: [], // 程序号下来菜单，所需数据

            addMachine: 0, // 添加刀位Modal中的选中设备id
            addProduct: 0, // 添加刀位Modal中的选中产品id
            addModal: false, // 添加刀位Modal的开关
            loading: false,
            addObj: {
                machine_id: 0,
                machine_name: '',
                machine_code: '',
                spindle_name: '',
                product_id: 0,
                product_name: '',
                product_drawingcode: '',
                programno: '',
                add_index: 1,
                items: [
                    {
                        cutter_location_name: '',
                        tool_id: 0,
                        type: 1,
                        tool_name: '',
                        tool_drawingcode: '',
                        ratedlife: '',
                        index: 1,
                        status: 1
                    }
                ]
            },
            obj: {}, // 选中刀位对象容器
            editModal: false,
            programnoList: [],
            editObj: {
                machine_id: 0,
                machine_name: '',
                machine_code: '',
                spindle_name: '',
                product_id: 0,
                product_name: '',
                product_drawingcode: '',
                programno: '',
                subprogramno: '',
                is_processing_program: '',
                cutter_location_name: '',
                tool_id: 0,
                type: 1,
                tool_name: '',
                tool_drawingcode: '',
                ratedlife: '',
                id: 0,
                coefficent: ''
            },
            total: 0, // 刀位列表total
            page: 1, // 刀位列表当前页数
            pagesize: 10, // 刀位列表pagesize
            tableData1: [], // 刀位列表数据容器
            tableColumns1: [
                {
                    // 列表字段属性集合
                    type: 'index2',
                    title: this.$t(1008),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    }
                    // width: 60,
                },
                {
                    title: '设备编号',
                    key: 'machine_code'
                },
                {
                    title: '设备名称',
                    key: 'machine_name'
                },
                {
                    title: '设备位置',
                    key: 'section_dsc'
                },
                {
                    title: '加工程序号',
                    key: 'is_processing_program'
                },
                {
                    title: '程序号',
                    key: 'programno'
                },
                {
                    title: '主轴号',
                    key: 'spindle_name'
                },
                {
                    title: '刀位号',
                    key: 'cutter_location_name'
                },
                {
                    title: '刀具名称',
                    key: 'tool_name'
                },
                {
                    title: '刀具类型',
                    key: 'type',
                    render: (h, params) => {
                        let str = '';
                        str = params.row.type == 0 ? '非刀片类' : '刀片类';
                        return h('div', str);
                    }
                },
                {
                    title: '寿命系数',
                    key: 'coefficent'
                },
                {
                    title: '额定寿命',
                    key: 'ratedlife'
                },
                {
                    title: '刀具图纸号',
                    key: 'drawingcode'
                }
            ],
            addModalRule: {
                machine_id: [
                    {
                        validator: validateMachine,
                        required: true
                    }
                ],
                // machine_name: [
                //     {
                //         required: true,
                //     },
                // ],
                // machine_code: [
                //     {
                //         required: true,
                //     },
                // ],
                spindle_name: [
                    {
                        required: true,
                        message: '请输入主轴号',
                        trigger: 'blur'
                    }
                ],
                // product_id: [
                //     {
                //         validator: validateProduct,
                //     },
                // ],
                programno: [
                    {
                        required: true,
                        message: '加工程序号不能为空',
                        trigger: 'blur'
                    }
                ],
                cutter_location_name: [
                    {
                        required: true,
                        message: '刀位不能为空',
                        trigger: 'blur'
                    }
                ],
                // tool_id: [
                //     {
                //         validator: validateTool,
                //         required: true,

                //     },
                // ],
                ratedlife: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    }
                ],
                coefficent: [{
                    required: true,
                    message: '不能为空',
                    trigger: 'blur'
                }]
            },
            editModalRule: {
                // machine_name: [
                //     {
                //         required: true,
                //         trigger: "blur",
                //     },
                // ],
                // machine_code: [
                //     {
                //         required: true,
                //         trigger: "blur",
                //     },
                // ],
                // spindle_name: [
                //     {
                //         required: true,
                //         trigger: "blur",
                //     },
                // ],
                // cutter_location_name: [
                //     {
                //         required: true,
                //         trigger: "blur",
                //     },
                // ],
                is_processing_program: [
                    {
                        required: true
                        // trigger: "blur",
                        // message: "不能为空",
                    }
                ],
                ratedlife: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    }
                ],
                coefficent: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '不能为空'
                    }
                ]
            }
        };
    },
    created () {
        this.initlevel(); // 初始化产线层级
        this.search(1, 10); // 初始化刀位列表
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1);
    },
    methods: {
        initlevel () {
            // 初始化产线层级
            axios
                .request({
                    url: 'config/ProjectLevelSection/GetSectionTree',
                    method: 'get'
                })
                .then((res) => {
                    const arr = [];
                    this.datalevel = res.data.data;
                    arr.push(res.data.data[0].value);
                    this.leveldata = arr;
                });
        },
        changelevel (data) {
            // 切换产线层级，触发事件
            // 请求数据
            let params = {
                sectionid: this.levelid,
                type: 1
            };
            this.levelid = data[data.length - 1];
            axios
                .request({
                    url:
                        '/tools/ToolConfigV2/getProgramListByCutterLocationConfig',
                    params,
                    method: 'get'
                })
                .then((res) => {
                    this.program_list = res.data.data;
                });
        },
        getProgram (query) {
            if (query !== '') {
                this.loading1 = true;
                setTimeout(() => {
                    let params = {
                        name: query,
                        count: 50
                    };
                    axios
                        .request({
                            url: '/tools/ToolConfigV2/getProgramListByName',
                            params,
                            method: 'get'
                        })
                        .then((res) => {
                            this.program_list = res.data.data;
                            this.loading1 = false;
                        });
                }, 200);
            } else {
                this.program_list = [];
            }
        },
        changePage (value) {
            // 换页事件
            this.page = value;
            this.tableData1 = this.search(this.page, this.pagesize);
        },
        handleRowChange (currentRow, oldCurrentRow) {
            // 选中一条数据，触发事件
            this.obj = currentRow;
        },
        handlePageSize (value) {
            // 切换分页页数，触发事件
            this.pagesize = value;
            this.tableData1 = this.search(this.page, this.pagesize);
        },
        search (page, pagesize) {
            // 点击查询，触发事件
            var data = {
                sectionid: this.levelid,
                program_no: this.filter_program,
                page: page,
                pagesize: pagesize
            };
            axios
                .request({
                    url: '/tools/ToolConfigV2/GetToolConfigInfo',
                    data,
                    method: 'post'
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        this.total = res.data.total;
                        this.tableData1 = res.data.data;
                        // this.mockTableData1(res.data.data);
                    }
                });
        },
        mockTableData1 (tablelist) {
            // 转换数据，并赋值给列表数据容器tableData1
            let data = [];
            tablelist.forEach((item) => {
                if (item) {
                    data.push({
                        machine_code: item.machine_code, // 设备编号
                        machine_name: item.machine_name, // 设备名称
                        section_dsc:
                            item.is_processing_program === '' ||
                            item.is_processing_program === null
                                ? 'NA'
                                : item.is_processing_program, // 是否主程序
                        programno:
                            item.isProcessValue === 1
                                ? item.programno
                                : item.subprogramno, // 程序号
                        spindle_name: item.spindle_name, // 主轴号
                        cutter_location_name: item.cutter_location_name, // 刀位号
                        tool_name: item.tool_name, // 刀具名称
                        type: item.type, // （非）刀片类型
                        ratedlife: item.ratedlife, // 额定寿命
                        drawingcode: item.drawingcode, // 刀具图纸号
                        id: item.id
                    });
                }
            });
            this.tableData1 = data;
        },
        clear_filter () {
            // 清除查询条件
            this.levelid = 0;
            this.leveldata = [];
            this.filter_program = '';
            this.search(1, 10);
        },
        add () {
            // 添加刀位
            this.addModal = true;
            // 初始化
            this.initAddModal();
            this.$refs.addObj.resetFields();
        },
        initAddModal () {
            this.addObj = {
                machine_id: 0,
                machine_name: '',
                machine_code: '',
                spindle_name: '',
                product_id: 0,
                product_name: '',
                product_drawingcode: '',
                programno: '',
                add_index: 1,
                items: [
                    {
                        cutter_location_name: '',
                        tool_id: 0,
                        type: 1,
                        tool_name: '',
                        tool_drawingcode: '',
                        ratedlife: '',
                        index: 1,
                        status: 1
                    }
                ]
            };
        },
        edit () {
            // 编辑刀位
            this.editModal = true;
            // 初始化
            this.initEditModal();
            this.$refs.addObj.resetFields();
        },
        initEditModal () {
            console.log(this.obj);
            let params = {
                id: this.obj.id
            };
            axios
                .request({
                    url: '/tools/ToolConfigV2/getCutterLocationEditInfo',
                    params,
                    method: 'get'
                })
                .then((res) => {
                    this.programnoList = [
                        {
                            is_processing_program: 1,
                            programno: res.data.data.programno
                        },
                        {
                            is_processing_program: 2,
                            programno: res.data.data.subprogramno
                        }
                    ];
                    this.editObj = {
                        coefficent: '' + res.data.data.coefficent,
                        machine_id: res.data.data.machcine_id,
                        machine_name: res.data.data.machine_name,
                        machine_code: res.data.data.machine_code,
                        spindle_name: res.data.data.spindle_name,
                        product_id: res.data.data.product_id,
                        product_name: res.data.data.product_name,
                        product_drawingcode: res.data.data.product_drawingcode,
                        programno: res.data.data.programno,
                        subprogramno: res.data.data.subprogramno,
                        is_processing_program:
                            res.data.data.is_processing_program,
                        cutter_location_name:
                            res.data.data.cutter_location_name,
                        tool_drawingcode: res.data.data.drawingcode,
                        tool_id: res.data.data.tool_id,
                        type: this.obj.type,
                        tool_name: res.data.data.tool_name,
                        ratedlife: '' + res.data.data.ratedlife,
                        id: this.obj.id
                    };
                    console.log(this.editObj)
                });
        },
        isProcessingProgramChange (value) {
            this.editObj.is_processing_program = value;
        },
        del () {
            // 删除刀位
            if (this.obj.id === '' || this.obj.id === undefined) {
                alert(this.$t(1043));
                return false;
            } else {
                if (confirm('确认删除该条数据吗')) {
                    axios
                        .request({
                            url:
                                '/tools/ToolConfigV2/deleteCutterLocation?id=' +
                                this.obj.id,
                            method: 'post'
                        })
                        .then((res) => {
                            if (res.data.ret === 1) {
                                this.$Message.success(this.$t(1058));
                                this.search(1, 10);
                            } else {
                                this.$Message.error(this.$t(res.data.msg));
                            }
                        });
                }
            }
        },
        loadexcel () {
            // 查看模板
            axios
                .request({
                    url: '/config/download/GetTemplate?name=ToolsLocationInfo',
                    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));
                    }
                });
        },
        // unloadexcel() {
        //     // 上传excel 导入数据
        //     const file = this.$refs.fileInt.files[0];
        //     const data = new FormData();
        //     data.append("file", file);
        //     axios
        //         .request({
        //             url: "/tools/upload/submit?templateName=ToolsLocationInfo",
        //             data,
        //             method: "post",
        //         })
        //         .then((res) => {
        //             if (res.data.ret === 1) {
        //                 this.$Message.success(this.$t(100378));
        //                 this.search(1, 10);
        //             } else {
        //                 if (typeof res.msg !== "number") {
        //                     inittip(res.msg);
        //                 } else {
        //                     this.$Message.error(this.$t("" + res.msg));
        //                 }
        //             }
        //         })
        //         .catch((err) => {
        //             this.$Message.error(err.data.msg);
        //         });
        // },
        handleFormatError (file) {
            this.$Message.error(
                this.$t('950396') + file.name + this.$t('950397')
            );
        },
        handleSuccess (res, file) {
            if (res.ret == 1) {
                this.$Message.success(this.$t('950398'));
                this.search();
            } else {
                if (typeof res.msg !== 'number') {
                    inittip(res.msg);
                } else {
                    this.$Message.error(this.$t('' + res.msg));
                }
            }
        },
        handleError () {
            this.$Message.error(this.$t('2010006') + '!');
        },
        toExcel () {
            // 导出excel
            axios
                .request({
                    url:
                        '/tools/ToolConfigV2/ExportRecords?sectionid=' +
                        this.levelid,
                    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));
                    }
                });
        },
        outMachineChild (machineData) {
            // 获取子组件：选择设备中的machcineid
            this.addMachine = machineData.machcine_id;
            this.addObj.machine_id = machineData.machine_id;
            this.addObj.machine_name = machineData.machine_name;
            this.addObj.machine_code = machineData.machine_code;
            this.$refs.addObj.validate();
        },
        outProductChild (productData) {
            // 获取子组件：选择产品中的productid
            if (this.addModal === true) {
                this.addMachine = productData.product_id;
                this.addObj.product_id = productData.product_id;
                this.addObj.product_name = productData.product_name;
                this.addObj.product_drawingcode =
                    productData.product_drawingcode;
                this.$refs.addObj.validate();
            }
            if (this.editModal === true) {
                this.editObj.product_id = productData.product_id;
                this.editObj.product_name = productData.product_name;
                this.editObj.product_drawingcode =
                    productData.product_drawingcode;
                this.$refs.editObj.validate();
            }
        },
        outToolChild (toolData, index) {
            // 获取子组件：选择产品中的productid
            this.addObj.items[index].tool_id = toolData.tool_id;
            this.addObj.items[index].tool_name = toolData.tool_name;
            this.addObj.items[index].tool_drawingcode =
                toolData.tool_drawingcode;
            this.$refs.addObj.validate();
        },
        outToolChildEdit (toolData) {
            // 获取子组件：选择产品中的productid
            this.editObj.tool_id = toolData.tool_id;
            this.editObj.tool_name = toolData.tool_name;
            this.editObj.tool_drawingcode = toolData.tool_drawingcode;
            this.$refs.editObj.validate();
        },
        handleAddSubmit (name) {
            // 添加
            this.$refs[name].validate((valid) => {
                if (valid) {
                    valid;
                    // 触发添加刀位接口
                    this.loading = true;
                    var data = {
                        cutter_location_name: [],
                        mid: '',
                        product_id: '',
                        programno: '',
                        ratedlife: [],
                        spindle_name: '',
                        tool_id: [],
                        type: [],
                        coefficent: []
                    };
                    if (this.addObj.items.length === 0) {
                        this.$Message.error('必须添加至少1条刀位信息');
                    } else {
                        data.mid = this.addObj.machine_id;
                        data.product_id = this.addObj.product_id;
                        data.spindle_name = this.addObj.spindle_name;
                        data.programno = this.addObj.programno;
                        this.addObj.items.forEach((item) => {
                            data.cutter_location_name.push(
                                item.cutter_location_name
                            );
                            data.ratedlife.push(item.ratedlife);
                            data.type.push(item.type);
                            data.tool_id.push(item.tool_id);
                            data.coefficent.push(item.coefficent);
                        });
                    }
                    axios
                        .request({
                            url: '/tools/ToolConfigV2/configCutterLocationAdd',
                            data,
                            method: 'post'
                        })
                        .then((res) => {
                            if (res.data.ret === 1) {
                                // res.data.ret === 1
                                this.loading = false;
                                this.$Message.success(this.$t(1055));
                                this.addModal = false;
                                this.search(1, 10);
                            } else {
                                this.$Message.error(this.$t(res.data.msg));
                                this.loading = false;
                                // this.addModal = true;
                            }
                        });
                }
            });
        },
        handleEditSubmit (name) {
            // 编辑
            this.$refs[name].validate((valid) => {
                if (valid) {
                    // valid
                    // 触发添加刀位接口

                    var data = {
                        setting: [
                            {
                                id: 0,
                                is_processing_program: 1,
                                product_id: 0,
                                programno: '',
                                ratedlife: 0,
                                toolID: 0,
                                type: 1,
                                coefficent: 0
                            }
                        ]
                    };
                    data.setting[0].id = this.editObj.id;
                    data.setting[0].is_processing_program = this.editObj.is_processing_program;
                    data.setting[0].programno =
                        this.editObj.is_processing_program === 1
                            ? this.editObj.programno
                            : this.editObj.subprogramno;
                    data.setting[0].product_id = this.editObj.product_id;
                    data.setting[0].ratedlife = this.editObj.ratedlife;
                    data.setting[0].toolID = this.editObj.tool_id;
                    data.setting[0].type = this.editObj.type;
                    data.setting[0].coefficent = this.editObj.coefficent;

                    axios
                        .request({
                            url: '/tools/ToolConfigV2/configCutterLocationEdit',
                            data,
                            method: 'post'
                        })
                        .then((res) => {
                            if (res.data.ret === 1) {
                                // res.data.ret === 1
                                this.$Message.success(this.$t(1060));
                                this.editModal = false;
                                this.search(1, 10);
                            } else {
                                this.$Message.error(this.$t(res.data.msg));
                            }
                        });
                } else {
                    // 解决ok按钮loading的问题
                    this.loading = false;
                    this.$nextTick(() => {
                        this.loading = true;
                    });
                }
            });
        },
        handleAdd () {
            this.addObj_index++;
            this.addObj.items.push({
                cutter_location_name: '',
                tool_id: 0,
                type: 1,
                tool_name: '',
                tool_drawingcode: '',
                ratedlife: '',
                index: this.addObj_index,
                status: 1
            });
        },
        handleReset (name) {
            this.$refs[name].resetFields();
        },
        handleRemove (index) {
            this.addObj.items[index].status = 0;
        }
    },
    watch: {}
};
</script>
<style lang="less" scoped>
</style>
