<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>{{$t(100391)}}：</label>
                <Select v-model="materialName" clearable class="searchSelect" filterable>
                    <Option
                        v-for="item in materials"
                        label-in-value
                        :value="item.id"
                        :key="item.id"
                    >{{ item.name }} [{{item.pn}}]</Option>
                </Select>
            </div>
            <div class="filter">
                <label>{{$t(200054)}}：</label>
                <Select v-model="status" clearable class="searchSelect">
                    <Option :value="1">{{$t(8075)}}</Option>
                    <Option :value="2">{{$t(8076)}}</Option>
                    <Option :value="3">{{$t(8077)}}</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="success"
                    class="twoWord"
                    icon="ios-create-outline"
                    @click="grind(1,10)"
                >{{$t(8068)}}</Button>
                <Button type="primary" class="twoWord" icon="md-add-circle" @click="enterMt">入库</Button>
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                @on-current-change="handleRowChange"
                highlight-row
                :data="tableData1"
                :columns="tableColumns1"
            ></Table>
            <div class="pageDiv">
                <div class="pageDirection">
                    <Page
                        :total="total"
                        :current="page"
                        @on-change="changePage"
                        show-elevator
                        show-sizer
                        show-total
                        @on-page-size-change="handlePageSize"
                        :page-size-opts="[10,20,30,40,100]"
                    ></Page>
                </div>
            </div>
        </div>
        <Modal class="newModalClass" v-model="modal" title="修磨" :styles="{width:'840px'}">
            <div style="padding-right:40px;">
                <div class="filter">
                    <label style="width:120px!important;">
                        <i>*</i>
                        {{$t(8078)}}：
                    </label>
                    <Input
                        type="text"
                        class="searchInput"
                        @on-keyup.enter="getInfo()"
                        v-model="toolNum"
                    />
                </div>
                <Table :columns="columns1" :data="data1"></Table>
            </div>

            <div slot="footer">
                <Button type="text" size="large" @click="closeadd()">{{$t('1033')}}</Button>
                <Button type="primary" size="large" @click="editdata()">{{$t('1011')}}</Button>
            </div>
        </Modal>
        <Modal class="newModalClass" v-model="modal1" title="入库" :styles="{width:'500px'}">
            <div class="modal_body" style="margin-right:40px;">
                <div class="divborder row1">
                    <div class="inblock">
                        <label>仓库：</label>
                        <Select v-model="storageLocation" class="searchSelect" style="width:200px">
                            <Option
                                v-for="item in storageList"
                                :value="item.id"
                                :key="item.id"
                            >{{ item.name }}</Option>
                        </Select>
                    </div>
                </div>
            </div>
            <div slot="footer">
                <Button type="text" size="large" @click="modal1=false">{{$t('1033')}}</Button>
                <Button type="primary" size="large" :loading="loading" @click="warehousingPost()">入库</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import moment from "moment";

export default {
    data() {
        return {
            materialName: "",
            materials: [],
            status: "",
            index_time: [],
            modal: false,
            total: 0,
            page: 1,
            pagesize: 10,
            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: "70px",
                },
                {
                    title: "ID",
                    key: "id",
                },
                {
                    title: this.$t(100391),
                    key: "material_name",
                },
                {
                    title: this.$t(600011),
                    key: "supplier",
                },
                {
                    title: this.$t(8079),
                    key: "remain_times",
                },
                {
                    title: this.$t(8080),
                    key: "grind_type",
                },
                {
                    title: this.$t(8081),
                    key: "recycling_time",
                },
                {
                    title: this.$t(8082),
                    key: "grind_time",
                },
                {
                    title: this.$t(200054),
                    key: "statusCN",
                    align: "center",
                    render: (h, params) => {
                        let bgColor = "";
                        if (params.row.status == 2) {
                            bgColor = "#028AF8";
                        } else if (params.row.status == 3) {
                            bgColor = "#70B603";
                        } else {
                            bgColor = "#D9001B";
                        }
                        return h(
                            "div",
                            {
                                style: {
                                    color: "#fff",
                                    backgroundColor: bgColor,
                                },
                            },
                            params.row.statusCN
                        );
                    },
                },
            ],
            toolNum: "",
            columns1: [
                {
                    title: this.$t(8083),
                    key: "serial_number",
                    width: "100px",
                },
                {
                    title: this.$t(100391),
                    key: "material_name",
                },
                {
                    title: this.$t(8084),
                    key: "grind_count",
                },
                {
                    title: this.$t(8079),
                    key: "remain_times",
                },
                {
                    title: this.$t(8080),
                    key: "grind_type",
                },
            ],
            data1: [],
            detailobj: {},
            modal1: false,
            modal2: false,
            number: 1,
            useorder: false,
            storageLocation: 0,
            storageList: [],
            mtcode: "",
            productDate: "",
            order: "",
            loading: false,
        };
    },
    created() {
        this.getMaterial();
        this.getStoreList();
        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);
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1);
        // this.columns1 = this.$time.initTableTitle(this.columns1)
    },
    methods: {
        // 获取物料
        getMaterial() {
            //
            let data = {
                pn: "", // 物料编号
                name: "", // 供应商id
                count: 999, // 显示数量
            };
            this.request("/config/Material/GetMaterialData", data, "get").then(
                (res) => {
                    // console.log(res.data);
                    if (res.data.ret == 1) {
                        this.materials = res.data.data;
                    }
                }
            );
        },
        getStoreList() {
            this.request("/wms/WaveHouse/GetWaveHouseNameList", "", "get").then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.storageList = res.data.data;
                    }
                }
            );
        },
        // getStorageLocation(id) {
        //     let data = {
        //         storageid: id,
        //     };
        //     this.request("/wms/Location/GetLastLocation", data, "get").then(
        //         (res) => {
        //             // console.log(res.data);
        //             if (res.data.ret == 1) {
        //                 this.storageList = res.data.data;
        //             }
        //         }
        //     );
        // },
        // 查询
        search(page, pagesize) {
            let starttime = this.index_time[0];
            let endtime = this.index_time[1];

            let data = {
                page: page,
                pagesize: pagesize,
                material: this.materialName,
                part_no: "",
                status: this.status,
                starttime: starttime,
                endtime: endtime,
            };
            this.axios
                .request({
                    url: "/tlm/ToolGrind/GetPagedList",
                    data,
                    method: "post",
                })
                .then((res) => {
                    if (res.data.ret == 1) {
                        this.total = res.data.total;
                        this.mockTableData1(res.data.data);
                    }
                });
        },
        mockTableData1(tablelist) {
            let data = [];
            if (tablelist.length > 0) {
                tablelist.forEach((item) => {
                    if (item) {
                        let status = "";

                        if (item.status == 2) {
                            status = this.$t(8076);
                        } else if (item.status == 3) {
                            status = this.$t(8085);
                        } else {
                            status = this.$t(8075);
                        }
                        data.push({
                            material_name: item.material_name,
                            material_id: item.material_id,
                            manage_mode: item.manage_mode,
                            part_no: item.part_no,
                            supplier: item.supplier,
                            quantity: item.quantity,
                            remain_times: item.remain_times,
                            grind_type:
                                item.grind_type == 2
                                    ? this.$t(8086)
                                    : this.$t(8087),
                            recycling_time: item.recycling_time,
                            grind_time: item.grind_time,
                            status: item.status,
                            statusCN: status,
                            id: item.id,
                            supplier: item.supplier,
                            supplier_id: item.supplier_id,
                            serial_number: item.serial_number,
                        });
                    }
                });
            }

            this.tableData1 = data;
        },
        // 修磨
        grind() {
            if (this.detailobj.id) {
                if (this.detailobj.status != 1) {
                    this.$Message.error(this.$t(8088));
                    return false;
                } else {
                    this.toolNum = "";
                    this.data1 = [];
                    this.modal = true;
                }
            } else {
                this.$Message.error(this.$t(1017));
                return false;
            }
            // this.modal = true;
        },
        // 提交刀具修磨
        editdata() {
            let data = {
                id: this.detailobj.id,
                serial_number: this.toolNum,
            };
            this.request("/tlm/ToolGrind/EditGrindInfo", data, "post").then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.modal = false;
                        this.$Message.success(this.$t(900345));
                        this.search(1, 10);
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                }
            );
        },
        // 扫描刀具序号
        getInfo() {
            // console.log(this.detailobj.serial_number);
            if (this.toolNum != this.detailobj.serial_number) {
                this.$Message.error("扫描批次号与所选物料不匹配");
                return false;
            }
            let data = {
                serial_number: this.toolNum,
            };
            this.request("/tlm/ToolGrind/GetGrindInfo", data, "get").then(
                (res) => {
                    console.log(res.data);
                    if (res.data.ret == 1) {
                        let obj = res.data.data;
                        obj.grind_type =
                            obj.grind_type == 1 ? this.$t(8087) : this.$t(8086);
                        if (
                            this.data1.findIndex((ele) => {
                              return   ele.id == obj.id;
                            }) < 0
                        ) {
                            this.data1.push(obj);
                        }
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                }
            );
        },
        closeadd() {
            this.modal = false;
        },
        enterMt() {
            if (this.detailobj.status == 3) {
                this.modal1 = true;
                // this.getStorageLocation(this.detailobj.storageid);
            } else {
                this.$Message.error("非已修磨状态不能入库");
                return;
            }
        },
        warehousingPost() {
            this.loading = true;
            let data = {};
            data = {
                materialId: this.detailobj.material_id,
                waveHouseID: this.storageLocation,
                count: this.number,
                id: this.detailobj.id,
            };

            this.request(
                "/tlm/ToolReplaceReturn/ReplaceReturnAfterGrind",
                data,
                "post"
            ).then((res) => {
                if (res.data.ret == 1) {
                    this.loading = false;
                    this.$Message.success("入库成功");
                    this.modal1 = false;
                    this.detailobj = {};
                    this.search(1, 10);
                } else {
                    this.loading = false;
                    this.$Message.error(this.$t(res.data.msg));
                    return;
                }
            });
        },
        handleRowChange(currentRow, oldCurrentRow) {
            this.detailobj = currentRow;
        },
        handlePageSize(value) {
            this.pagesize = value;
            this.tableData1 = this.search(this.page, this.pagesize);
        },
        changePage(value) {
            this.page = value;
            this.tableData1 = this.search(this.page, this.pagesize);
        },
        request(url, data, type) {
            if (type == "get") {
                return this.axios
                    .request({
                        url: url,
                        params: data,
                        method: type,
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            } else {
                return this.axios
                    .request({
                        url: url,
                        data,
                        method: type,
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            }
        },
    },
};
</script>
<style lang="less" scoped>
.row1 {
    display: flex;
    flex-wrap: wrap;
}
.inblock {
    margin: 0 10px;
    height: 40px;
    line-height: 40px;
    /* display: inline-block; */
}
.location {
    width: 500px;
    white-space: normal;
}
</style>
