<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>{{ $t(8218) }}：</label>
                <Input v-model="tool_name" class="searchInput" />
            </div>
            <div class="filter">
                <label>{{ $t(8217) }}：</label>
                <Input v-model="tool_number" class="searchInput" />
            </div>
            <div class="filter">
                <label>{{ $t(8219) }}：</label>
                <Input v-model="tool_type" class="searchInput" />
            </div>
            <div class="searchBtn">
                <Button
                    type="primary"
                    class="twoWord"
                    icon="ios-search"
                    @click="search(1, 10)"
                    >{{ $t("1001") }}</Button
                >
                <Button
                    type="error"
                    class="twoWord"
                    @click="returnGoods()"
                    icon="md-close-circle"
                    >退货</Button
                >
                <Button
                    type="success"
                    class="twoWord"
                    icon="md-add-circle"
                    @click="warehousing()"
                    >入库</Button
                >
                <!-- <label>测试结果:</label>
                <Select v-model="testResult" clearable style="width:160px;">
                    <Option :value="1">待检测</Option>
                    <Option :value="2">通过</Option>
                    <Option :value="3">不通过</Option>
                </Select>-->
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                @on-current-change="handleRowChange"
                @on-selection-change="handleSelectRow"
                highlight-row
                :data="tableData1"
                :columns="columns1"
            ></Table>
            <div class="pageDiv">
                <div class="pageDirection">
                    <Page
                        :total="total"
                        :current="page"
                        @on-change="changePage"
                        show-elevator
                        show-sizer
                        show-total
                        @on-page-size-change="handlePageSize"
                        :page-size-opts="[10, 20, 30, 40, 100]"
                    ></Page>
                </div>
            </div>
        </div>
        <Modal
            class="newModalClass"
            v-model="modal1"
            :styles="{ width: '1000px' }"
        >
            <p slot="header" v-if="detailobj">
                <span>{{ detailobj.category_name }}</span>
            </p>
            <div class="modal_body" style="padding-right: 40px">
                <Table
                    class="tableClass"
                    :data="modalTable"
                    :columns="defendColumns"
                    :max-height="900"
                ></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>
                    <Icon type="ios-add-circle-outline" style="font-size:28px" @click="addItem" />
                </div>-->
            </div>
            <div slot="footer">
                <Button
                    type="text"
                    size="large"
                    @click="modal1 = false"
                    v-text="$t('1033')"
                    >取消</Button
                >
                <Button
                    type="primary"
                    size="large"
                    @click="model1 = false"
                    v-text="$t('1011')"
                    >确定</Button
                >
            </div>
        </Modal>
        <Modal
            class="newModalClass"
            title="退货"
            v-model="modal2"
            :styles="{ width: '940px' }"
        >
            <div class="modal_body" style="padding-right: 40px">
                <Table
                    class="tableClass"
                    :data="returnData"
                    :columns="returnColumns"
                    :max-height="900"
                ></Table>
            </div>
            <div slot="footer">
                <Button type="text" @click="modal2 = false" v-text="$t('1033')"
                    >取消</Button
                >
                <Button type="primary" @click="submitList" v-text="$t('1011')"
                    >确定</Button
                >
            </div>
        </Modal>
        <!-- 入库 -->
        <Modal
            class="newModalClass"
            title="入库"
            v-model="modal3"
            :styles="{ width: '940px' }"
        >
            <div class="modal_body" style="padding-right: 40px">
                <Table
                    class="tableClass"
                    :data="enterData"
                    :columns="defendColumns1"
                    :max-height="900"
                ></Table>
                <!-- <div>
                    <Icon type="ios-add-circle-outline" style="font-size:28px" @click="addItem" />
                </div>-->
            </div>
            <div slot="footer">
                <Button
                    type="text"
                    size="large"
                    @click="modal3 = false"
                    v-text="$t('1033')"
                    >取消</Button
                >
                <Button
                    type="primary"
                    size="large"
                    @click="submitList"
                    v-text="$t('1011')"
                    >提交</Button
                >
            </div>
        </Modal>
    </div>
</template>

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

export default {
    data() {
        return {
            action:
                axios.publicPath +
                "ims/Upload/Submit?templateName=TechToolList",
            headers: {
                token: localStorage.getItem("token"),
                timestamp: localStorage.getItem("timestamp"),
                nonce: localStorage.getItem("nonce"),
                sign: localStorage.getItem("sign"),
            },
            tool_name: "",
            tool_number: "",
            tool_type: "",
            checkState: "",
            modal1: false,
            modal2: false,
            modal3: false,
            selection: [],
            columns1: [
                {
                    type: "selection",
                    width: 60,
                    align: "center",
                },
                {
                    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: "类别",
                    key: "category_name",
                    width: 100,
                },
                {
                    title: this.$t(8218),
                    key: "material_name",
                },
                {
                    title: this.$t(8217),
                    key: "tool_number",
                },
                {
                    title: "模具批次号/序号",
                    key: "serial_number",
                },
                {
                    title: "供应商",
                    key: "supplier_name",
                },
                {
                    title: "检测结果",
                    key: "state",
                    render: (h, params) => {
                        let bg = "";
                        if (params.row.result == 2) {
                            bg = "red"; // 通过
                        }
                        if (params.row.result == 1) {
                            bg = "green";
                        }
                        return h("div", [
                            h("span", {
                                style: {
                                    display: "inline-block",
                                    marginRight: "5px",
                                    borderRadius: "50%",
                                    width: "20px",
                                    height: "20px",
                                    backgroundColor: bg,
                                },
                            }),
                            h(
                                "a",
                                {
                                    on: {
                                        click: () => {
                                            let data = {
                                                Page: 0,
                                                PageSize: 0,
                                                ProjectId: 0,
                                                OrderNumber:
                                                    params.row.orderNumber,
                                                OrderNumbers: [],
                                            };
                                            // this.remove(params.index);
                                            this.request(
                                                "/ims/ToolTest/GetTestDetailData",
                                                data,
                                                "post"
                                            ).then((res) => {
                                                if (res.data.ret == 1) {
                                                    this.modalTable =
                                                        res.data.data;
                                                    this.modal1 = true;
                                                }
                                                console.log(res.data);
                                            });
                                        },
                                    },
                                },
                                "查看详情"
                            ),
                        ]);
                    },
                },
                {
                    title: "测试结果",
                    key: "test_result",
                    render: (h, params) => {
                        let str = "";
                        if (params.row.test_result == 1) {
                            str = "通过";
                        }
                        if (params.row.test_result == 2) {
                            str = "未通过";
                        }
                        return h("span", str);
                    },
                },
            ],
            modal: false,
            defendColumns: [
                {
                    title: this.$t(8025), // 属性名称
                    key: "seq",
                    align: "center",
                },
                {
                    title: this.$t(8026), // 属性值
                    key: "type",
                    align: "center",
                },
                {
                    title: this.$t(8027), // 单位
                    key: "tools",
                    align: "center",
                },
                {
                    title: this.$t(8028), // 单位
                    key: "standardValues",
                    align: "center",
                },
                {
                    title: "允许公差", // 单位
                    align: "center",
                    children: [
                        {
                            title: this.$t(200059), // 单位
                            key: "max",
                            align: "center",
                        },
                        {
                            title: this.$t(200060), // 单位
                            key: "mini",
                            align: "center",
                        },
                    ],
                },
                {
                    title: "检测值", // 单位
                    key: "actualValue",
                    align: "center",
                    render: (h, params) => {
                        let color = "";
                        if (params.row.result == 1) {
                            color = "#000";
                        } else {
                            color = "red";
                        }
                        return h(
                            "span",
                            {
                                style: {
                                    color: color,
                                },
                            },
                            params.row.actualValue
                        );
                    },
                },
            ],
            defendList: [],
            defendColumns1: [
                {
                    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: "类别",
                    key: "category_name",
                    width: 100,
                },
                {
                    title: this.$t(100391),
                    key: "material_name",
                },
                {
                    title: this.$t(900933),
                    key: "part_no",
                },
                {
                    title: this.$t(8034), // 属性名称
                    key: "location",
                    align: "center",
                },
                {
                    title: this.$t(8035), // 属性值
                    key: "to",
                    align: "center",
                    render: (h, params) => {
                        return h(
                            "Select",
                            {
                                props: {
                                    transfer: true,
                                    value: params.row.to,
                                },
                                on: {
                                    "on-change": (event) => {
                                        // console.log(
                                        //     this.enterData[params.index]
                                        // );
                                        // console.log(this.testData[params.index],event)
                                        if (this.type == 1) {
                                            this.testData[
                                                params.index
                                            ].to = event;
                                        }
                                        if (this.type == 3) {
                                            this.enterData[
                                                params.index
                                            ].to = event;
                                        }
                                    },
                                },
                            },
                            params.row.locations.map((item) => {
                                // console.log(item.key)
                                return h("Option", {
                                    props: {
                                        value: item.id,
                                        label: item.name,
                                    },
                                });
                            })
                        );
                    },
                },
            ],
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            detailobj: {},
            enterData: [],
            location: [],
            returnData: [],
            returnColumns: [
                {
                    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: "类别",
                    key: "category_name",
                    width: 100,
                },
                {
                    title: this.$t(100391),
                    key: "material_name",
                },
                {
                    title: this.$t(900933),
                    key: "part_no",
                },
                {
                    title: this.$t(8034), // 属性名称
                    key: "location",
                    align: "center",
                },
                {
                    title: this.$t(8035), // 属性值
                    key: "to",
                    align: "center",
                    render: (h, params) => {
                        return h(
                            "Select",
                            {
                                props: {
                                    transfer: true,
                                    value: params.row.to,
                                },
                                on: {
                                    "on-change": (event) => {
                                        this.returnData[
                                            params.index
                                        ].to = event;
                                    },
                                },
                            },
                            // 11111
                            params.row.locations.map((item) => {
                                // console.log(item.key)
                                return h("Option", {
                                    props: {
                                        value: item.id,
                                        label: item.name,
                                    },
                                });
                            })
                        );
                    },
                },
                {
                    title: "退货理由", // 单位
                    key: "reason",
                    align: "center",
                    render: (h, params) => {
                        return h("Input", {
                            props: {
                                value: params.row.reason,
                                size: "small",
                                min: 0,
                            },
                            on: {
                                input: (val) => {
                                    this.returnData[params.index].reason = val;
                                },
                            },
                        });
                    },
                },
            ],
            modalTable: [],
        };
    },
    created() {
        this.search(1, 10);
        // 格式化表格样式
        this.returnColumns = this.$time.initTableTitle(this.returnColumns);
        // 格式化表格样式
        this.defendColumns1 = this.$time.initTableTitle(this.defendColumns1);
        // 格式化表格样式
        // this.defendColumns = this.$time.initTableTitle(this.defendColumns);
        // 格式化表格样式
        this.columns1 = this.$time.initTableTitle(this.columns1);
    },
    methods: {
        search(page, pagesize) {
            let data = {
                Page: page,
                PageSize: pagesize,
                ProjectId: 0,
                state: "",
                states: "3,4",
                need_test: 1,
                tool_number: this.tool_number.trim(),
                tool_name: this.tool_name.trim(),
                tool_type: this.tool_type.trim(),
            };
            // this.page = page;
            this.request("/ims/ToolTest/GetTestData", data, "post").then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.total = res.data.total;
                        this.tableData1 = res.data.data;
                    }
                }
            );
        },
        getDefendList() {
            let data = {
                Page: 0,
                PageSize: 0,
                Id: this.detailobj.tool_id,
                ProjectId: 0,
                ToolType: 1,
            };
            this.request("/ims/ToolTest/GetDetection", data, "post").then(
                (res) => {
                    // console.log(res.data.data);
                    if (res.data.ret == 1) {
                        this.defendList = res.data.data;
                    }
                }
            );
        },
        getLocation(id) {
            let data = {
                storageid: id,
                locationid: 0,
            };
            this.request("/wms/Location/GetLastLocation", data, "get").then(
                (res) => {
                    this.location = res.data.data;
                }
            );
        },
        check() {
            if (this.selection.length == 0) {
                this.$Message.error("请先选择数据");
                return false;
            }
            this.testData = [];
            let arr = [],
                bool = false;
            this.selection.forEach((ele) => {
                // arr.push(ele.storageid);
                if (ele.need_test == 1) {
                    this.testData.push(ele);
                } else {
                    bool = true;
                }
                // if (arr.indexOf(ele.storageid) < 0) {
                //     bool = true;
                // }
            });

            if (bool == true) {
                // this.$Message.error("所选物料必须是同一仓库");
                this.$Message.error("请选择需要测试的物料");
                return false;
            }

            // this.testData.forEach(ele=>{
            // 	ele.to=''
            // })
            this.getLocation(this.testData[0].storageid);
            this.type = 1;
            this.modal = true;
        },
        returnGoods() {
            this.returnData = [];
            if (this.selection.length == 0) {
                this.$Message.error("请先选择数据");
                return false;
            }
            let bool = false;
            this.selection.forEach((ele) => {
                if (ele.test_result == 2) {
                    bool = false;
                } else {
                    bool = true;
                }
            });
            if (bool) {
                this.$Message.error("当前物料状态不能退货");
                return false;
            }

            // this.getLocation(this.returnData[0].storageid);
            let arr = this.selection;
            let arr1 = arr;
            this.returnData = arr1;
            this.returnData.forEach((ele) => {
                ele.to = 0;
            });

            this.type = 2;
            this.modal2 = true;
        },
        warehousing() {
            if (this.selection.length == 0) {
                this.$Message.error("请先选择数据");
                return false;
            }
            let bool = false;

            this.selection.forEach((ele) => {
                if (ele.state == 4 && ele.test_result == 1) {
                    bool = false;
                } else {
                    bool = true;
                }
            });
            if (bool) {
                this.$Message.error("当前物料状态不能入库");
                return false;
            }
            this.enterData = this.selection;
            let arr = this.selection;
            let arr1 = arr;
            this.enterData = arr1;
            this.enterData.forEach((ele) => {
                ele.to = 0;
            });

            this.type = 3;
            this.getLocation(this.enterData[0].storageid);

            this.modal3 = true;
        },
        submitList() {
            let data = {},
                arr = [],
                obj = {};
            var bool = false;
            // let o = {
            //     OrderNumber: "string",
            //     to: 0,
            //     reason: "string"
            // };

            data.data = arr;

            if (this.type == 2) {
                // 退货
                this.returnData.forEach((ele) => {
                    arr.push({
                        orderNumber: ele.orderNumber,
                        to: ele.to,
                        reason: ele.reason,
                    });
                });
                data.state = 999;
            }
            if (this.type == 3) {
                // 入库
                this.enterData.forEach((ele) => {
                    arr.push({
                        orderNumber: ele.orderNumber,
                        to: ele.to,
                        reason: "",
                    });
                });
                data.state = 997;
            }
            console.log(data);

            // return false;

            // if (bool) {
            //     this.$Message.error("请输入测试项目维护信息");
            //     return false;
            // }
            this.request(
                "/ims/ToolTest/HandleTestDetailData",
                data,
                "post"
            ).then((res) => {
                // console.log(res.data);
                if (res.data.ret == 1) {
                    this.$Message.success("提交成功");
                    this.modal2 = false;
                    this.modal3 = false;
                    this.search(1, 10);
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        handleSelectRow(val) {
            this.selection = val;
            // let arr = []
            this.selection.forEach((ele) => {
                let data = {
                    storageid: ele.storageid,
                    locationid: 0,
                };
                this.request("/wms/Location/GetLastLocation", data, "get").then(
                    (res) => {
                        ele.locations = res.data.data;
                    }
                );
            });
            console.log(val);
        },
        handleRowChange(currentRow, oldCurrentRow) {
            this.detailobj = currentRow;
            this.getDefendList();
        },
        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>
.a {
    background: green;
    border-radius: 50%;
}
</style>
