<template>
    <div class="testdiv">
        <div class="divborder">
            <h2>基本信息</h2>
            <div class="newSearchDiv">
                <div class="filter">
                    <label>{{ $t("1132") }}：</label>
                    <Cascader
                        trigger="click"
                        class="searchSelect"
                        :data="datalevel"
                        :value="leveldata"
                    ></Cascader>
                </div>
                <div class="filter">
                    <label>任务类型：</label>
                    <Select
                        label-in-value
                        clearable
                        filterable
                        class="searchSelect"
                        v-model="taskType"
                    >
                        <Option :value="1">Load</Option>
                        <Option :value="2">Unload</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>产品编号：</label>
                    <Select
                        label-in-value
                        clearable
                        filterable
                        class="searchSelect"
                        v-model="productCode"
                    >
                        <Option
                            v-for="(item, index) in productList"
                            :value="item.id"
                            :key="index"
                            :label="
                                item.name +
                                '#' +
                                item.code +
                                '#' +
                                item.drawingcode
                            "
                            >{{
                                item.name +
                                "#" +
                                item.code +
                                "#" +
                                item.drawingcode
                            }}</Option
                        >
                    </Select>
                </div>
                <div class="filter">
                    <label>任务编号：</label>
                    <Input
                        type="text"
                        class="searchInput"
                        v-model="taskNumber"
                    />
                </div>
                <div class="filter">
                    <label>工件编号：</label>
                    <Input type="text" class="searchInput" v-model="sn" />
                </div>
                <div class="filter">
                    <label>状态：</label>
                    <Select
                        label-in-value
                        clearable
                        filterable
                        class="searchSelect"
                        v-model="state"
                    >
                        <Option :value="1">待执行</Option>
                        <Option :value="2">执行中</Option>
                        <Option :value="3">执行完成</Option>
                        <Option :value="4">取消</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>执行类型：</label>
                    <Select
                        label-in-value
                        clearable
                        filterable
                        class="searchSelect"
                        v-model="performType"
                    >
                        <Option :value="1">手动执行</Option>
                        <Option :value="2">自动执行</Option>
                    </Select>
                </div>
                <div class="filter">
                    <label>触发时间：</label>
                    <DatePicker
                        type="datetime"
                        placeholder="Select date"
                        style="width: 200px"
                        v-model="startTime"
                        @on-change="getTime"
                    ></DatePicker>
                </div>
                <div class="filter">
                    <label>完成时间：</label>
                    <DatePicker
                        type="datetime"
                        placeholder="Select date"
                        style="width: 200px"
                        v-model="endTime"
                        @on-change="getTime2"
                    ></DatePicker>
                </div>
                <div class="searchBtn">
                    <Button
                        type="primary"
                        class="twoWord"
                        icon="ios-search"
                        @click="search(page, pagesize)"
                        >{{ $t("1001") }}</Button
                    >
                    <!--  -->
                    <!-- <Button
                        type="warning"
                        class="twoWord"
                        icon="ios-search"
                        @click="cancel()"
                        >取消</Button
                    > -->
                    <!--只有状态是待执行的可以改成取消-->
                    <Button
                        type="primary"
                        class="twoWord"
                        icon="ios-build"
                        @click="manual()"
                        >手动</Button
                    >
                    <!--当生产线的模式是手动是才可以进行手动任务执行-->
                    <Button
                        type="primary"
                        class="twoWord"
                        icon="ios-search"
                        @click="exportExcel()"
                        >导出</Button
                    >
                </div>
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                :columns="columns1"
                :data="data1"
                @on-current-change="handleRowChange"
                highlight-row
            ></Table>
            <div class="pageDiv">
                <div class="pageDirection">
                    <Page
                        :total="total"
                        :current="page"
                        show-elevator
                        show-sizer
                        show-total
                        @on-change="changePage"
                        @on-page-size-change="handlePageSize"
                        :page-size-opts="[10, 20, 30, 40, 100]"
                    ></Page>
                </div>
            </div>
        </div>
    </div>
</template>

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

export default {
    data() {
        return {
            leveldata: [], // 产线层级插件所需数据
            datalevel: [], // 产线层级选中数据
            columns1: [
                {
                    title: this.$t("1008"),
                    key: "xuhao",
                    render: (h, params) => {
                        return h(
                            "span",
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    },
                    width: 70,
                },
                {
                    title: "任务编号",
                    key: "taskno",
                    width: 300,
                },
                {
                    title: "产线层级",
                    key: "section",
                },
                {
                    title: "触发方",
                    key: "trigger",
                },
                {
                    title: "任务类型",
                    key: "tasktype",
                },
                {
                    title: "工件编号",
                    key: "sn",
                },
                {
                    title: "工单编号",
                    key: "ordernumber",
                },
                {
                    title: "产品编号",
                    key: "productCode",
                },
                {
                    title: "工序编号",
                    key: "routeNo",
                },
                {
                    title: "工序",
                    key: "route",
                },
                {
                    title: "程序号",
                    key: "program",
                },
                {
                    title: "储位位置",
                    key: "location",
                },
                {
                    title: "工装编号",
                    key: "fixtureCode",
                },
                {
                    title: "动作",
                    key: "action",
                },
                {
                    title: "执行类型",
                    key: "actionType",
                },
                {
                    title: "状态",
                    key: "status",
                    render: (h, params) => {
                        let str = "";
                        switch (params.row.status) {
                            case 1:
                                str = "待执行";
                                break;
                            case 2:
                                str = "执行中";
                                break;
                            case 3:
                                str = "执行完成";
                                break;
                            case 4:
                                str = "取消";
                                break;
                        }
                        return h("div", str);
                    },
                },
                {
                    title: "触发时间",
                    key: "triggerTime",
                },
                {
                    title: "完成时间",
                    key: "complatetime",
                },
            ],
            data1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            taskType: 0,
            from: 0,
            productCode: "",
            productList: [],
            taskNumber: "",
            sn: "",
            state: 0,
            performType: 0,
            startTime: "",
            endTime: "",
            detailobj: {},
        };
    },
    created() {
        this.columns1 = this.$time.initTableTitle(this.columns1);
        this.initlevel();
        this.iniproducts();
        this.search(1, 10);
    },
    methods: {
        search(page, pagesize) {
            let params = {
                section: this.sectionId,
                tasktype: this.taskType,
                productCode: this.productCode,
                sn: this.sn,
                status: this.status,
                actionType: this.performType,
                triggertime: this.startTime,
                complatetime: this.endTime,
                page: page,
                pageSize: pagesize,
            };
            this.axios
                .request({
                    url: "/acc/AutomationState/GetTasklist",
                    params,
                    method: "get",
                })
                .then((res) => {
                    const arr = [];
                    if (res.data.ret == 1) {
                        console.log(res.data.data);
                        this.data1 = res.data.data.data;
                    }
                    this.total = res.data.data.total;
                });
        },
        cancel() {
            if (this.detailobj.status != 1) {
                this.$Message.error("该任务清单状态不能取消");
                return false;
            }
            return;
            let data = {
                section: this.sectionid,
            };
            this.request(
                // "/acc/AutomationOperate/CompalateAssemble",
                data,
                "post"
            ).then((res) => {
                if (res.data.ret == 1) {
                    this.$Message.success(this.$t(res.data.msg));
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        manual() {
            // if (this.detailobj.actiontype != 1) {
            //     this.$Message.error("该任务清单不能手动执行");
            //     return false;
            // }
            if (!this.detailobj.id) {
                this.$Message.error(this.$t(1043));
                return false;
            }
            let data = {
                id: this.detailobj.id,
            };
            this.request("/acc/AutomationOperate/Send", data, "get").then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.$Message.success(this.$t(res.data.msg));
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                }
            );
        },
        exportExcel() {
            // this.axios.request({
            //     url: "/config/Download/GetTemplate?name=StorageLocation",
            //     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));
            //     }
            // });
        },
        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;
                        arr.push(res.data.data[0].value);
                        this.leveldata = arr;
                    }
                });
        },
        iniproducts() {
            var params = {
                name: "",
                count: 1000000,
            };
            this.axios
                .request({
                    url: "/config/ProjectProduct/GetProductsByCode",
                    params,
                    method: "get",
                })
                .then((res) => {
                    let data = res.data.data;
                    if (res.data.ret === 1) {
                        this.productList = res.data.data;
                    }
                });
        },
        getTime(val) {
            this.startTime = val;
        },
        getTime2(val) {
            this.endTime = val;
        },
        exportExcel() {
            let data = {};
            this.axios
                .request({
                    url: "",
                    data,
                    method: "",
                })
                .then((res) => {
                    if (res.data.ret === 1) {
                        window.location.href =
                            axios.publicPath + "" + res.data.data;
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                });
        },
        handleRowChange(currentRow, oldCurrentRow) {
            this.detailobj = currentRow;
        },
        handlePageSize(value) {
            this.pagesize = value;
            this.data1 = this.search(this.page, this.pagesize);
        },
        changePage(value) {
            this.page = value;
            this.data1 = this.search(this.page, this.pagesize);
        },
        request(url, data, type) {
            if (type == "get") {
                return this.axios
                    .request({
                        url: url,
                        params: data,
                        method: type,
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            } else {
                return this.axios
                    .request({
                        url: url,
                        data,
                        method: type,
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            }
        },
    },
};
</script>

<style>
</style>