<template>
    <div>
        <el-card>
            <el-form :inline="true" :model="filterQuery" size="mini">
                <el-row style="width: 100%">
                    <el-form-item class="query-form-item">
                        <el-input placeholder="请输入养护记录号" v-model="filterQuery.orderId"
                                  clearable="true"></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input placeholder="请输入领用记录号" v-model="filterQuery.collOrderId"
                                  clearable="true"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button-group>
                            <el-button
                                type="primary"
                                icon="el-icon-refresh"
                                @click="onReset"
                            ></el-button>
                            <el-button type="primary" icon="search" @click="onSubmit"
                            >查询
                            </el-button
                            >
                            <el-button type="primary" icon="search" @click="addMAOrder"
                            >新增养护记录
                            </el-button
                            >
                        </el-button-group>
                    </el-form-item>
                </el-row>
            </el-form>

            <el-table v-loading="loading" :data="list" style="width: 100%">
                <el-table-column label="序号" type="index" fixed></el-table-column>
                <el-table-column label="养护记录号" prop="orderId" width="180"></el-table-column>
                <el-table-column label="领用记录号" prop="collOrderId" width="180"></el-table-column>
                <el-table-column label="养护日期" prop="createTime" width="180"></el-table-column>
                <el-table-column label="养护人" prop="createUser" width="120"></el-table-column>
                <el-table-column label="操作" width="180" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="editOrder(scope.row)"
                        >编辑
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="updateOrder(scope.row)"
                        >完成养护
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="deleteDialog(scope.row.id)"
                        >删除
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-pagination
            :page-size="filterQuery.limit"
            :current-page="filterQuery.page"
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>

        <el-dialog
            :title="formMap[formName]"
            :visible.sync="formVisible"
            width="70%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="formVisible"
        >
            <addDeviceMAOrder
                :deviceMAOrder="deviceMAOrder"
                :closeDialog="closeDialog"
            ></addDeviceMAOrder>
        </el-dialog>

    </div>
</template>

<script>
import {getDeviceMAOrderList, updateDeviceMAOrder, deleteDeviceMAOrder} from "@/api/inventory/deviceMAOrder";
import addDeviceMAOrder from "@/views/inventory/addDeviceMAOrder.vue";

export default {
    data() {
        return {
            filterQuery: {
                status: 0,
                orderId: null,
                collOrderId: null,
                page: 1,
                limit: 20
            },
            list: [],
            total: 0,
            loading: false,
            idQuery: null,
            formVisible: false,
            formName: null,
            formMap: {
                add: "新增设备养护记录",
                edit: "编辑设备养护记录"
            },
            deviceMAOrder: null
        };
    },
    components: {
        addDeviceMAOrder
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                status: 0,
                orderId: null,
                page: 1,
                limit: 20,
            };
            this.getList();
        },
        onSubmit() {
            this.getList();
        },
        handleSizeChange(val) {
            this.filterQuery.limit = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        getList() {
            this.loading = true
            getDeviceMAOrderList(this.filterQuery).then((res) => {
                this.loading = false;
                if (res.code === 20000) {
                    this.list = res.data.list || [];
                    this.total = res.data.total || 0;
                } else {
                    this.list = [];
                    this.total = 0;
                }
            }).catch((error) => {
                this.loading = false;
                this.list = [];
                this.total = 0;
            });
        },
        closeDialog() {
            this.formVisible = false;
            this.getList();
        },
        addMAOrder() {
            this.deviceMAOrder = null;
            this.formName = 'add';
            this.formVisible = true;
        },
        editOrder(row) {
            this.deviceMAOrder = row;
            this.formName = 'edit';
            this.formVisible = true;
        },
        deleteDialog(id) {
            this.$confirm('此操作将永久删除该养护记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {id: id};
                deleteDeviceMAOrder(params).then((res) => {
                    if (res.code === 20000) {
                        this.$message.success("删除成功！");
                        this.getList();
                    } else {
                        this.$message.error(res.message);
                    }
                })
            }).catch((error) => {
                this.$message.error(error.message);
            });
        },
        updateOrder(row) {
            // 拷贝参数
            let data = JSON.parse(JSON.stringify(row))
            data.status = 1;
            updateDeviceMAOrder(data).then((res) => {
                if (res.code === 20000) {
                    this.getList();
                } else {
                    this.$message.error(res.message);
                }
            }).catch((error) => {
                this.$message.error(error.message);
            })
        }
    },
    created() {
        this.getList();
    },
};
</script>

<style type="text/scss" lang="scss">
</style>
