<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>{{ $t("1025") }}：</label>
                <Cascader
                    class="searchInput"
                    :data="datalevel"
                    :value="leveldata1"
                    trigger="click"
                    @on-change="handlelevelchange1"
                ></Cascader>
            </div>
            <div class="filter">
                <label>{{ $t("960432") }}：</label>
                <Select class="searchSelect" v-model="machineId" clearable>
                    <Option
                        v-for="item in machineList"
                        :value="item.id"
                        :key="item.id"
                        >{{ item.title }}</Option
                    >
                </Select>
            </div>
            <div class="filter">
                <label>{{ $t(900908) }}：</label>
                <DatePicker
                    type="datetimerange"
                    format="yyyy-MM-dd HH:mm:ss"
                    @on-change="timechange1"
                    :value="datetime"
                    placement="bottom-end"
                    split-panels
                    placeholder
                    class="searchInput MaxWidth"
                ></DatePicker>
            </div>
            <div class="filter">
                <label>{{ $t(700095) }}：</label>
                <Select class="searchSelect" v-model="status" clearable>
                    <Option :value="0">未审核</Option>
                    <Option :value="1">审核中</Option>
                    <Option :value="2">已审核</Option>
                    <Option :value="3">未通过</Option>
                </Select>
            </div>
            <div class="searchBtn">
                <Button
                    type="primary"
                    class="twoWord"
                    icon="ios-search"
                    @click="search(1,10)"
                    >{{ $t("1001") }}</Button
                >
                <Button
                    type="success"
                    class="twoWord"
                    icon="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="fourWord"
                    icon="md-cloud-download"
                    @click="loadexcel()"
                    >{{ $t("1007") }}</Button
                >
                <Button
                    type="primary"
                    class="fourWord"
                    icon="md-checkmark-circle-outline"
                    @click="checkPost()"
                    >提交审核</Button
                >
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                @on-current-change="handleRowChange"
                highlight-row
                :data="tableData1"
                :columns="tableColumns1"
            ></Table>
            <div class="pageDiv">
                <div class="pageDirection">
                    <Page
                        :total="total"
                        :current="page"
                        @on-change="changePage"
                        show-elevator
                        show-sizer
                        show-total
                        @on-page-size-change="handlePageSize"
                        :page-size-opts="[10, 20, 30, 40, 100]"
                    ></Page>
                </div>
            </div>
        </div>
        <Modal
            class="newModalClass"
            v-model="modal"
            :styles="{ width: '650px' }"
        >
            <div slot="header">
                <p v-show="type == 1">添加</p>
                <p v-show="type == 2">修改</p>
            </div>
            <div class="modal_body">
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{ $t("1132") }}：
                    </label>
                    <Cascader
                        class="searchInput"
                        :data="datalevel"
                        :value="leveldata"
                        trigger="click"
                        @on-change="handlelevelchange"
                    ></Cascader>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{ $t("960432") }}：
                    </label>
                    <Select class="searchSelect" v-model="machineId2">
                        <Option
                            v-for="item in machineList"
                            :value="item.id"
                            :key="item.id"
                            >{{ item.title }}</Option
                        >
                    </Select>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{ $t("600011") }}：
                    </label>
                    <Input
                        type="text"
                        v-model="supplier"
                        class="searchInput"
                    ></Input>
                </div>
                <div class="filter">
                    <label> <i>*</i>合同金额： </label>
                    <InputNumber
                        v-model="price"
                        :min="0"
                        class="searchInput"
                    ></InputNumber>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{ $t(800219) }}：
                    </label>
                    <DatePicker
                        type="date"
                        @on-change="timechange"
                        :value="repairDate"
                        placement="bottom-end"
                        split-panels
                        placeholder
                        class="searchInput"
                    ></DatePicker>
                </div>
                <div class="filter">
                    <label>
                        <i>*</i>
                        {{ $t(960580) }}：
                    </label>
                    <Select class="searchSelect" v-model="processId">
                        <Option
                            v-for="item in processList"
                            :value="item.id"
                            :key="item.id"
                            >{{ item.title }}</Option
                        >
                    </Select>
                </div>
            </div>
            <div slot="footer">
                <Button
                    type="text"
                    size="large"
                    @click="modal = false"
                    v-text="$t('1033')"
                    >取消</Button
                >
                <Button
                    type="primary"
                    size="large"
                    @click="postdata()"
                    v-text="$t('1011')"
                    >确定</Button
                >
            </div>
        </Modal>
    </div>
</template>

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

export default {
    data() {
        return {
            datalevel: [],
            leveldata: [],
            leveldata1: [],
            machineId: "",
            machineList: "",
            datetime: [],
            status: "",
            total: 0,
            page: 1,
            pagesize: 10,
            detailobj: {},
            tableData1: [],
            tableColumns1: [
                {
                    type: "index2",
                    title: this.$t("1008"), // 序号
                    key: "xuhao",
                    render: (h, params) => {
                        return h(
                            "span",
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    },
                },
                {
                    title: "产线层级",
                    key: "sectionName",
                },
                {
                    title: "设备",
                    key: "machineName",
                },
                {
                    title: "委外供应商",
                    key: "supplier",
                },
                {
                    title: "合同金额",
                    key: "price",
                },
                {
                    title: "维修时间",
                    key: "repairTime",
                    render: (h, params) => {
                        let str = "";
                        str = moment(params.row.repairTime * 1000).format(
                            "YYYY-MM-DD"
                        );
                        return h("div", str);
                    },
                },
                {
                    title: "审核状态",
                    key: "state",
                    render: (h, params) => {
                        let str = "";
                        switch (params.row.state) {
                            case 0:
                                str = "未审核";
                                break;
                            case 1:
                                str = "审核中";
                                break;
                            case 2:
                                str = "已审核";
                                break;
                            case 3:
                                str = "未通过";
                                break;
                        }
                        return h("div", str);
                    },
                },
                {
                    title: "提交人",
                    key: "editorName",
                },
            ],
            type: 0,
            modal: false,
            supplier: "",
            machineId2: "",
            repairDate: "",
            processId: "",
            processList: [],
            price: 0,
            checkProcess: [],
            testArr: [],
        };
    },
    created() {
        this.initlevel();
        this.datetime = [
            moment(new Date().getTime() - 3600 * 24 * 7 * 1000).format(
                "YYYY-MM-DD "
            ) + "00:00:00",
            moment(new Date().getTime()).format("YYYY-MM-DD ") + "23:59:59",
        ];
        this.getProcess();
        this.search(1,10);
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1);
    },
    methods: {
        initlevel() {
            // 初始化产线层级
            this.axios
                .request({
                    url: "config/ProjectLevelSection/GetSectionTree",
                    method: "get",
                })
                .then((res) => {
                    const arr = [];
                    if (res.data.data.length > 0) {
                        this.datalevel = res.data.data;
                    }
                });
        },
        handlelevelchange(val) {
            this.leveldata = val;
            this.machineList = [];
            this.getMachine(this.leveldata[this.leveldata.length - 1]);
        },
        handlelevelchange1(val) {
            this.leveldata1 = val;
            this.machineList = [];
            this.getMachine(this.leveldata1[this.leveldata1.length - 1]);
            
        },
        getMachine(sectionid) {

            var params = {
                section: sectionid,
            };
            this.axios
                .request({
                    url: "/tlm/ToolInstall/GetMachineBySection",
                    params,
                    method: "get",
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        this.machineList = res.data.data;
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                });
        },
        getProcess() {
            // 获取审核流程
            this.request("/esop/EsopProcess/GetAllProcess", "", "get").then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.processList = res.data.data;
                    }
                }
            );
        },
        timechange(val) {
            this.repairDate = val;
        },
        timechange1(val) {
            this.datetime = val;
        },
        search(page,pagesize) {
            let start = 0,
                end = 0,
                section = 0;
            start = Date.parse(this.datetime[0]) / 1000;
            end = Date.parse(this.datetime[1]) / 1000;
            section = this.leveldata1[this.leveldata1.length - 1];
            let data = {
                page: page,
                pageSize: pagesize,
                section: section,
                machine: this.machineId,
                state: this.status,
                repairstart: start,
                repairend: end,
                editstart: "",
                editend: "",
                toExcel: "0",
                projectId: 0,
            };
            // return
            this.request(
                "/tpm/OutsourcedMaintenance/GetOutsourcedMaintenance",
                data,
                "post"
            ).then((res) => {
                if (res.data.ret == 1) {
                    this.total = res.data.total;
                    this.tableData1 = res.data.data;
                }
            });
        },
        add() {
            this.leveldata = [];
            this.machineId2 = "";
            this.supplier = "";
            this.price = 0;
            this.repairDate = "";
            this.processId = "";
            this.type = 1;
            this.modal = true;
        },
        edit() {
            if (!this.detailobj.id) {
                this.$Message.error(this.$t(9000396));
                return false;
            }
            if (this.detailobj.state != 0 && this.detailobj.state != 3) {
                this.$Message.error("当前状态无法修改");
                return false;
            }

            this.showEdit(this.datalevel, this.detailobj.section);
            this.leveldata = this.testArr.reverse();

            this.getMachine(this.detailobj.section);
            this.machineId2 = this.detailobj.machineId;
            this.supplier = this.detailobj.supplier;
            this.price = this.detailobj.price;
            this.repairDate = moment(this.detailobj.repairTime * 1000).format(
                "YYYY-MM-DD"
            );
            this.processId = this.detailobj.processId;
            this.type = 2;
            this.modal = true;
        },
        showEdit(nodes = [], id) {
            for (let i = 0; i < nodes.length; i++) {
                const node = nodes[i];
                if (node.value === id) {
                    this.testArr.push(node.value);
                    return true;
                }

                if (Array.isArray(node.children)) {
                    if (this.showEdit(node.children, id)) {
                        this.testArr.push(node.value);
                        return true;
                    }
                }
            }
            return false;
        },
        postdata() {
            if (this.leveldata.length == 0) {
                this.$Message.error("请选择产线层级");
                return false;
            }
            if (this.machineId2 == "") {
                this.$Message.error("请选择设备");
                return false;
            }
            if (this.supplier == "") {
                this.$Message.error("请填写供应商");
                return false;
            }
            if (this.price == 0) {
                this.$Message.error("请填写金额");
                return false;
            }
            if (this.repairDate == "") {
                this.$Message.error("请选择维修时间");
                return false;
            }
            if (this.process == "") {
                this.$Message.error("请选择审核流程");
                return false;
            }
            let time = 0;
            if (this.repairDate != "") {
                time = Date.parse(this.repairDate) / 1000;
                console.log(time);
            }
            let data = {
                id: this.type == 1 ? 0 : this.detailobj.id,
                processId: this.processId,
                repairTime: time,
                price: this.price,
                supplier: this.supplier,
                machineId: this.machineId2,
            };
            this.request(
                "/tpm/OutsourcedMaintenance/SetOutsourcedMaintenance",
                data,
                "post"
            ).then((res) => {
                if (res.data.ret == 1) {
                    this.$Message.success(this.$t(res.data.msg));
                    this.search(1,10);
                    this.modal = false;
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        dele() {
            if (!this.detailobj.id) {
                this.$Message.error(this.$t(9000396));
                return false;
            }
            if (this.detailobj.state != 0 && this.detailobj.state != 3) {
                this.$Message.error("当前状态无法删除");
                return false;
            }
            this.$Modal.confirm({
                title: this.$t("1018"),
                content: "",
                width: "290px",
                closable: true,
                okText: this.$t("1004"),
                onOk: () => {
                    var data = {
                        ids: this.detailobj.id,
                    };
                    // return;
                    this.request(
                        "/tpm/OutsourcedMaintenance/DelOutsourcedMaintenance",
                        data,
                        "get"
                    ).then((res) => {
                        if (res.data.ret === 1) {
                            this.search(1,10);
                            this.$Message.success(this.$t(res.data.msg));
                        } else {
                            this.$Message.error(this.$t(res.data.msg));
                            return false;
                        }
                    });
                },
                onCancel: () => {
                    return false;
                },
            });
        },
        loadexcel() {
            let start = 0,
                end = 0,
                section = 0;
            start = Date.parse(this.datetime[0]) / 1000;
            end = Date.parse(this.datetime[1]) / 1000;
            section = this.leveldata1[this.leveldata1.length - 1];
            let data = {
                page: this.page,
                pageSize: this.pagesize,
                section: section,
                machine: this.machineId,
                state: this.status,
                repairstart: start,
                repairend: end,
                editstart: "",
                editend: "",
                toExcel: "1",
                projectId: 0,
            };
            // return
            this.request(
                "/tpm/OutsourcedMaintenance/GetOutsourcedMaintenance",
                data,
                "post"
            ).then((res) => {
                if (res.data.ret == 1) {
                    window.location.href =
                        axios.publicPath + "" + res.data.data;
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        checkPost() {
            //审核未通过的修改完成后才能提交审核
            if (!this.detailobj.id) {
                this.$Message.error(this.$t(9000396));
                return false;
            }
            if (this.detailobj.state != 0 && this.detailobj.state != 3) {
                this.$Message.error("当前状态无法提交审核");
                return false;
            }
            this.$Modal.confirm({
                title: "确认提交审核吗",
                content: "",
                width: "290px",
                closable: true,
                okText: this.$t("1011"),
                onOk: () => {
                    let data = {
                        ids: this.detailobj.id,
                        state: 1,
                    };
                    this.request(
                        "/tpm/OutsourcedMaintenance/SubmitOutsourcedMaintenance",
                        data,
                        "post"
                    ).then((res) => {
                        if (res.data.ret == 1) {
                            this.$Message.success(this.$t(res.data.msg));
                            this.search(1,10);
                        } else {
                            this.$Message.error(this.$t(res.data.msg));
                        }
                    });
                },
                onCancel: () => {
                    return false;
                },
            });
        },
        handleRowChange(currentRow, oldCurrentRow) {
            this.detailobj = currentRow;
        },
        handlePageSize(value) {
            this.pagesize = value;
            this.tableData1 = this.search(1,this.pagesize);
        },
        changePage(value) {
            this.page = value;
            this.tableData1 = this.search(this.page,10);
        },
        request(url, data, type) {
            if (type == "get") {
                return this.axios
                    .request({
                        url: url,
                        params: data,
                        method: type,
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            } else {
                return this.axios
                    .request({
                        url: url,
                        data,
                        method: type,
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            }
        },
    },
};
</script>
<style lang="less" scoped>

</style>

