<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="timechange"
                    v-model="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()"
                >{{$t('1001')}}</Button>

                <Button
                    type="primary"
                    class="fourWord"
                    style="margin-left:50px"
                    icon="ios-checkmark-circle-outline"
                    @click="checkPost(2)"
                >审核通过</Button>
                <Button
                    type="error"
                    class="fourWord"
                    icon="ios-close-circle-outline"
                    @click="checkPost(3)"
                >审核驳回</Button>
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                @on-current-change="handleRowChange"
                @on-selection-change="getSelection"
                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>
    </div>
</template>

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

export default {
    data() {
        return {
            datalevel: [],
            leveldata: [],
            leveldata1: [],
            machineId: "",
            machineList: "",
            datetime: [],
            status: "",
            total: 0,
            page: 1,
            pagesize: 10,
            detailobj: {},
            tableData1: [],
            tableColumns1: [
                {
                    type: "selection",
                    align: "center",
                },
                {
                    type: "index2",
                    title: this.$t("1008"), // 序号
                    key: "xuhao",
                    render: (h, params) => {
                        return h(
                            "span",
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    },
                    width:70
                },
                {
                    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,
            ids:''
        };
    },

    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.search();
        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;
                    }
                });
        },
        handlelevelchange1(val) {
            this.leveldata1 = val;
            this.getMachine();
        },
        getMachine() {
            var params = {
                section: this.leveldata1[this.leveldata1.length - 1],
            };
            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));
                    }
                });
        },
        timechange(val) {
            this.repairDate = 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 mid = '';
            
            let data = {
                page: page,
                pageSize: pagesize,
                section: section,
                machine: this.machineId,
                state: this.status,
                repairstart: "",
                repairend: "",
                editstart: start,
                editend: end,
                toExcel: "0",
                projectId: 0,
                mid:this.$store.state.user.userId
            };
            // return
            this.request(
                "/tpm/OutsourcedMaintenance/GetOutsourcedMaintenanceForSubmit",
                data,
                "post"
            ).then((res) => {
                if (res.data.ret == 1) {
                    // this.total = res.data.total;
                    this.tableData1 = res.data.data;
                }
            });
        },
        getSelection(val){
            console.log(val);
            let arr = [];
            if(val){
                for (let i = 0; i < val.length; i++) {
                    arr.push(val[i].id)
                }
                this.ids = arr.join(',')
            }
        },
        checkPost(state) {
            //审核未通过的修改完成后才能提交审核
            if (!this.ids) {
                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: state==2?"是否确认通过":'是否确认驳回',
                content: "",
                width: "290px",
                closable: true,
                okText: this.$t("1011"),
                onOk: () => {
                    let data = {
                        ids: this.ids,
                        state: state,
                    };
                    this.request(
                        "/tpm/OutsourcedMaintenance/SubmitOutsourcedMaintenance",
                        data,
                        "post"
                    ).then((res) => {
                        if (res.data.ret == 1) {
                            this.$Message.success(this.$t(res.data.msg));
                            this.search();
                        } 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();
        },
        changePage(value) {
            this.page = value;
            this.tableData1 = this.search();
        },
        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>
