<template>
    <div class="testdiv">
        <div class="divborder newSearchDiv">
            <div>
                <!-- <div class="filter">
                    <label>刀具类别：</label>
                    <Input
                        class="searchInput"
                        type="text"
                        v-model="keyword"
                        :placeholder="'关键词'"
                        @change="inittree()"
                    />
                </div> -->
                <div class="searchBtn">
                    <!-- <Button
                        type="success"
                        class="twoWord"
                        icon="md-add-circle"
                        @click="add()"
                        >{{ $t("1002") }}</Button
                    >
                    <Button
                        type="warning"
                        class="twoWord"
                        icon="ios-create-outline"
                        @click="edit()"
                        >{{ $t("1003") }}</Button
                    >
                    <Button
                        type="error"
                        class="twoWord intervalRight"
                        icon="md-close-circle"
                        @click="dele()"
                        >{{ $t("1004") }}</Button
                    > -->
                    <Button type="primary" class="sixWord" @click="toolDefend()"
                        >测试项目维护</Button
                    >
                    <Upload
                        style="display: inline-block"
                        ref="upload"
                        :action="action2"
                        name="excel-file"
                        :headers="headers"
                        :show-upload-list="false"
                        :on-format-error="handleFormatError2"
                        :on-success="handleSuccess2"
                        :on-error="handleError2"
                        :format="['xlsx']"
                    >
                        <Button type="primary" class="sixWord intervalRight">{{
                            $t(8110)
                        }}</Button>
                    </Upload>
                    <Button
                        type="primary"
                        class="eightWord"
                        @click="downloadTest()"
                        >{{ $t(8111) }}</Button
                    >
                </div>
            </div>
        </div>
        <div class="divborder">
            <div>
                <Row :gutter="16">
                    <Col span="7" style="height: 800px; overflow: auto">
                        <div>
                            <Tree
                                ref="tree"
                                :render="renderContent"
                                :data="tree_data"
                                @on-select-change="treeselect"
                            ></Tree>
                        </div>
                    </Col>
                    <Col span="17" style="height: 1100px">
                        <div class="nodetail" v-show="!treeselectnode.toolid">
                            <img :src="url" alt="" />
                            <p>请选择刀头</p>
                        </div>
                        <div>
                            <!-- <div class="div_2D">{{ $t(8112) }}</div> -->
                            <div class="div_2D_img" v-show="src&&treeselectnode.toolid">
                                <embed
                                    :src="src"
                                    type="application/pdf"
                                    width="100%"
                                    height="400px;"
                                />
                            </div>
                        </div>
                        <div class="container" v-show="treeselectnode.id&&treeselectnode.toolid">
                            <div class="basic">
                                <div class="toolTitle">基本信息</div>
                                <div
                                    class="detail"
                                    v-for="item in tabledata1"
                                    :key="item.id"
                                >
                                     <p>
                                        <span class="column">类别名称：</span>
                                        {{ item.category_name }}
                                    </p>
                                    <p>
                                        <span class="column">刀头名称：</span
                                        >{{ item.name }}
                                    </p>
                                    <p>
                                        <span class="column">刀头编号：</span
                                        >{{ item.number }}
                                    </p>
                                    <p>
                                        <span class="column">料号：</span
                                        >{{ item.part_no }}
                                    </p>
                                </div>
                            </div>
                            <div class="geo">
                                <div class="toolTitle">几何参数</div>
                                <div class="detail">
                                    <p
                                        v-for="item in tabledata2"
                                        :key="item.id"
                                    >
                                        <span class="column"
                                            >{{ item.name }}：</span
                                        >
                                        {{ item.code }} {{ item.unit }}
                                        <!-- [{{item.type_value}}] -->
                                    </p>
                                </div>
                            </div>
                            <div class="crafts">
                                <div class="toolTitle">工艺参数</div>
                                <div class="detail">
                                    <p
                                        v-for="item in tabledata3"
                                        :key="item.id"
                                    >
                                        <span class="column"
                                            >{{ item.name }}：</span
                                        >
                                        {{ item.code }} {{ item.unit }}
                                        <!-- [{{item.type_value}}] -->
                                    </p>
                                </div>
                            </div>
                        </div>
                        <!-- <div>
                            <Table
                                style="margin-top: 10px"
                                :data="tabledata1"
                                :columns="tableColumns1"
                                :max-height="200"
                            ></Table>
                            <Table
                                style="margin-top: 10px"
                                :data="tabledata2"
                                :columns="tableColumns2"
                                :max-height="200"
                            ></Table>
                            <Table
                                style="margin-top: 10px"
                                :data="tabledata3"
                                :columns="tableColumns3"
                                :max-height="200"
                            ></Table>
                        </div> -->
                    </Col>
                </Row>
            </div>
        </div>

        <!-- 添加修改模态框开始 -->
        <Modal
            class="newModalClass"
            title="属性配置"
            v-model="add_model"
            :styles="{ width: '800px' }"
        >
            <div class="modal_body" style="margin-right: 40px">
                <div
                    style="
                        border: 1px solid #eee;
                        border-radius: 8px;
                        padding: 5px 25px;
                    "
                >
                    <Upload
                        style="display: inline-block"
                        ref="upload"
                        :action="action"
                        name="excel-file"
                        :headers="headers"
                        :show-upload-list="false"
                        :on-format-error="handleFormatError"
                        :on-success="handleSuccess"
                        :on-error="handleError"
                        :format="['pdf']"
                    >
                        <Button icon="md-cloud-upload">上传2D图纸(pdf)</Button>
                    </Upload>
                    <div style v-text="image_name"></div>
                </div>
                <div
                    style="
                        margin-top: 15px;
                        border: 1px solid #eee;
                        border-radius: 8px;
                        padding: 10px;
                    "
                >
                    <h4 style="margin-left: 20px; font-size: 16px">
                        基本信息：
                    </h4>
                    <div style="margin-top: 10px">
                        <div class="filter">
                            <label><i>*</i>{{ $t(920145) }}：</label>
                            <Input
                                v-model="addobj.cname"
                                disabled
                                class="searchInput"
                            />
                            <!-- <Input class="publicinput" type="text"  v-model="addobj.name"  /> -->
                        </div>
                        <div class="filter">
                            <label><i>*</i>刀头名称：</label>
                            <Select
                                class="searchSelect"
                                v-model="addobj.material_id"
                                filterable
                                remote
                                label-in-value
                                :remote-method="remoteMethod2"
                                :loading="loading2"
                                @on-change="name_change"
                            >
                                <Option
                                    v-for="(option, index) in tool_namelist"
                                    :value="option.id"
                                    :key="index"
                                    >{{
                                        option.name +
                                        "【" +
                                        option.part_no +
                                        "】" +
                                        "【" +
                                        option.spec +
                                        "】"
                                    }}</Option
                                >
                            </Select>
                        </div>
                        <div class="filter">
                            <label> <i>*</i>刀头编号：</label>
                            <Input
                                v-model="addobj.number"
                                class="searchInput"
                            />
                        </div>
                        <div class="filter">
                            <label><i>*</i>{{ $t(900933) }}：</label>
                            <Input
                                v-model="addobj.partno"
                                disabled
                                class="searchInput"
                            />
                        </div>
                        <div class="filter">
                            <label> <i>*</i>是否测试：</label>
                            <Select
                                class="searchSelect"
                                v-model="addobj.need_test"
                            >
                                <Option :value="1">是</Option>
                                <Option :value="2">否</Option>
                            </Select>
                        </div>
                    </div>
                </div>
                <div
                    style="
                        margin-top: 15px;
                        border: 1px solid #eee;
                        border-radius: 8px;
                        padding: 10px;
                    "
                >
                    <Table
                        style="margin-top: 10px"
                        :data="tabledata4"
                        :columns="tableColumns4"
                        :max-height="200"
                    ></Table>
                </div>
                <div
                    style="
                        margin-top: 15px;
                        border: 1px solid #eee;
                        border-radius: 8px;
                        padding: 10px;
                    "
                >
                    <Table
                        style="margin-top: 10px"
                        :data="tabledata5"
                        :columns="tableColumns5"
                        :max-height="200"
                    ></Table>
                </div>
            </div>
            <div slot="footer">
                <Button
                    type="text"
                    size="large"
                    @click="add_model = false"
                    v-text="$t('1033')"
                    >取消</Button
                >
                <Button
                    type="primary"
                    size="large"
                    @click="adddata"
                    v-text="$t('1011')"
                    >确定</Button
                >
            </div>
        </Modal>
        <Modal
            title="测试项目维护"
            v-model="modal"
            :styles="{ top: '40px', width: '900px' }"
        >
            <div class="modal_body">
                <Table
                    style="margin-top: 10px"
                    :data="defendList"
                    :columns="defendColumns"
                    :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="modal = false"
                    v-text="$t('1033')"
                    >取消</Button
                >
                <Button
                    type="primary"
                    size="large"
                    @click="submitList"
                    v-text="$t('1011')"
                    >提交</Button
                >
            </div>
        </Modal>
    </div>
</template>
<script>
// import pdf from 'vue-pdf'
export default {
    data() {
        return {
            url: require("../../../assets/images/default2.png"),
            keyword: "", // 刀具类别
            tree_data: [], // 刀具类型
            treeselectnode: {}, // 树选中节点node
            type: 1, // 模态框类型
            addobj: {}, // 添加对象
            src: "", // pdf路径
            //   src: this.axios.publicPath + 'Files/image/2020-05-29/38b664482ffb4b20b3847f26527dd104.pdf', // pdf路径
            add_model: false, // 添加修改模态框
            action: this.axios.publicPath + "config/upload/UploadImage",
            action1:
                this.axios.publicPath +
                "tlm/Upload/Submit?templateName=ToolInfoList",
            action2:
                this.axios.publicPath +
                "tlm/Upload/Submit?templateName=ToollifeDetectionTool",
            headers: {
                token: localStorage.getItem("token"),
                timestamp: localStorage.getItem("timestamp"),
                nonce: localStorage.getItem("nonce"),
                sign: localStorage.getItem("sign"),
            },
            pdf_name: "", //
            image_name: "", // 上传的pdf文件名
            loading2: false,
            tool_namelist: [], // 刀具名称list
            tabledata1: [], // 基本信息 数据
            tableColumns1: [
                // 基本信息表头
                {
                    title: this.$t(300018),
                    key: "station",
                    align: "center",
                    children: [
                        {
                            title: this.$t("920145"), // 类别名称
                            key: "category_name",
                            align: "center",
                        },
                        {
                            title: "刀头名称", // 刀柄名称
                            key: "name",
                            align: "center",
                        },
                        {
                            title: "刀头编号", // 刀柄编号
                            key: "number",
                            align: "center",
                        },
                        {
                            title: this.$t("900933"), // 料号
                            key: "part_no",
                            align: "center",
                        },
                    ],
                },
            ],
            tabledata2: [], // 几何参数table
            tableColumns2: [
                // 几何参数表头
                {
                    title: this.$t(920042),
                    // key: 'station',
                    align: "center",
                    children: [
                        {
                            title: this.$t("8000034"), // 属性名称
                            key: "name",
                            align: "center",
                        },
                        {
                            title: this.$t("600563"), // 属性值
                            key: "code",
                            align: "center",
                        },
                        {
                            title: this.$t("200017"), // 单位
                            key: "unit",
                            align: "center",
                        },
                    ],
                },
            ],
            tabledata3: [], // 工艺参数table
            tableColumns3: [
                // 工艺参数表头
                {
                    title: this.$t(920043),
                    // key: 'station',
                    align: "center",
                    children: [
                        {
                            title: this.$t("8000034"), // 属性名称
                            key: "name",
                            align: "center",
                        },
                        {
                            title: this.$t("600563"), // 属性值
                            key: "code",
                            align: "center",
                        },
                        {
                            title: this.$t("200017"), // 单位
                            key: "unit",
                            align: "center",
                        },
                    ],
                },
            ],
            tabledata4: [], // 属性配置的几何参数的date
            tableColumns4: [
                // 属性配置的几何参数的表头
                {
                    title: this.$t(920042),
                    // key: 'station',
                    align: "center",
                    children: [
                        {
                            title: this.$t("8000034"), // 属性名称
                            key: "name",
                            align: "center",
                        },
                        {
                            title: this.$t("600563"), // 属性值
                            key: "code",
                            align: "center",
                            render: (h, params) => {
                                if (params.row.type === 1) {
                                    return h("Input", {
                                        props: {
                                            value: params.row.value,
                                            size: "small",
                                        },
                                        on: {
                                            input: (val) => {
                                                this.tabledata4[
                                                    params.index
                                                ].value = val;
                                            },
                                        },
                                    });
                                } else {
                                    return h(
                                        "Select",
                                        {
                                            props: {
                                                transfer: true,
                                                value: params.row.value,
                                            },
                                            on: {
                                                "on-change": (event) => {
                                                    this.tabledata4[
                                                        params.index
                                                    ].value = event;
                                                },
                                            },
                                        },
                                        params.row.selectValue.map((item) => {
                                            return h("Option", {
                                                props: {
                                                    value: item.key,
                                                    label: item.value,
                                                },
                                            });
                                        })
                                    );
                                }
                            },
                        },
                        {
                            title: this.$t("200017"), // 单位
                            key: "unit",
                            align: "center",
                        },
                    ],
                },
            ],
            tabledata5: [], // 属性配置的工艺参数的date
            tableColumns5: [
                // 属性配置的工艺参数的表头
                {
                    title: this.$t(920043),
                    // key: 'station',
                    align: "center",
                    children: [
                        {
                            title: this.$t("8000034"), // 属性名称
                            key: "name",
                            align: "center",
                        },
                        {
                            title: this.$t("600563"), // 属性值
                            key: "key",
                            align: "center",
                            render: (h, params) => {
                                if (params.row.type === 1) {
                                    return h("Input", {
                                        props: {
                                            value: params.row.value,
                                            size: "small",
                                        },
                                        on: {
                                            input: (val) => {
                                                this.tabledata5[
                                                    params.index
                                                ].value = val;
                                            },
                                        },
                                    });
                                } else {
                                    return h(
                                        "Select",
                                        {
                                            props: {
                                                transfer: true,
                                                value: params.row.value,
                                            },
                                            on: {
                                                "on-change": (event) => {
                                                    this.tabledata5[
                                                        params.index
                                                    ].value = event;
                                                },
                                            },
                                        },
                                        params.row.selectValue.map((item) => {
                                            return h("Option", {
                                                props: {
                                                    value: item.key,
                                                    label: item.value,
                                                },
                                            });
                                        })
                                    );
                                }
                            },
                        },
                        {
                            title: this.$t("200017"), // 单位
                            key: "unit",
                            align: "center",
                        },
                    ],
                },
            ],
            modal: false,
            defendList: [],
            defendColumns: [
                {
                    title: "检测编号", // 属性名称
                    key: "seq",
                    align: "center",
                    render: (h, params) => {
                        return h("Input", {
                            props: {
                                value: params.row.seq,
                                size: "small",
                            },
                            on: {
                                input: (val) => {
                                    this.defendList[params.index].seq = val;
                                },
                            },
                        });
                    },
                },
                {
                    title: "检测类型", // 属性值
                    key: "Type",
                    align: "center",
                    render: (h, params) => {
                        return h(
                            "Select",
                            {
                                props: {
                                    value: params.row.type,
                                    size: "small",
                                    transfer: true,
                                },
                                on: {
                                    "on-change": (val) => {
                                        this.defendList[
                                            params.index
                                        ].type = val;
                                        if (val == "OK/NOK") {
                                            this.defendList[
                                                params.index
                                            ].disabled = true;
                                        } else {
                                            this.defendList[
                                                params.index
                                            ].disabled = false;
                                        }
                                    },
                                },
                            },
                            [
                                [
                                    h(
                                        "Option",
                                        {
                                            props: {
                                                value: "数值输入",
                                            },
                                        },
                                        "数值输入"
                                    ),
                                    h(
                                        "Option",
                                        {
                                            props: {
                                                value: "OK/NOK",
                                            },
                                        },
                                        "OK/NOK"
                                    ),
                                ],
                            ]
                        );
                    },
                },
                {
                    title: "检测工具", // 单位
                    key: "tools",
                    align: "center",
                    render: (h, params) => {
                        return h("Input", {
                            props: {
                                value: params.row.tools,
                                size: "small",
                            },
                            on: {
                                input: (val) => {
                                    this.defendList[params.index].tools = val;
                                },
                            },
                        });
                    },
                },
                {
                    title: "标准值", // 单位
                    key: "standardValues",
                    align: "center",
                    render: (h, params) => {
                        return h("Input", {
                            props: {
                                value: params.row.standardValues,
                                size: "small",
                                type: "text",
                                // disabled: params.row.disabled
                            },
                            on: {
                                input: (val) => {
                                    this.defendList[
                                        params.index
                                    ].standardValues = val;
                                },
                            },
                        });
                    },
                },
                {
                    title: "允许公差", // 单位
                    align: "center",
                    children: [
                        {
                            title: "上限", // 单位
                            key: "max",
                            align: "center",
                            render: (h, params) => {
                                return h("InputNumber", {
                                    props: {
                                        value: params.row.max,
                                        size: "small",
                                        min: 0,
                                        disabled: params.row.disabled,
                                    },
                                    on: {
                                        input: (val) => {
                                            this.defendList[
                                                params.index
                                            ].max = val;
                                        },
                                    },
                                });
                            },
                        },
                        {
                            title: "下限", // 单位
                            key: "mini",
                            align: "center",
                            render: (h, params) => {
                                return h("InputNumber", {
                                    props: {
                                        value: params.row.mini,
                                        size: "small",
                                        min: 0,
                                        disabled: params.row.disabled,
                                    },
                                    on: {
                                        input: (val) => {
                                            this.defendList[
                                                params.index
                                            ].mini = val;
                                        },
                                    },
                                });
                            },
                        },
                    ],
                },
                {
                    title: this.$t(1038), // 单位
                    key: "tools",
                    align: "center",
                    render: (h, params) => {
                        return h(
                            "Button",
                            {
                                props: {
                                    size: "small",
                                },
                                on: {
                                    click: () => {
                                        // console.log(params.row);
                                        // this.defendList[params.index].tools = val;
                                        if (params.row.id == 0) {
                                            this.defendList.splice(
                                                params.index,
                                                1
                                            );
                                        } else {
                                            let data = {
                                                id: params.row.id,
                                            };
                                            console.log(data);
                                            this.request(
                                                "/tlm/ToolTest/DelDetection",
                                                data,
                                                "get"
                                            ).then((res) => {
                                                if (res.data.ret == 1) {
                                                    this.$Message.success(
                                                        "删除成功"
                                                    );
                                                    this.getDefendList();
                                                }
                                            });
                                        }
                                    },
                                },
                            },
                            this.$t(1004)
                        );
                    },
                },
            ],
        };
    },
    components: {
        // pdf:pdf
    },
    created() {
        this.inittree();
    },
    methods: {
        renderContent(h, { root, node, data }) {
            let fcolor = "";
            if (data.toolid !== 0) {
                fcolor = "blue";
            }

            return h(
                "span",
                {
                    style: {
                        display: "inline-block",
                        width: "100%",
                    },
                    on: {
                        mouseover: (e) => {
                            document.getElementById(
                                "btn" + node.node.id
                            ).style.opacity = 1;
                        },
                        mouseout: (e) => {
                            document.getElementById(
                                "btn" + node.node.id
                            ).style.opacity = 0;
                        },
                    },
                },
                [
                    // h("span", [
                    h(
                        "span",
                        {
                            style: {
                                marginRight: "50px",
                                color: fcolor,
                            },
                        },
                        data.title
                    ),
                    h(
                        "span",
                        {
                            style: {
                                opacity: 0,
                            },
                            attrs: {
                                id: "btn" + node.node.id,
                            },
                        },
                        [
                            h(
                                "Button",
                                {
                                    props: {
                                        type: "success",
                                        ghost: true,
                                        size: "small",
                                    },
                                    style: {
                                        marginRight: "8px",
                                        display:
                                            data.toolid === 0
                                                ? "inline-block"
                                                : "none",
                                    },
                                    on: {
                                        click: () => {
                                            console.log(node.node.id);
                                            // node.node.selected = true
                                            this.treeselectnode =
                                                node.node;
                                            this.add();
                                            // this.treeselect();
                                        },
                                    },
                                },
                                [
                                    h("Icon", {
                                        props: {
                                            size: "20",
                                            type: "ios-add",
                                        },
                                    }),
                                ]
                            ),
                            h(
                                "Button",
                                {
                                    props: {
                                        type: "warning",
                                        ghost: true,
                                        size: "small",
                                    },
                                    style: {
                                        marginRight: "8px",
                                        display:
                                            data.toolid !== 0
                                                ? "inline-block"
                                                : "none",
                                    },
                                    on: {
                                        click: () => {
                                            // this.append(data);
                                            this.treeselectnode =
                                                node.node;
                                            // this.treeselect();
                                            this.edit();
                                        },
                                    },
                                },
                                [
                                    h("Icon", {
                                        props: {
                                            size: "20",
                                            type: "md-create",
                                        },
                                    }),
                                ]
                            ),
                            h(
                                "Button",
                                {
                                    props: {
                                        type: "error",
                                        ghost: true,
                                        size: "small",
                                    },
                                    style: {
                                        marginRight: "8px",
                                        display:
                                            data.toolid !== 0
                                                ? "inline-block"
                                                : "none",
                                    },
                                    on: {
                                        click: () => {
                                            // console.log(node.node.id);
                                             this.treeselectnode =
                                                node.node;
                                            this.dele(node.node.id);
                                        },
                                    },
                                },
                                [
                                    h("Icon", {
                                        props: {
                                            size: "20",
                                            type: "ios-close",
                                        },
                                    }),
                                ]
                            ),
                        ]
                    ),
                    // ]),
                ]
            );
            
        },
        inittree() {
            var params = {
                type: 4,
                keyword: this.keyword ? this.keyword : "",
            };
            this.axios
                .request({
                    url: "/tlm/ToolInfo/GetToolTree",
                    params,
                    method: "get",
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        this.tree_data = [];
                        res.data.data.expand = true;
                        this.tree_data.push(res.data.data);
                    } else {
                        this.$Message.error(this.$t("" + res.data.msg));
                    }
                });
        },
        add() {
            this.type = 1;
            // console.log(
            //     this.treeselectnode.id === 0,
            //     this.treeselectnode.toolid !== 0
            // );
            if (!this.treeselectnode.id) {
                this.$Message.error("请先选择刀头类别");
                return false;
            }
            this.addobj = {
                // grind_times:0,
                // use_times:0,
                cname: this.treeselectnode.title,
            };
            
            this.image_name = "";
            this.pdf_name = "";
            this.addobj.pdf_name = "";
            this.remoteMethod2("");
            this.getadddata(1);
            this.add_model = true;
        },
        getadddata(num) {
            this.initadddata(this.treeselectnode.id, num);
            this.getToolBase(this.treeselectnode.id, num);
        },
        getToolBase(id, num) {
            var params = {
                id: this.treeselectnode.id, // id
            };
            this.axios
                .request({
                    url: "/tlm/ToolInfo/GetItem",
                    params,
                    method: "get",
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        if (num === 1) {
                            this.addobj.cname = this.treeselectnode.name;
                        } else {
                            this.addobj.toolid = this.treeselectnode.toolid;
                            this.addobj.cname = res.data.data.category_name;
                            this.addobj.number = res.data.data.number;
                            this.addobj.material_id = res.data.data.material_id;
                            this.addobj.partno = res.data.data.part_no;
                            this.addobj.need_test = res.data.data.need_test;

                            this.image_name = res.data.data.image_name;
                        }
                    } else {
                        // this.$Message.error(this.$t(''+ res.data.msg))
                    }
                });
        },
        handleFormatError(file) {
            this.$Message.error(
                this.$t("950396") + file.name + this.$t("950397")
            );
        },
        handleSuccess(res, file) {
            if (res.ret == 1) {
                this.$Message.success(this.$t("1"));
                this.image_name = file.name;
                this.pdf_name = res.data;
                this.addobj.pdf_name = res.data;
            } else {
                if (("" + res.msg).indexOf(",") != -1) {
                    inittip(res.msg);
                } else {
                    this.$Message.error(this.$t("" + res.msg));
                }
            }
        },
        handleError() {
            this.$Message.error(this.$t("2010006") + "!");
        },
        remoteMethod2(query) {
            // 刀具名称搜索
            if (query !== "") {
                this.loading2 = true;
                setTimeout(() => {
                    this.loading2 = false;
                    var params = {
                        keyword: query,
                    };
                    this.axios
                        .request({
                            url: "/tlm/ToolInfo/GetMaterialList",
                            params,
                            method: "get",
                        })
                        .then((res) => {
                            if (res.data.ret == 1) {
                                this.tool_namelist = res.data.data;
                            }
                        });
                }, 200);
            } else {
                this.remoteMethod2();
            }
        },
        initadddata(id, num) {
            var params = {
                categoryId: this.treeselectnode.id,
                type: num,
            };
            this.axios
                .request({
                    url: "/tlm/ToolInfo/GetCategoryParameterList",
                    params,
                    method: "get",
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        if (res.data.data.geometryParameter.length > 0) {
                            res.data.data.geometryParameter.forEach((ele) => {
                                ele.key = ele.id;
                                ele.value = ele.code;
                            });
                            this.tabledata4 = res.data.data.geometryParameter;
                        } else {
                            this.tabledata4 = [];
                        }
                        if (res.data.data.technologyParameter.length > 0) {
                            res.data.data.technologyParameter.forEach((ele) => {
                                ele.key = ele.id;
                                ele.value = ele.code;
                            });
                            this.tabledata5 = res.data.data.technologyParameter;
                        } else {
                            this.tabledata5 = [];
                        }
                    } else {
                        this.$Message.error(this.$t("" + res.data.msg));
                    }
                });
        },
        name_change(val) {
            if (!val) {
                this.remoteMethod2("");
                return false;
            }
            this.addobj.name = val ? val.label : "";
            this.tool_namelist.forEach((ele) => {
                if (val.value === ele.id) {
                    this.addobj.partno = ele.part_no;
                }
            });
        },
        treeselect(value) {
            var arrx = this.$refs.tree.getSelectedNodes();
            if (arrx.length == 0) {
                return;
            }
            this.treeselectnode = arrx[0];
            var params = {};
            if (arrx[0].children.length == 0) {
                params = {
                    id: this.treeselectnode.id, // 树节点
                };
            } else {
                return;
            }
            this.axios
                .request({
                    url: "/tlm/ToolInfo/GetItem",
                    params,
                    method: "get",
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        if (res.data.data.image !== "") {
                            this.src =
                                this.axios.publicPath + res.data.data.image;
                            this.addobj.pdf_name = res.data.data.image;
                            this.pdf_name = res.data.data.image;
                        } else {
                            this.src = "";
                            this.addobj.pdf_name = "";
                            this.pdf_name = "";
                        }
                        var arr = [];
                        arr.push({
                            category_name: res.data.data.category_name,
                            name: res.data.data.name,
                            number: res.data.data.number,
                            part_no: res.data.data.part_no,
                        });
                        this.tabledata1 = arr;
                        if (this.treeselectnode.toolid === 0) {
                            this.tabledata1 = [];
                        }
                        if (res.data.data.geometryParameter.length > 0) {
                            res.data.data.geometryParameter.forEach((ele) => {
                                ele.type_value =
                                    ele.type === 1
                                        ? this.$t(800016)
                                        : this.$t(8109);
                                ele.code = ele.value;
                            });
                            this.tabledata2 = res.data.data.geometryParameter;
                        } else {
                            this.tabledata2 = [];
                        }
                        if (res.data.data.technologyParameter.length > 0) {
                            res.data.data.technologyParameter.forEach((ele) => {
                                ele.type_value =
                                    ele.type === 1
                                        ? this.$t(800016)
                                        : this.$t(8109);
                                ele.code = ele.value;
                            });
                            this.tabledata3 = res.data.data.technologyParameter;
                        } else {
                            this.tabledata3 = [];
                        }
                    } else {
                        if (this.treeselectnode.id === 0) {
                            this.tabledata1 = [];
                            this.tabledata2 = [];
                            this.tabledata3 = [];
                        } else {
                            // this.$Message.error(this.$t(''+res.data.msg))
                        }
                    }
                });
        },
        getToolInfo() {
            var params = {
                id: this.treeselectnode.id, // this.treeselectnode.id
            };
            this.axios
                .request({
                    url: "/tlm/ToolInfo/GetItem",
                    params,
                    method: "get",
                })
                .then((res) => {
                    if (
                        res.data.ret === 1 &&
                        this.treeselectnode.toolid !== 0
                    ) {
                        if (res.data.data.image) {
                            this.addobj.pdf_name = res.data.data.image;
                            this.pdf_name = res.data.data.image;
                            // this.image_name=res.data.data.image_name
                        } else {
                            this.addobj.pdf_name = "";
                            this.pdf_name = "";
                            // this.image_name=''
                        }
                    } else {
                        // this.$Message.error(this.$t(''+res.data.msg))
                    }
                });
        },
        adddata() {
            if (
                !this.addobj.material_id ||
                !this.addobj.number ||
                !this.addobj.need_test
            ) {
                this.$Message.error("请输入必填选项");
                return false;
            }
            this.addobj.id = this.treeselectnode.id;
            this.addobj.category_id = this.treeselectnode.id;
            this.addobj.category_name = this.treeselectnode.title;
            this.addobj.type = 4;
            this.addobj.name = this.addobj.cname;
            this.addobj.category_name = this.addobj.cname; //
            this.addobj.image_name = this.image_name;
            this.addobj.image = this.addobj.pdf_name
                ? this.addobj.pdf_name
                : "";
            this.addobj.paraValue = [];
            this.addobj.need_test = this.addobj.need_test
                ? this.addobj.need_test
                : 2;
            // console.log(this.tabledata5.length )
            if (this.tabledata4.length > 0) {
                this.addobj.paraValue = this.addobj.paraValue.concat(
                    this.tabledata4
                );
            }
            if (this.tabledata5.length > 0) {
                this.addobj.paraValue = this.addobj.paraValue.concat(
                    this.tabledata5
                );
            }
            // var arr = []
            // this.addobj.paraValue = arr.concat(this.tabledata4, this.tabledata5)
            this.axios
                .request({
                    url: "/tlm/ToolInfo/Add",
                    data: this.addobj,
                    method: "post",
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        this.add_model = false;
                        this.$Message.success(this.$t("1"));
                        if (this.type === 1) {
                            this.inittree();
                        } else if (this.type === 2) {
                            console.log(this.treeselectnode.id);
                            this.treeselect('');
                        }
                    } else {
                        this.$Message.error(this.$t("" + res.data.msg));
                    }
                });
        },
        edit() {
            // this.treeselectnode.id = this.treeselectnode.id;
            if (
                !this.treeselectnode.id 
            ) {
                this.$Message.error("请先选择刀头");
                return false;
            }

            this.addobj = {
                grind_times: 0,
                use_times: 0,
                cname: this.treeselectnode.title,
            };
            this.type = 2;
            this.remoteMethod2("");
            this.getadddata(2);
            this.add_model = true;

            // var params = {
            //   id: this.treeselectnode.id,
            // }
            // this.axios.request({
            //   url: 'tlm/Category/GetCategory',
            //   params,
            //   method: 'get'
            // }).then(res => {
            //   if (res.data.ret === 1) {
            //     this.type = 2;
            //     this.addobj = res.data.data;
            //     this.add_model = true
            //   } else {
            //     this.$Message.error(this.$t(""+res.data.msg))
            //   }
            // })
        },
        dele() {
            if (this.treeselectnode.toolid === undefined) {
                this.$Message.error("请先选择刀头");
                return false;
            }
            this.$Modal.confirm({
                title: this.$t("1018"),
                content: "",
                width: "290px",
                closable: true,
                okText: this.$t("1004"),
                onOk: () => {
                    var data = {
                        id: this.treeselectnode.id,
                    };
                    this.axios
                        .request({
                            url:
                                "/tlm/ToolInfo/Delete?id=" +
                                this.treeselectnode.id,
                            data,
                            method: "post",
                        })
                        .then((res) => {
                            if (res.data.ret === 1) {
                                this.$Message.success(this.$t("1"));
                                this.inittree();
                                this.tabledata1 = [];
                                this.tabledata2 = [];
                                this.tabledata3 = [];
                                this.treeselectnode = {};
                            } else {
                                this.$Message.error(this.$t("" + res.data.msg));
                            }
                        });
                },
                onCancel: () => {
                    return false;
                },
            });
        },
        handleFormatError2(file) {
            this.$Message.error(
                this.$t("950396") + file.name + this.$t("950397")
            );
        },
        handleSuccess2(res, file) {
            if (res.ret == 1) {
                this.$Message.success(this.$t("1"));
                this.inittree();
            } else {
                if (("" + res.msg).indexOf(",") != -1) {
                    inittip(res.msg);
                } else {
                    this.$Message.error(this.$t("" + res.msg));
                }
            }
        },
        handleError2() {
            this.$Message.error(this.$t("2010006") + "!");
        },
        getDefendList() {
            let data = {
                Page: 0,
                PageSize: 0,
                Id: this.treeselectnode.id,
                ProjectId: 0,
                ToolType: 4,
            };
            this.request("/tlm/ToolTest/GetDetection", data, "post").then(
                (res) => {
                    // console.log(res.data.data);
                    if (res.data.ret == 1) {
                        if (res.data.data.length > 0) {
                            let arr = [];
                            arr = res.data.data;
                            arr.forEach((ele) => {
                                if (ele.type == "OK/NOK") {
                                    ele.disabled = true;
                                } else {
                                    ele.disabled = false;
                                }
                            });
                            this.defendList = arr;
                        }
                    }
                }
            );
        },
        toolDefend() {
            if (!this.treeselectnode.id) {
                this.$Message.error("请选择刀头");
                return false;
            }
            if (this.treeselectnode.children.length > 0) {
                this.$Message.error("请选择刀头名称");
                return false;
            }
            this.getDefendList();

            this.modal = true;
        },
        addItem() {
            let obj = {
                id: 0,
                seq: "",
                Type: "",
                tools: "",
                standardValues: "",
                max: 0,
                mini: 0,
                ToolType: 4,
                ToolId: this.treeselectnode.id,
                disabled: false,
            };
            this.defendList.push(obj);
        },
        submitList() {
            let data = [];
            var bool = false,
                type2 = false;
            if (this.defendList.length == 0) {
                this.modal = false;
                return;
            }
            this.defendList.forEach((ele) => {
                if (ele.seq == "" || ele.type == "" || ele.tools == "") {
                    bool = true;
                } else if (ele.max < ele.mini) {
                    type2 = true;
                } else {
                    data.push(ele);
                }
            });
            if (bool) {
                this.$Message.error("请输入测试项目维护信息");
                return false;
            }
            if (type2) {
                this.$Message.error("上限值应大于下限值");
                return false;
            }
            // return false;
            this.request("/tlm/ToolTest/SetDetection", data, "post").then(
                (res) => {
                    console.log(res.data);
                    if (res.data.ret == 1) {
                        this.$Message.success(this.$t(900345));
                        this.modal = false;
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                }
            );
        },
        downloadTest() {
            var params = {
                name: "ToollifeDetectionTool",
            };
            this.axios
                .request({
                    url: "/config/download/GetTemplate",
                    params,
                    method: "get",
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        window.location.href =
                            this.axios.publicPath + res.data.data;
                    } else {
                        this.$Message.error(this.$t("" + res.data.msg));
                    }
                });
        },
        request(url, data, type) {
            if (type == "get") {
                return this.axios
                    .request({
                        url: url,
                        params: data,
                        method: type,
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            } else {
                return this.axios
                    .request({
                        url: url,
                        data,
                        method: type,
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            }
        },
    },
};
</script>
<style src="../../../style/edit.css"></style>
<style lang="less" scoped>
.div_2D {
    width: 80px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    background-color: #2d8cf0;
    color: #fff;
    font-size: 16px;
}
.div_2D_img {
    height: 400px;
}
</style>
