<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>统计指标：</label>
                <Select
                    v-model="kpi"
                    label-in-value
                    clearable
                    class="searchSelect"
                    @on-change="changeTable"
                >
                    <Option :value="1">设备维度</Option>
                    <Option :value="2">模具维度</Option>
                </Select>
            </div>
            <div class="filter" v-show="kpi == 1">
                <label>产线层级：</label>
                <Cascader
                    class="searchInput"
                    :data="datalevel"
                    v-model="leveldata"
                    trigger="click"
                    @on-change="changelevel"
                    style="width: 200px"
                ></Cascader>
            </div>
            <div class="filter" v-show="kpi == 1">
                <label>设备名称：</label>
                <Select
                    v-model="machineId"
                    label-in-value
                    clearable
                    class="searchSelect"
                >
                    <Option
                        v-for="item in machinedata"
                        :value="item.id"
                        :key="item.id"
                        >{{ item.title }}</Option
                    >
                </Select>
            </div>
            <div class="filter" v-show="kpi == 1">
                <label>{{ $t(8205) }}：</label>
                <Input
                    class="searchInput"
                    type="text"
                    v-model="toolLocaltion"
                    clearable
                />
            </div>
            <div class="filter" v-show="kpi == 2">
                <label>模具编号：</label>
                <Input
                    class="searchInput"
                    type="text"
                    v-model="toolNumber"
                    clearable
                />
            </div>
            <div class="filter" v-show="kpi == 2">
                <label>供应商：</label>
                <Select
                    v-model="businessId"
                    label-in-value
                    clearable
                    class="searchSelect"
                >
                    <Option
                        v-for="item in supplierList"
                        :value="item.id"
                        :key="item.id"
                        >{{ item.text }}</Option
                    >
                </Select>
            </div>
            <!-- <div v-show="!morecondition"> -->
            <div class="filter">
                <label>安装{{ $t(900908) }}：</label>
                <DatePicker
                    type="datetimerange"
                    format="yyyy-MM-dd HH:mm:ss"
                    :clearable="false"
                    v-model="index_time"
                    @on-change="changeTime"
                    class="searchInput MaxWidth"
                ></DatePicker>
            </div>
            <div class="filter">
                <label> <span>安装人员</span>： </label>
                <Select
                    v-model="user"
                    label-in-value
                    clearable
                    class="searchSelect"
                >
                    <Option
                        v-for="item in userList"
                        :value="item.id"
                        :key="item.id"
                        >{{ item.name }}</Option
                    >
                </Select>
            </div>
            <!-- </div> -->
            <div class="searchBtn">
                <Button
                    type="primary"
                    class="twoWord"
                    icon="ios-search"
                    @click="search(0)"
                    >{{ $t("1001") }}</Button
                >
                <Button
                    type="primary"
                    class="fourWord"
                    icon="ios-cloud-download"
                    @click="search(1)"
                    >{{ $t("1007") }}</Button
                >
            </div>
            <!-- <MoreConbtn @getCon="morecondition = !morecondition" :morecondition="morecondition"></MoreConbtn> -->
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                v-show="kpi == 1"
                highlight-row
                :data="tableData"
                :columns="tableColumns1"
            ></Table>
            <Table
                class="tableClass"
                v-show="kpi == 2"
                highlight-row
                :data="tableData"
                :columns="tableColumns2"
            ></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>
    </div>
</template>

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

export default {
    data() {
        return {
            morecondition: false,
            kpi: 1,
            index_time: [],
            user: "",
            userList: [],
            name: "",
            supplier: "",
            supplierList: [],
            page: 1,
            pagesize: 10,
            total: 0,
            tableData: [],
            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: "设备",
                    key: "machineName",
                    render: (h, params) => {
                        let str = "";
                        str =
                            params.row.machineName +
                            "(" +
                            params.row.machineCode +
                            ")";
                        return h("span", str);
                    },
                },
                {
                    title: this.$t(8205),
                    key: "toolLocaltion",
                },
                {
                    title: "模具编号",
                    key: "toolCode",
                },
                {
                    title: "模具条形码",
                    key: "serialNumber",
                },
                {
                    title: "模具名称",
                    key: "toolName",
                },

                {
                    title: "安装时间",
                    key: "installTime",
                },
                {
                    title: "安装人员",
                    key: "installUser",
                },
                {
                    title: "卸载时间",
                    key: "uninstallTime",
                },

                // {
                //     title: this.$t(700100),
                //     key: "reviewer",
                // },
            ],
            tableColumns2: [
                {
                    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: "serialNumber",
                },
                {
                    title: "模具名称",
                    key: "toolName",
                },
                {
                    title: "模具编号",
                    key: "toolCode",
                },

                {
                    title: "供应商",
                    key: "supplier",
                },
                {
                    title: "设备",
                    key: "machineName",
                },
                {
                    title: "安装人员",
                    key: "installUser",
                },
                {
                    title: "安装时间",
                    key: "installTime",
                },
                {
                    title: "加工件数",
                    key: "life",
                },
                {
                    title: "卸载时间",
                    key: "uninstallTime",
                },
                {
                    title: "卸载原因",
                    key: "uninstallReason",
                    render: (h, params) => {
                        let str = "";
                        switch (params.row.uninstallReason) {
                            case "1":
                                str = "损坏";
                                break;
                            case "2":
                                str = "磨损";
                                break;
                            case "3":
                                str = "其他原因";
                                break;
                        }
                        return h("span", str);
                    },
                },
            ],
            datalevel: [],
            leveldata: [],
            sectionId: 0,
            machineId: "",
            machinedata: [],
            toolLocaltion: "",
            toolNumber: "",
            businessId: 0,
            initDate: [],
        };
    },
    created() {
        this.getUser();
        this.getSupplers();
        this.initlevel();
        this.initDate = [
            moment(new Date().getTime() - 3600 * 1000 * 24 * 6).format(
                "YYYY-MM-DD "
            ) + "00:00:00",
            moment(new Date().getTime()).format("YYYY-MM-DD ") + "23:59:59",
        ];
        this.index_time = this.initDate;
        // this.search(0);
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1);
        this.tableColumns2 = this.$time.initTableTitle(this.tableColumns2);
    },
    methods: {
        getUser() {
            let data = {
                page: 1,
                pagesize: 9999,
            };
            this.request("/config/user/GetUsers", data, "get").then((res) => {
                if (res.data.ret == 1) {
                    this.userList = res.data.data;
                }
            });
        },
        getSupplers() {
            this.request("config/Materials/GetSuppliers", "", "get").then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.supplierList = res.data.data;
                    }
                }
            );
        },
        initlevel() {
            // 初始化产线层级
            this.request(
                "config/ProjectLevelSection/GetSectionTree",
                "",
                "get"
            ).then((res) => {
                // const arr = [];
                if (res.data.data.length > 0) {
                    this.datalevel = res.data.data;
                    // arr.push(res.data.data[0].value);
                    // this.leveldata = arr;
                }
            });
        },
        changelevel(data) {
            // 切换产线层级，触发事件
            // 请求数据
            this.leveldata = data;
            this.sectionId = data[data.length - 1];
            this.getMachine();
        },
        changeTime(data) {
            this.index_time = data;
        },
        changeTable(data) {
            // if(data == 1){
            this.leveldata = [];
            this.machineId = "";
            this.toolLocaltion = "";
            // }
            // if (data==2) {
            this.toolNumber = "";
            this.supplier = "";
            // }
            this.user = "";
            this.index_time = this.initDate;
            this.total = 0;
            this.tableData = [];
        },
        getMachine() {
            let data = {
                section: this.sectionId,
            };
            this.request(
                "/ims/ToolInstall/GetMachineBySection",
                data,
                "get"
            ).then((res) => {
                if (res.data.ret === 1) {
                    this.machinedata = res.data.data;
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        search(toExcel) {
            let start = 0,
                end = 0;
            if (this.index_time.length > 0) {
                start = Date.parse(this.index_time[0]) / 1000;
                end = Date.parse(this.index_time[1]) / 1000;
            }

            let data = {
                type: this.kpi,
                toolNumber: this.toolNumber,
                businessId: this.businessId,
                start: start,
                end: end,
                installUser: this.user,
                toExcel: toExcel,
                machineId: this.machineId,
                toolLocaltion: this.toolLocaltion,
                page: this.page,
                pagesize: this.pagesize,
                section: this.sectionId,
            };
            this.request(
                "/ims/ToolInstall/GetToolInstallInfo",
                data,
                "post"
            ).then((res) => {
                console.log(res.data);
                if (res.data.ret == 1) {
                    if (toExcel == 0) {
                        this.total = res.data.total;
                        this.tableData = res.data.data;
                    }
                    if (toExcel == 1) {
                        window.location.href =
                            axios.publicPath + "" + res.data.data;
                    }
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        dataPost(data) {},
        changePage(value) {
            // 换页事件
            this.page = value;

            this.tableData = this.search(0);
        },
        handleRowChange(currentRow, oldCurrentRow) {
            // 选中一条数据，触发事件
            this.selectedObj = currentRow;
            // console.log(this.selectedObj)
        },
        handlePageSize(value) {
            // 切换分页页数，触发事件
            this.pagesize = value;
            this.tableData = this.search(0);
        },
        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>
</style>
