<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>{{$t(100391)}}：</label>
                <Select v-model="materialName" label-in-value clearable class="searchSelect">
                    <Option
                        v-for="(item,index) in materials"
                        label-in-value
                        :value="item.name"
                        :key="index"
                    >{{ item.name }} [{{item.pn}}]</Option>
                </Select>
            </div>
            <div class="filter">
                <label>{{$t(100046)}}：</label>
                <Select v-model="user" label-in-value clearable class="searchSelect">
                    <Option
                        v-for="(item,index) in userList"
                        label-in-value
                        :value="item.id"
                        :key="index"
                    >{{ item.name }} -->{{item.work_code}}</Option>
                </Select>
            </div>
            <div class="filter">
                <label>{{$t(900337)}}：</label>
                <Select v-model="workOrder" label-in-value clearable class="searchSelect">
                    <Option
                        v-for="(item,index) in orderList"
                        :value="item.id"
                        :key="index"
                    >{{ item.workorder_code }}</Option>
                </Select>
            </div>

            <div class="filter">
                <label>{{$t(900908)}}：</label>
                <DatePicker
                    type="datetimerange"
                    format="yyyy-MM-dd HH:mm:ss"
                    :clearable="false"
                    v-model="index_time"
                    class="searchInput MaxWidth"
                ></DatePicker>
            </div>
            <div class="searchBtn">
                <Button
                    type="primary"
                    class="twoWord"
                    icon="ios-search"
                    @click="search(1,10)"
                >{{$t('1001')}}</Button>

                <Button
                    type="primary"
                    class="twoWord"
                    icon="ios-repeat"
                    @click="recycle()"
                >{{$t(8067)}}</Button>
                <Button type="warning" @click="torepair(3)">修磨</Button>
                <Button type="error" @click="torepair(4)">报废</Button>
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                :columns="columns"
                :data="data1"
                @on-current-change="handleRowChange"
                highlight-row
            ></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"
            v-model="recycleModal"
            title="回收仓库"
            :styles="{top: '40px',width:'400px'}"
        >
            <div class="row">
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{$t(8070)}}：
                    </label>
                    <Select v-model="waveHouseID" label-in-value clearable style="width:160px;">
                        <Option
                            v-for="(item,index) in storeList"
                            label-in-value
                            :value="item.id"
                            :key="item.id"
                        >{{ item.name }}</Option>
                    </Select>
                </div>
            </div>
            <div slot="footer">
                <Button type="text" size="large" @click="closeadd()">{{$t('1033')}}</Button>
                <Button type="primary" size="large" @click="torepair(2)">{{$t('1011')}}</Button>
            </div>
        </Modal>
        <!-- 领用 -->
        <Modal
            class="newModalClass"
            v-model="useModal"
            :title="$t(300047)"
            :styles="{top: '40px',width:'650px'}"
        >
            <div class="row">
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{$t(300044)}}：
                    </label>
                    <Select v-model="newuser" label-in-value clearable style="width:160px;">
                        <Option
                            v-for="(item,index) in userList"
                            label-in-value
                            :value="item.id"
                            :key="item.id"
                        >{{ item.name }} -->{{item.work_code}}</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{$t(1008)}}：
                    </label>
                    <Input
                        type="text"
                        class="searchInput"
                        v-model="order"
                        @on-keyup.enter="getMtInfo()"
                    />
                </div>
                <div class="filter">
                    <label>料号：</label>
                    <Input type="text" class="searchInput" v-model="pn" />
                </div>
                <div class="filter">
                    <label>物料名称：</label>
                    <Input type="text" class="searchInput" v-model="useMaterialName" />
                </div>
                <div class="filter">
                    <label>数量：</label>
                    <Input type="number" class="searchInput" v-model="useCount" />
                </div>
            </div>
            <div slot="footer">
                <Button type="text" size="large" @click="closeadd()">{{$t('1033')}}</Button>
                <Button type="primary" size="large" @click="usePost()">{{$t('1011')}}</Button>
            </div>
        </Modal>

        <!-- 更换 -->
        <Modal
            class="newModalClass"
            v-model="replaceModal"
            title="更换"
            :styles="{top: '40px',width:'900px'}"
        >
            <p>旧物料归还</p>
            <div class="divborder row">
                <div class="filter">
                    <label>工单号：</label>
                    <Select
                        v-model="replaceWorkOrder"
                        label-in-value
                        clearable
                        class="searchSelect"
                    >
                        <Option
                            v-for="(item,index) in orderList"
                            :value="item.id"
                            :key="index"
                        >{{ item.workorder_code }}</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i> 归还人：
                    </label>
                    <Select v-model="replaceUser" label-in-value clearable class="searchSelect">
                        <Option
                            v-for="(item,index) in userList"
                            label-in-value
                            :value="item.id"
                            :key="index"
                        >{{ item.name }} -->{{item.work_code}}</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>设备编号：
                    </label>
                    <Select
                        v-model="machineCode"
                        clearable
                        class="searchSelect"
                        @on-change="getCutterNum"
                        filterable
                    >
                        <Option
                            v-for="(item,index) in codeList"
                            :value="item.id"
                            :key="index"
                        >{{ item.name }} ({{item.code}})</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>刀位号
                    </label>
                    <Select
                        v-model="replaceToolNum"
                        clearable
                        class="searchSelect"
                        @on-change="getOldOrder"
                    >
                        <Option
                            v-for="(item,index) in cutList"
                            :value="item.cutter_number"
                            :key="index"
                        >{{ item.cutter_number }}</Option>
                    </Select>
                    <!-- <Input type="text" style="width:160px" v-model="replaceToolNum" /> -->
                </div>
                <div class="filter">
                    <label>旧物料序号：</label>
                    <Input
                        type="text"
                        class="searchInput"
                        @on-keyup.enter="getOldOrder(1)"
                        v-model="oldMaterialCode"
                    />
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>物料名称：
                    </label>
                    <Select
                        v-model="replaceMaterialName"
                        clearable
                        class="searchSelect"
                        @on-change="selectMt"
                    >
                        <Option
                            v-for="(item,index) in materialsupplier"
                            :value="item.id"
                            :key="index"
                            :label="item.name"
                        >
                            <span>{{ item.name }}-->{{item.part_no}}-->{{item.supplier}}</span>
                        </Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>物料状态：
                    </label>
                    <Select v-model="replaceState" clearable class="searchSelect">
                        <Option :value="2">回收</Option>
                        <Option :value="3" v-show="repair">修磨</Option>
                        <Option :value="4">报废</Option>
                    </Select>
                </div>
                <div>
                    <Button type="primary" size="large" @click="getReplaceMt()">确定更换</Button>
                </div>
            </div>
            <p>新物料领出</p>
            <div class="divborder">
                <div>
                    <div class="filter" v-show="manage_mode==1 || manage_mode ==2">
                        <label v-show="manage_mode==2">序号：</label>
                        <label v-show="manage_mode==1">批次号：</label>
                        <Input
                            type="text"
                            class="searchInput"
                            @on-keyup.enter="getOldOrder(1)"
                            v-model="oldMaterialCode"
                        />
                    </div>
                </div>
                <div>
                    <Table
                        style="margin-top:10px"
                        @on-current-change="handleRowChange"
                        highlight-row
                        :data="tableData1"
                        :columns="tableColumns1"
                    ></Table>
                </div>
            </div>
            <div slot="footer">
                <Button type="text" size="large" @click="closeadd()">{{$t('1033')}}</Button>
                <Button type="primary" size="large" @click="replacePost()">确定</Button>
            </div>
        </Modal>

        <!-- 归还 -->
        <Modal class="newModalClass" v-model="returnModal" title="归还" :styles="{width:'950px'}">
            <div class="row">
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{$t(900337)}}：
                        <span></span>
                    </label>
                    <Select v-model="returnWorkOrder" label-in-value clearable class="searchSelect">
                        <Option
                            v-for="(item,index) in orderList"
                            :value="item.workorder_code"
                            :key="index"
                        >{{ item.workorder_code }}</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{$t(8064)}}：
                        <span></span>
                    </label>
                    <Select v-model="returnUser" label-in-value clearable class="searchSelect">
                        <Option
                            v-for="(item,index) in userList"
                            label-in-value
                            :value="item.id"
                            :key="index"
                        >{{ item.name }} -->{{item.work_code}}</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>{{$t(8065)}}：</label>
                    <Input
                        type="text"
                        class="searchInput"
                        @on-keyup.enter="getOldOrder(2)"
                        v-model="returnOldCode"
                    />
                </div>
            </div>
            <div class="row">
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{$t(100391)}}：
                        <span></span>
                    </label>
                    <Select v-model="returnMaterialId" clearable class="searchSelect">
                        <Option
                            v-for="(item,index) in materialsupplier"
                            :value="item.id"
                            :key="index"
                            :label="item.name"
                        >
                            <span>{{ item.name }}-->{{item.part_no}}-->{{item.supplier}}</span>
                        </Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{$t(8066)}}：
                        <span></span>
                    </label>
                    <Select v-model="returnState" clearable class="searchSelect">
                        <Option :value="2">{{$t(8067)}}</Option>
                        <Option :value="3" v-show="repair">{{$t(8068)}}</Option>

                        <Option :value="4">{{$t(8069)}}</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label style="width:80px">
                        <i>*</i>
                        {{$t(8070)}}：
                        <span></span>
                    </label>
                    <Select v-model="store_id" clearable class="searchSelect">
                        <Option
                            v-for="(item,index) in storeList"
                            :value="item.id"
                            :key="index"
                        >{{ item.name }}</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{$t(300084)}}：
                        <span></span>
                    </label>
                    <Input type="number" class="searchInput" v-model="returnNum" />
                </div>
                <div class="searchBtn" style="display:inline-flex;">
                    <Button
                        type="primary"
                        class="twoWord"
                        size="large"
                        @click="add()"
                    >{{$t(700147)}}</Button>
                </div>
            </div>
            <div style="padding-right:40px;">
                <p>{{$t(8071)}}</p>
                <div>
                    <Table style="margin-top:10px" :data="tableData2" :columns="tableColumns2"></Table>
                </div>
            </div>
            <div slot="footer">
                <Button type="text" size="large" @click="closeadd()">{{$t('1033')}}</Button>
                <Button type="primary" size="large" @click="returnPost()">{{$t(1011)}}</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import moment from "moment";

export default {
    data() {
        return {
            materialName: "",
            materials: [],
            user: "",
            userList: [],
            workOrder: "",
            orderList: [],
            index_time: [],
            total: 0,
            page: 1,
            pagesize: 10,
            data1: [],
            columns: [
                {
                    title: this.$t("1008"),
                    key: "order",
                    render: (h, params) => {
                        return h("span", params.index + 1);
                    },
                    width: 70,
                    align: "center",
                },

                {
                    title: this.$t(100391),
                    key: "material_name",
                    align: "center",
                },
                {
                    title: this.$t(900933),
                    key: "part_no",
                    align: "center",
                },
                {
                    title: "卸刀状态",
                    key: "reason",
                    align: "center",
                    render: (h, params) => {
                        let str = "";
                        switch (params.row.reason) {
                        case '1':
                            str = "断刀";
                            break;
                        case '2':
                            str = "崩刃";
                            break;
                        case '3':
                            str = "磨损";
                            break;
                        case '4':
                            str = "寿命到期";
                            break;
                        case '5':
                            str = "其他原因";
                            break;
                        }
                        return h('div', str)
                    },
                },
                {
                    title: this.$t(600647),
                    key: "quantity",
                    align: "center",
                },
                // {
                //     title: this.$t(200054),
                //     key: "statusCN",
                //     align: "center",
                // },
                {
                    title: this.$t(100046),
                    key: "operate_user",
                    align: "center",
                },
                {
                    title: this.$t(1046),
                    key: "operate_time",
                    align: "center",
                },
                {
                    title: this.$t(600011),
                    key: "supplier",
                    align: "center",
                },
                {
                    title: this.$t(900337),
                    key: "workorder_code",
                    align: "center",
                },
                {
                    title: this.$t(600008),
                    key: "life",
                    align: "center",
                },
                // {
                //     title: this.$t(1038),
                //     key: "operate_type",
                //     align: "center",
                // },
            ],
            useModal: false,
            newuser: "",
            order: "",
            pn: "",
            useMaterialName: "",
            useCount: 0,
            replaceModal: false,
            replaceWorkOrder: "", // 工单号
            replaceUser: "", // 归还人
            machineCode: "", // 设备编号
            codeList: [],
            replaceToolNum: "", // 刀位号
            cutList: [],
            oldMaterialCode: "", // 旧物料序号
            replaceMaterialName: "", // 物料名称
            materialsupplier: [], // 带供应商的物料
            supplierId: "",
            manage_mode: 0,
            replaceState: "", // 物料状态
            replaceObj: {},
            repair: false,
            tableData1: [],
            tableColumns1: [
                {
                    title: "ID",
                    key: "name",
                    render: (h, params) => {
                        return h("span", params.index + 1);
                    },
                    width: 70,
                    align: "center",
                },
                {
                    title: this.$t(8072),
                    key: "serial_number",
                    align: "center",
                },
                {
                    title: this.$t(8073),
                    key: "position",
                    align: "center",
                },
                {
                    title: this.$t(100391),
                    key: "material_name",
                    align: "center",
                },
                {
                    title: this.$t(900933),
                    key: "part_no",
                    align: "center",
                },
                {
                    title: this.$t(600011),
                    key: "supplier",
                    align: "center",
                },
                {
                    title: this.$t(600647),
                    key: "quantity",
                    align: "center",
                },
                {
                    title: this.$t(300045),
                    key: "count",
                    align: "center",
                },
            ],
            recycleModal: false,
            waveHouseID: 0,
            returnModal: false,
            returnWorkOrder: "",
            returnUser: "",
            returnOldCode: "",
            returnMaterialId: "",
            returnState: "",
            material_name: "",
            supplier: "",
            partNo: "",
            returnNum: "",
            tableData2: [],
            tableColumns2: [
                {
                    title: "ID",
                    key: "name",
                    align: "center",
                    render: (h, params) => {
                        return h("span", params.index + 1);
                    },
                },
                // {
                //     title: this.$t(8072),
                //     key: "serial_number",
                //     align: "center",
                // },
                {
                    title: this.$t(100391),
                    key: "material_name",
                    align: "center",
                },
                {
                    title: this.$t(900933),
                    key: "pn",
                    align: "center",
                },
                {
                    title: this.$t(600011),
                    key: "supplier",
                    align: "center",
                },
                {
                    title: this.$t(200054),
                    key: "statusCN",
                    align: "center",
                },
                {
                    title: this.$t(600647),
                    key: "quantity",
                    align: "center",
                },
                {
                    title: this.$t(1038),
                    key: "com",
                    align: "center",
                    render: (h, params) => {
                        return h("div", [
                            h(
                                "p",
                                {
                                    style: {
                                        color: "#2D8CF0",
                                        cursor: "pointer",
                                    },
                                    on: {
                                        click: (row) => {
                                            var arr = [];
                                            // this.tableData2.filter(item => {
                                            //   if (
                                            //     item.material_id !=
                                            //                               params.row.material_id
                                            //   ) {
                                            //     arr.push(item);
                                            //   }
                                            // });
                                            // this.tableData2 = arr;
                                            this.tableData2.splice(
                                                params.index,
                                                1
                                            );
                                            // this.tableData2 = arr;
                                            // this.tableData2.push(params.row);
                                        },
                                    },
                                },
                                this.$t(1004)
                            ),
                        ]);
                    },
                },
            ],
            storeList: [],
            store_id: "",
        };
    },
    created() {
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1);
        // 格式化表格样式
        this.tableColumns2 = this.$time.initTableTitle(this.tableColumns2);
        // 格式化表格样式
        this.columns = this.$time.initTableTitle(this.columns);
        this.getMtList();
        this.getUserList();
        this.getOrderList();
        this.getStoreList();
        this.getMcode();
        this.getSupplierMt();
        this.index_time = [
            moment(new Date().getTime() - 3600 * 1000 * 24 * 7).format(
                "YYYY-MM-DD "
            ) + "00:00:00",
            moment(new Date().getTime()).format("YYYY-MM-DD ") + "23:59:59",
        ];
        this.search(1, 10);
    },
    methods: {
        // 物料
        getMtList() {
            var data = {
                pn: "", // 物料编号
                name: "", // 供应商id
                count: 50, // 显示数量
            };
            this.request("/config/Material/GetMaterialData", data, "get").then(
                (res) => {
                    if (res.data.ret == 1) {
                        // this.codeList = res.data.data;
                        // console.log(res.data.data);
                        this.materials = res.data.data;
                    }
                }
            );
        },
        // 人员
        getUserList() {
            var data = {
                page: 1,
                pagesize: 999999,
            };
            this.request("/config/user/GetUsers", data, "get").then((res) => {
                if (res.data.ret == 1) {
                    this.userList = res.data.data;
                }
            });
        },
        // 工单号
        getOrderList() {
            this.request("/tlm/ToolWorkorder/GetWorkOrderList", "", "get").then(
                (res) => {
                    // console.log(res.data);
                    if (res.data.ret == 1) {
                        this.orderList = res.data.data;
                    }
                }
            );
        },
        // 设备编号
        getMcode() {
            let data = {
                name: "",
                count: 999,
            };
            this.request("/config/Machine/ListNCMachines", data, "get").then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.codeList = res.data.data;
                    }
                }
            );
        },
        getCutterNum(value) {
            this.machineCode = value;
            var data = {
                mid: this.machineCode,
            };
            this.request("/tlm/ToolInstall/GetCutterNumbers", data, "get").then(
                (res) => {
                    this.cutList = res.data.data;
                }
            );
        },
        getMtInfo() {
            let data = {
                sn: this.order,
            };
            this.request(
                "/tlm/ToolReplaceReturn/GetRecieveInfo",
                data,
                "get"
            ).then((res) => {
                if (res.data.ret == 1) {
                    this.pn = res.data.data.part_no;
                    this.useMaterialName = res.data.data.material_name;
                }
            });
        },
        // 带供应商的物料列表
        getSupplierMt() {
            let data = {
                material_id: "", // 物料id
                part_no: "", // 料号
                supplier_id: "", // 供应商
                name: "",
                count: 50,
            };
            this.request(
                "/tlm/ToolInfo/GetMaterialSuppliers",
                data,
                "get"
            ).then((res) => {
                if (res.data.ret == 1) {
                    this.materialsupplier = res.data.data;
                }
            });
        },
        // 仓库
        getStoreList() {
            this.request("/wms/WaveHouse/GetWaveHouseNameList", "", "get").then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.storeList = res.data.data;
                    }
                }
            );
        },
        search(page, pagesize) {
            let starttime = this.index_time[0];
            let endtime = this.index_time[1];

            let data = {
                page: page,
                pagesize: pagesize,
                workorder_id: this.workOrder,
                material_id: this.materialName,
                operate_mid: this.user,
                starttime: starttime,
                endtime: endtime,
            };
            this.request(
                "/tlm/ToolReplaceReturn/GetPagedList",
                data,
                "post"
            ).then((res) => {
                if (res.data.ret == 1) {
                    this.total = res.data.total;
                    this.data1 = res.data.data;
                    // this.renderTable(res.data.data);
                }
            });
        },
        recycle() {
            if (!this.replaceObj.id) {
                this.$Message.error(this.$t(1043));
                return false;
            }
            this.recycleModal = true;
        },
        torepair(type) {
            if (!this.replaceObj.id) {
                this.$Message.error(this.$t(1043));
                return false;
            }
            if (type == 3) {
                this.$Modal.confirm({
                    title: this.$t("600013"),
                    content: "是否确定要修磨",
                    width: "290px",
                    closable: true,
                    okText: this.$t("1011"),
                    onOk: () => {
                        this.typePost(type);
                    },
                });
            } else if (type == 4) {
                this.$Modal.confirm({
                    title: this.$t("600013"),
                    content: "确定要报废吗",
                    width: "290px",
                    closable: true,
                    okText: this.$t("1011"),
                    onOk: () => {
                        this.typePost(type);
                    },
                });
            } else {
                this.typePost(type);
            }
        },
        typePost(type) {
            let data = {
                storagecategory: type,
                id: this.replaceObj.id,
                waveHouseID: this.waveHouseID,
            };

            this.request(
                "/tlm/ToolReplaceReturn/ReplaceReturn",
                data,
                "post"
            ).then((res) => {
                if (res.data.ret == 1) {
                    if (type == 2) {
                        this.recycleModal = false;
                        this.search(1, 10);
                    }
                    this.$Message.success(this.$t(res.data.msg));
                    this.search(1, 10);
                    // if (type == 3) {
                    //     this.$router.push("/circulation/tool_grinding");
                    // }
                    // if (type == 4) {
                    //     this.$router.push("/circulation/tool_scrapping");
                    // }
                }
            });
        },
        closeadd() {
            this.recycleModal = false;
            this.useModal = false;
            this.replaceModal = false;
            this.returnModal = false;
        },
        // 搜索表格
        renderTable(arr = []) {
            let data = [];
            arr.forEach((item) => {
                let status = "";
                if (item.status == 1) {
                    status = "领用";
                }
                if (item.status == 2) {
                    status = this.$t(8067);
                }
                if (item.status == 3) {
                    status = this.$t(8068);
                }
                if (item.status == 4) {
                    status = "报废";
                }
                let type = "";
                if (item.operate_type == 1) {
                    type = "更换";
                } else if (item.operate_type == 2) {
                    type = "归还";
                } else {
                    type = "领用";
                }
                data.push({
                    ctime: item.ctime,
                    id: item.id,
                    material_id: item.material_id,
                    material_name: item.material_name,
                    operate_mid: item.operate_mid,
                    operate_time: item.operate_time,
                    operate_typeId: item.operate_type,
                    operate_type: type,
                    operate_user: item.operate_user,
                    part_no: item.part_no,
                    quantity: item.quantity,
                    status: item.status,
                    statusCN: status,
                    supplier: item.supplier,
                    workorder_code: item.workorder_code,
                    workorder_id: item.workorder_id,
                });
            });
            this.data1 = data;
        },
        useMaterial() {
            this.useModal = true;
        },
        replaceMaterial() {
            this.replaceModal = true;
        },
        returnMaterial() {
            this.returnWorkOrder = "";
            this.returnUser = "";
            this.returnOldCode = "";
            this.returnMaterialId = "";
            this.returnState = "";
            this.returnNum = "";
            this.store_id = "";
            this.returnModal = true;
        },
        usePost() {
            var data = {
                sn: this.pn,
                picker: this.newuser,
                quantity: this.useCount,
            };
            this.request("/tlm/ToolReplaceReturn/Recieve", data, "post").then(
                (res) => {
                    console.log(res.data);
                    this.$Message.error(this.$t(res.data.msg));
                }
            );
        },
        // 监听物料选择
        selectMt(value) {
            this.materialsupplier.forEach((item) => {
                if (item.id == value) {
                    this.material_name = item.name;
                    this.supplierId = item.supplier_id;
                    this.supplier = item.supplier;
                    this.partNo = item.part_no;
                }
            });
        },
        // 扫描物料序号获取信息
        getOldOrder(a) {
            let serial_number = "";
            if (a == 1) {
                // 更换
                serial_number = this.oldMaterialCode;
            }
            if (a == 2) {
                // 归还
                serial_number = this.returnOldCode;
            }
            let data = {
                serial_number: serial_number,
                machineid: this.machineCode,
                cutter_number: this.replaceToolNum,
            };
            this.request(
                "/tlm/ToolInfo/GetToolMaterialInfos",
                data,
                "get"
            ).then((res) => {
                if (res.data.ret == 1) {
                    let obj = res.data.data;
                    if (a == 1) {
                        this.oldMaterialCode = obj.serial_number;
                        this.getMaterialLists(obj.material_id, obj.supplier_id);
                        this.replaceMaterialName = obj.material_id;
                        this.supplierId = obj.supplier_id;
                    }

                    if (a == 2) {
                        let item = {};
                        // item = {
                        //     serial_number: obj.serial_number,
                        //     material_name: obj.material_name,
                        //     material_id: obj.material_id,
                        //     pn: obj.part_no,
                        //     supplier: obj.supplier,
                        //     supplier_id: obj.supplier_id,
                        //     // statusCN: status,
                        //     // status: obj.returnState,
                        //     quantity: obj.quantity
                        // };
                        // this.tableData2.push(item);
                    }
                    if (obj.grind == 1) {
                        this.repair = true;
                    }
                    this.replaceState = 2;
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        getMaterialLists(mid, sid) {
            var data = {
                material_id: mid,
                supplier_id: sid,
                serial_number: this.oldMaterialCode,
            };
            this.request(
                "/tlm/ToolInfo/GetStockToolMaterials",
                data,
                "get"
            ).then((res) => {
                if (res.data.ret == 1) {
                    let obj = {},
                        arr = [];
                    res.data.data.forEach((ele) => {
                        arr.push({
                            name: ele.material_name,
                            id: ele.material_id,
                            part_no: ele.part_no,
                            supplier: ele.supplier,
                        });
                    });

                    this.materialsupplier = arr;
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        // 确认更换按钮
        getReplaceMt() {
            let data = {
                material_id: this.replaceMaterialName,
                supplier_id: this.supplierId,
                serial_number: this.oldMaterialCode,
            };
            this.request(
                "/tlm/ToolInfo/GetStockToolMaterials",
                data,
                "get"
            ).then((res) => {
                if (res.data.ret == 1) {
                    this.renderReplace(res.data.data);
                }
                // collar_use: 1
                // manage_mode: 2
                // material_id: 31
                // material_name: "料号产品1"
                // part_no: "料号1"
                // position: "仓库1 -> 区域01 -> 货架01 -> Level01 -> location"
                // quantity: 1
                // serial_number: "ALjy20190916000001"
                // storage_id: 52
                // supplier: "A品牌"
                // supplier_id: 198
            });
        },
        renderReplace(arr = []) {
            let data = [];
            arr.forEach((item) => {
                if (item) {
                    if (item.manage_mode == 1) {
                        this.manage_mode = 1;
                    } else if (item.manage_mode == 2) {
                        this.manage_mode = 2;
                    } else {
                        this.manage_mode = 3;
                    }
                    data.push({
                        material_name: item.material_name,
                        material_id: item.material_id,
                        serial_number: item.serial_number,
                        supplier: item.supplier,
                        supplier_id: item.supplier_id,
                        part_no: item.part_no,
                        quantity: item.quantity,
                        position: item.position,
                        count: 1,
                        id: item.storage_id,
                    });
                }
            });
            this.tableData1 = data;
        },
        // 提交更换
        replacePost() {
            if (this.replaceObj.id) {
                let data = {
                    status: this.replaceState,
                    workorder_id: this.replaceWorkOrder,
                    material_id: this.replaceMaterialName,
                    quantity: 1,
                    operate_mid: this.replaceUser,
                    serial_number: this.oldMaterialCode,
                    supplier_id: this.supplierId,
                    storage_id: this.replaceObj.id,
                };
                this.request(
                    "/tlm/ToolReplaceReturn/Replace",
                    data,
                    "post"
                ).then((res) => {
                    if (res.data.ret == 1) {
                        this.$Message.success(this.$t(8074));
                        this.replaceModal = false;
                        this.search(1, 10);
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                });
            } else {
                this.$Message.error(this.$t(9000396));
            }
        },
        add() {
            let status = "";
            if (this.returnState == 2) {
                status = this.$t(8067);
            }
            if (this.returnState == 3) {
                status = this.$t(8068);
            }
            if (this.returnState == 4) {
                status = this.$t(8069);
            }
            let supplier, supplier_id;
            this.materialsupplier.forEach((ele) => {
                if (ele.id == this.returnMaterialId) {
                    this.material_name = ele.name;
                    this.supplier = ele.supplier;
                    this.supplierId = ele.supplier_id;
                    this.partNo = ele.part_no;
                }
            });
            let obj = {
                serial_number: this.returnOldCode,
                material_name: this.material_name,
                material_id: this.returnMaterialId,
                pn: this.partNo,
                supplier: this.supplier,
                supplier_id: this.supplierId,
                statusCN: status,
                status: this.returnState,
                quantity: this.returnNum,
            };
            this.tableData2.push(obj);
        },
        returnPost() {
            let arr = [];
            this.tableData2.forEach((ele) => {
                let obj = {};
                obj.material_id = ele.material_id;
                obj.supplier_id = ele.supplier_id;
                obj.serial_number = ele.serial_number;
                obj.quantity = ele.quantity;
                obj.status = ele.status;
                arr.push(obj);
            });
            let data = {
                workorder_id: this.returnWorkOrder,
                operate_mid: this.returnUser,
                sendbacks: arr,
                waveHouseID: this.store_id,
            };
            // console.log(data)
            if (
                this.returnWorkOrder == "" ||
                this.returnUser == "" ||
                this.returnNum == "" ||
                this.store_id == "" ||
                arr.length == 0
            ) {
                this.$Message.error(this.$t(930055));
                return false;
            }
            // return false;

            this.request("/tlm/ToolReplaceReturn/SendBack", data, "post").then(
                (res) => {
                    // console.log(res.data);
                    if (res.data.ret == 1) {
                        this.$Message.success(this.$t(8074));
                        this.search(1, 10);
                        this.returnModal = false;
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                }
            );
        },
        // 更换表格操作
        handleRowChange(currentRow, oldCurrentRow) {
            this.replaceObj = currentRow;
            // this.getStaffList();
        },
        // 归还表格操作
        handleRowChange1(currentRow, oldCurrentRow) {
            this.replaceObj = currentRow;
            // this.getStaffList();
        },
        handlePageSize(value) {
            this.pagesize = value;
            this.data1 = this.search(this.page, this.pagesize);
        },
        changePage(value) {
            this.page = value;
            this.data1 = 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 lang="less" scoped>
.ivu-select-item {
    white-space: normal;
}
</style>
