<template>
    <div class="app-container">
        <div class="title-container">
            <breadcrumb id="breadcrumb-container" class="breadcrumb-container" />
        </div>
        <y-page-list-layout :page-list="pageData" :page-para="listQuery" :get-page-list="selectMyOrderPage">
            <template slot="left">
                <PermissionButton menu-code="_views_assets_add_order" class-name="filter-item" type="danger"
                    icon="el-icon-circle-plus-outline" :page-jump="true" round style="float: left"
                    :page-query="{ 'couldEdit': true, 'saveOrderBtn': '保存订单', 'showBtn': true, 'couldBack': true }" />
                <el-input style="margin-left: 20px;width: 300px;float: left;" class="filter-item"
                    v-model="listQuery.keyWord" placeholder="项目编号/报告号/项目名称/业务来源" clearable>
                </el-input>
                <el-select v-model="listQuery.principalId" filterable placeholder="项目负责人（可搜索）"
                    style=" width: 200px;margin-left: 10px;float: left;">
                    <el-option v-for="(u, id) in allUsers" :label="u.name" :value="u.id"></el-option>
                </el-select>
                <el-select v-model="listQuery.delivery" filterable placeholder="送达状态"
                    style=" width: 200px;margin-left: 10px;float: left;">
                    <el-option v-for="(s, value) in deliverys" :label="s.label" :value="s.value"></el-option>
                </el-select>
                <el-button class="filter-item" style="margin-left: 10px;float: left;" type="primary" @click="searchList"
                    round>搜索
                </el-button>
                <el-button class="filter-item" style="float: left;" round type="success" @click="resetParams()">重置
                </el-button>
            </template>
            <parentTable ref="table" v-loading="listLoading" :data="pageData.records" slot="table" style="width: 100%;">
                <el-table-column label="项目编号" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.orderId }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="分单类型" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.allotType }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="意见书号" align="center" width="250">
                    <template slot-scope="{row}">
                        <span>{{ row.statementNo }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="报告号" align="center" width="250">
                    <template slot-scope="{row}">
                        <span>{{ row.reportNo }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="项目名称" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="业务类型" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.assetsBusinessGener }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="客户名称" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.customerName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="业务来源" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.customerSubName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="委托人" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.bailor }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="委托人联系电话" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.bailorContactTel }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="当前节点" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.currentNodeName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="当前节点负责人" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.handlerName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="项目负责人" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.principalName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="客户经理" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.clientManagerName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="评估结论对应的评估值(万)" align="center" width="200">
                    <template slot-scope="{row}">
                        <span>{{ row.estimatedValue }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="订单应收款(元)" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.shouldAmount }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="订单实收款(元)" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.realAmount }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="产品应收款(元)" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.productionShouldAmount }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="标准收费(元)" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.standardAmount }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="折扣比例" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.discount }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="实际开票金额" align="center" width="150">
                    <template slot-scope="{row}">
                        <span>{{ row.invoiceRealAmount }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="送达状态" align="center" width="150">
                    <template slot-scope="{row}">
                        <el-tag v-if="row.reportDelivery" type='success'>已送达</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" :min-width="dynamicWidth" fixed="right">
                    <template slot-scope="{row}">
                        <div class="optionDiv" style="white-space: nowrap; display: inline-block">
                            <el-button type="danger" size="mini" v-if="(!row.reportDelivery || row.reportDelivery == null) && row.reportRepertoryState">
                                标记送达
                            </el-button>
                            <el-button type="success" size="mini" @click="orderDetail(row)">
                                查看
                            </el-button>
                            <el-button type="primary" size="mini" @click="openShouldAmountDialog(row)">
                                订单应收款
                            </el-button>
                            <el-button v-if="row.reportNo !== null" type="success" size="mini" @click="openProShouldAmountDialog(row)">
                                产品应收款
                            </el-button>
                            <el-button v-if="row.currentNodeCode === 'REPORT_OUT_APPLY'" type="danger" size="mini" @click="openRepertoryOutWarehouse(row)">
                                申请提前出库
                            </el-button>
                            <!-- <el-button v-if="row.currentNodeCode === 'REPORT_OUT_APPLY'" type="text"
                            @click="canceladdRepertoryOutWarehouse(row)">
                            撤销出库申请 
                            </el-button> -->
                        </div>
                    </template>
                </el-table-column>
            </parentTable>
        </y-page-list-layout>
        <el-dialog :visible.sync="shouldAmountDialog" width="35%" center top="35vh" custom-class="doWarehouseClass"
            @closed="cleanData">
            <el-form ref="fund" :model="fund" style="margin-left:40px">
                <el-row>
                    <el-col :xs="24" :sm="12" :lg="24" :span="12">
                        <el-form-item label="应收款金额(元)：" prop="shouldAmount" label-width="160px"
                            class="postInfo-container-item"
                            :rules="{ required: true, message: '应收款金额不能为空', trigger: 'blur' }">
                            <el-input-number :precision="2" :min="null" :value-on-clear="0"
                                v-model.number="fund.shouldAmount" type="number" style="width:300px" class="filter-item" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :lg="24" :span="12">
                        <el-form-item label="备注：" prop="remark" label-width="160px" class="postInfo-container-item">
                            <el-input style="width:300px" type="textarea" v-model="fund.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="shouldAmountDialog = false">取 消</el-button>
                <el-button type="primary" @click="saveShouldAmount()">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog :visible.sync="proShouldAmountDialog" width="35%" center top="35vh" custom-class="doWarehouseClass"
            @closed="cleanData">
            <el-form ref="productionFundFrom" :model="productionFundFrom" style="margin-left:40px">
                <el-row>
                    <el-col :xs="24" :sm="12" :lg="24" :span="12">
                        <el-form-item label="产品应收款金额(元)：" prop="productionShouldAmount" label-width="160px"
                            class="postInfo-container-item"
                            :rules="{ required: true, message: '应收款金额不能为空', trigger: 'blur' }">
                            <el-input-number :precision="2" :min="null" :value-on-clear="0"
                                v-model.number="productionFundFrom.productionShouldAmount" type="number" style="width:300px"
                                class="filter-item" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :lg="24" :span="12">
                        <el-form-item label="备注：" prop="remark" label-width="160px" class="postInfo-container-item">
                            <el-input style="width:300px" type="textarea" v-model="productionFundFrom.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="proShouldAmountDialog = false">取 消</el-button>
                <el-button type="primary" @click="saveProShouldAmount()">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog :visible.sync="repertoryOutWarehouseVisible">
            <el-form ref="outWarehouse" :model="outWarehouse" style="margin-left:40px; margin-right: 200px;">
                <el-row>
                    <el-col>
                        <el-form-item label="出库原因：" prop="remark" label-width="160px" class="postInfo-container-item">
                            <el-input type="textarea" placeholder="请输入申请提前出库原因" v-model="outWarehouse.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="repertoryOutWarehouseVisible = false">取 消</el-button>
                <el-button type="primary" @click="addRepertoryOutWarehouse()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import YPageListLayout from '@/components/YPageListLayout'
import Breadcrumb from '@/components/Breadcrumb'
import PermissionButton from '@/components/PermissionButton/PermissionButton'
import { getOperatorWidth } from '@/utils/tableUtil.js'

export default {
    name: 'AssetsAllList',
    components: {
        Breadcrumb,
        YPageListLayout,
        PermissionButton
    },
    data() {
        return {
            dynamicWidth: 0,
            pageData: { records: [] },
            listLoading: false,
            listQuery: {
                page: 1,
                size: 10,
                current: 1,
                // 节点code
                nodeCode: null,
                // 客户经理id
                clientManagerId: this.$store.getters.userInfo.id,
                // 项目负责人id
                principalId: null,
                // 业务类型
                assetsBusinessGener: null,
                // 关键字
                keyWord: null,
                // 送达状态
                delivery: null
            },
            // 用户下拉列表
            allUsers: [],
            // 送达状态下拉列表
            deliverys: [
                {
                    value: '1',
                    label: '已送达'
                }, {
                    value: '0',
                    label: '未送达'
                },
            ],
            shouldAmountDialog: false,
            proShouldAmountDialog: false,
            fund: {
                id: null,
                businessType: null,
                orderName: null,
                orderId: null,
                shouldAmount: null,
                remark: null,
                businessType: null,
                businessId: null,
                businessSubId: null,
                productionFundId: null,
                evaluateAmount: null
            },
            repertoryOutWarehouseVisible: false,
            outWarehouse: {
                id: null,
                productionFundId: null,
                reportNo: null,
                statementNo: null,
                remark: null,
                realAmount: null
            },
            businessId: null,
            businessSubId: null,
            businessMinId: null,
            recordId: null,
            currentNodeId: null,
            // 产品收款信息
            productionFundFrom: {
                id: null,
                businessType: null,
                businessId: null,
                productionNo: null,
                productionType: null,
                orderFundId: null,
                productionShouldAmount: null,
                remark: null
            }
        }
    },
    created() {
        // 获取用户下拉列表
        this.getAllUser();
        // 获取我的资产业务订单
        this.selectMyOrderPage();
    },
    updated() {
        this.dynamicWidth = getOperatorWidth();
    },
    methods: {
        // 无条件分页查询
        selectMyOrderPage() {
            this.$api.assets.selectMyOrderPage(this.listQuery).then(res => {
                if (res.code === 200) {
                    this.pageData = res.data;
                }
            })
        },
        // 条件查询
        searchList() {
            // 重置分页
            this.listQuery.page = 1
            this.listQuery.size = 10
            this.selectMyOrderPage()
        },
        // 重置搜索条件
        resetParams() {
            this.$router.push({ query: {} });
            this.listQuery = {
                current: 1,
                size: 10,
            }
            this.selectMyOrderPage();
        },
        // 获取所有用户下拉列表
        getAllUser() {
            this.$api.user.simpleAll().then(res => {
                if (res.code === 200) {
                    this.allUsers = res.data;
                }
            })
        },
        orderDetail(row) {
            this.$router.push({
                path: "/assets/orderDetail",
                query: {
                    row: JSON.stringify(row),
                    back: '/assets/myOrderList',
                    couldEdit: false,
                    couldBack: false,
                    disabledStatus: true
                }
            })
        },
        openShouldAmountDialog(row) {
            this.shouldAmountDialog = true;
            this.fund.id = row.orderFundId;
            this.fund.businessType = 'ASSET_BUSINESS';
            this.fund.businessId = row.assetsId;
            this.fund.orderFundId = row.orderFundId;
            this.fund.shouldAmount = row.shouldAmount;
            this.fund.remark = row.remark;
            this.fund.orderId = row.orderId;
            this.fund.orderName = row.name;
        },
        openProShouldAmountDialog(row) {
            if (row.orderFundId !== null) {
                this.proShouldAmountDialog = true;
                this.productionFundFrom.id = row.productionFundId;
                this.productionFundFrom.businessType = 'ASSET_BUSINESS';
                this.productionFundFrom.businessId = row.assetsId;
                this.productionFundFrom.orderFundId = row.orderFundId;
                this.productionFundFrom.productionNo = row.reportNo;
                this.productionFundFrom.productionShouldAmount = row.productionShouldAmount;
                if (row.reportNo.includes('川友评报字')) {
                    this.productionFundFrom.productionType = 'REPORT'
                } else if (row.reportNo.includes('川友咨报字')) {
                    this.productionFundFrom.productionType = 'CONSULT'
                } else if (row.reportNo.includes('川友字')) {
                    this.productionFundFrom.productionType = 'LETTER'
                } else {
                    this.productionFundFrom.productionType = 'STATEMENT'
                }
                this.productionFundFrom.productionShouldAmount = row.productionShouldAmount;
                this.productionFundFrom.remark = row.remark;
            } else {
                this.$notify({
                    title: '警告',
                    message: '需要先填写订单应收款',
                    type: 'error',
                    duration: 3000
                });
            }

        },
        saveShouldAmount() {
            this.$refs.fund.validate(valid => {
                if (valid) {
                    if (this.fund.orderFundId) {
                        this.$api.orderFund.edit(this.fund).then(res => {
                            if (res.code === 200 && res.data) {
                                this.$notify({
                                    title: '成功',
                                    message: '订单应收款保存成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.selectMyOrderPage();
                                this.shouldAmountDialog = false;
                            } else {
                                this.$notify({
                                    title: '失败',
                                    message: '订单应收款保存错误',
                                    type: 'error',
                                    duration: 1000
                                });
                            }
                        })
                    } else {
                        this.$api.orderFund.add(this.fund).then(res => {
                            if (res.code === 200 && res.data) {
                                this.$notify({
                                    title: '成功',
                                    message: '订单应收款保存成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.selectMyOrderPage();
                                this.shouldAmountDialog = false;
                            } else {
                                this.$notify({
                                    title: '失败',
                                    message: '订单应收款保存错误',
                                    type: 'error',
                                    duration: 1000
                                });
                            }
                        })
                    }
                }
            })
        },
        cleanData() {
            this.fund.shouldAmount = null;
            this.fund.id = null;
            this.fund.businessType = null;
            this.fund.remark = null;
            this.fund.businessType = null;
            this.fund.businessId = null;
            this.fund.businessSubId = null;
            this.fund.productionFundId = null;
            this.fund.remark = null;
            this.fund.evaluateAmount = null
        },
        openRepertoryOutWarehouse(row) {
            this.repertoryOutWarehouseVisible = true;
            this.outWarehouse.reportNo = row.reportNo;
            this.outWarehouse.businessType = 'ASSET_BUSINESS';
            this.businessId = row.assetsId;
            this.businessSubId = row.statementNo;
            this.businessMinId = row.reportNo;
            this.recordId = row.recordId;
            this.currentNodeId = row.currentNodeId;
        },
        // 新增出库申请
        addRepertoryOutWarehouse() {
            if (this.outWarehouse.remark !== null && this.outWarehouse.remark !== '') {
                let taskRecordDTO = new Object();
                taskRecordDTO.recordId = this.recordId;;
                taskRecordDTO.taskData = this.outWarehouse;
                this.$api.productionOutWarehouse.add(taskRecordDTO).then(res => {
                    if (res.code === 200 && res.data) {
                        this.$notify({
                            title: '成功',
                            message: '出库申请已提交',
                            type: 'success',
                            duration: 2000
                        });
                        this.commitNode('PASS', this.outWarehouse.remark);
                        this.repertoryOutWarehouseVisible = false;
                        this.selectMyOrderPage();
                    } else {
                        this.$notify({
                            title: '失败',
                            message: '出库申请提交失败',
                            type: 'error',
                            duration: 2000
                        });
                    }
                })
            } else {
                this.$notify({
                    title: '警告',
                    message: '出库原因是必填项',
                    type: 'error',
                    duration: 2000
                });
            }
        },
        // 撤销出库申请
        canceladdRepertoryOutWarehouse(row) {
            this.$confirm('请确认是否撤销此出库申请?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                this.$api.productionOutWarehouse.detail(row.warehouseId).then(res => {
                    if (res.departmentCheckState !== '审核中') {
                        this.$notify({
                            title: '警告',
                            message: '出库申请' + res.departmentCheckState + '，无法撤销。',
                            type: 'error',
                            duration: 2000
                        });
                        return;
                    }
                })

                if (row.id) {
                    this.$api.productionOutWarehouse.cancel(row.id).then(res => {
                        if (res.code === 200 && res.data) {
                            this.$notify({
                                title: '成功',
                                message: '出库申请已撤销',
                                type: 'success',
                                duration: 2000
                            });
                            // 撤销成功退回节点
                            this.commitNode('REVERSE');
                        }
                    })
                }
            })
        },
        // 提交节点
        commitNode(state, remark) {
            let commit = new Object;
            commit.instanceNodeId = this.currentNodeId;
            commit.state = state;
            commit.comments = remark;
            commit.businessMinId = this.businessMinId;
            commit.businessSubId = this.businessSubId;
            this.$api.workflow.commit(commit).then(res => {
                if (res.code === 200 && res.data) {
                    this.$notify({
                        title: '成功',
                        message: '工作流节点提交成功。',
                        type: 'success',
                        duration: 2000
                    });
                } else {
                    this.$notify({
                        title: '失败',
                        message: '工作流节点提交失败，请联系管理员。',
                        type: 'error',
                        duration: 2000
                    });
                }
            })
        },
        // 保存产品应收款
        saveProShouldAmount() {
            this.$refs.productionFundFrom.validate(valid => {
                if (valid) {
                    if (this.productionFundFrom.id) {
                        this.$api.productionFund.edit(this.productionFundFrom).then(res => {
                            if (res.code === 200 && res.data) {
                                this.$notify({
                                    title: '成功',
                                    message: '产品应收款保存成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.selectMyOrderPage();
                                this.proShouldAmountDialog = false;
                            } else {
                                this.$notify({
                                    title: '失败',
                                    message: '产品应收款保存错误',
                                    type: 'error',
                                    duration: 1000
                                });
                            }
                        })
                    } else {
                        this.$api.productionFund.add(this.productionFundFrom).then(res => {
                            if (res.code === 200 && res.data) {
                                this.$notify({
                                    title: '成功',
                                    message: '产品应收款保存成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.selectMyOrderPage();
                                this.proShouldAmountDialog = false;
                            } else {
                                this.$notify({
                                    title: '失败',
                                    message: '产品应收款保存错误',
                                    type: 'error',
                                    duration: 1000
                                });
                            }
                        })
                    }
                }
            })
        }
    },
    // 标记产品送达
    markDelivery(row) {
        this.$api.assets.markDelivery(row.reportNo).then(res => {
            if (res.code === 200 && res.data) {
                this.$notify({
                    title: '成功',
                    message: '标记送达成功！',
                    type: 'success',
                    duration: 1000
                });
            }
        })
    }
}
</script>

<style lang="scss" scoped></style>