<template>
    <div>
        <navbar title="经营情况"></navbar>
        <div class="wrapper">
            <van-form ref="form" :model="situationForm">
                <van-field v-model="situationForm.salesDesc" label="销售额描述" placeholder="请输入" type="textarea"
                    :row="2"></van-field>
                <van-field v-model="situationForm.retainedProfitsDesc" label="净利润描述" placeholder="请输入" type="textarea"
                    :row="2"></van-field>
                <van-field v-model="situationForm.employeeCount" label="员工人数(人)" placeholder="请输入" type="number"></van-field>
                <van-field v-model="situationForm.yearlySalary" label="年工资" placeholder="请输入" type="number"></van-field>
                <van-field v-model="situationForm.scope" label="公司经营范围" placeholder="请输入" type="textarea"
                    :row="2"></van-field>
                <van-field v-model="situationForm.address" label="公司经营地址" placeholder="请输入" type="textarea"
                    :row="2"></van-field>
                <div class="jine">
                    <van-field v-model="situationForm.siteArea" label="经营场地面积" placeholder="请输入" type="number"></van-field>
                    <span class="danwei" v-if="situationForm.siteArea">平</span>
                </div>

                <van-field v-model="situationForm.siteNature" label="经营场地性质" placeholder="请输入"></van-field>
            </van-form>
            <div class="title">固定资产投入情况</div>
            <van-form ref="form" :model="assetsForm">
                <van-field v-model="assetsForm.landPlant" label="土地及厂房" placeholder="请输入" type="textarea"
                    :row="2"></van-field>
                <van-field v-model="assetsForm.mechEquip" label="机械设备" placeholder="请输入" type="textarea"
                    :row="2"></van-field>
                <van-field v-model="assetsForm.otherFixedAssets" label="其他固定资产" placeholder="请输入" type="textarea"
                    :row="2"></van-field>

            </van-form>
            <div class="header">
                <div class="title">上游情况分析</div>
                <div class="header_btn">
                    <van-button plain color="#2372eb" size="mini" @click="handleUp">添加</van-button>
                </div>
            </div>
            <div class="main_item" v-for="(item, index) in upStreamList" :key="item.key" @click="goUpDetail(item)">
                <div class="main_item_left">{{ item.supplierName }}({{ item.settleAmt }})</div>
                <div class="main_item_right"><van-icon name="arrow" class="header_icon" /></div>
            </div>

            <div class="header">
                <div class="title">下游情况分析</div>
                <div class="header_btn">
                    <van-button plain color="#2372eb" size="mini" @click="handleDown">添加</van-button>
                </div>
            </div>
            <div class="main_item" v-for="(item, index) in downStreamList" :key="item.key" @click="goDownDetail(item)">
                <div class="main_item_left">{{ item.supplierName }}({{ item.settleAmt }})</div>
                <div class="main_item_right"><van-icon name="arrow" class="header_icon" /></div>
            </div>
            <van-form ref="form" :model="industryForm" class="industryForm">
                <van-field v-model="industryForm.industryPosAnaly" label="行业地位分析" placeholder="请输入" type="textarea"
                    :row="2"></van-field>
            </van-form>
        </div>
        <div class="footer">
            <van-button type="primary" class="btn" @click="handleCommit">保存</van-button>
        </div>
    </div>
</template>
<script>
import navbar from '../../../components/navbar/navbar.vue';
import { queryOperateCase, addOperateCase, updateOperateCase, querySupplierInfos } from "@/service/api/report";
import {
    Icon,
    Field,
    Form,
    Button,
    Toast
} from 'vant';
export default {
    components: {
        navbar: navbar,
        VanIcon: Icon,
        VanForm: Form,
        VanField: Field,
        VanButton: Button
    },
    data() {
        return {
            situationForm: {
                salesDesc: '',
                retainedProfitsDesc: '',
                employeeCount: '',
                yearlySalary: '',
                scope: '',
                address: '',
                siteArea: '',
                siteNature: ""
            },
            assetsForm: {
                landPlant: '',
                mechEquip: '',
                otherFixedAssets: ''
            },
            upStreamList: [

            ],
            downStreamList: [

            ],
            industryForm: {
                industryPosAnaly: ''
            },
            status: ''

        }
    },
    created() {
        this.initQueryOperateCase()
        this.initQuerySupplierInfos()
    },
    methods: {
        async initQueryOperateCase() {
            const params = {
                companyId: sessionStorage.getItem('companyId'),
            }
            let result = await queryOperateCase(params);
            if (result) {
                this.status = 2
                this.situationForm = result
                this.assetsForm = result
                this.industryForm = result
            } else {
                this.status = 1
            }

        },
        async initQuerySupplierInfos() {
            const params = {
                companyId: sessionStorage.getItem('companyId'),
                pageNum: 1,
                pageSize: 10000
            }
            let result = await querySupplierInfos(params);
            console.log(result, 'bbbbbb')
            if (result) {
                this.upStreamList = result.records.filter(item => item.supplierType == '上游')
                this.downStreamList = result.records.filter(item => item.supplierType == '下游')
                console.log(this.downStreamList, 'this.downStreamListthis.downStreamList')
            }
        },
        async handleCommit() {
            if (this.status == 1) {
                const params = {
                    companyId: sessionStorage.getItem('companyId'),
                    salesDesc: this.situationForm.salesDesc,
                    retainedProfitsDesc: this.situationForm.retainedProfitsDesc,
                    employeeCount: this.situationForm.employeeCount,
                    yearlySalary: this.situationForm.yearlySalary,
                    scope: this.situationForm.scope,
                    address: this.situationForm.address,
                    siteArea: this.situationForm.siteArea,
                    siteNature: this.situationForm.siteNature,
                    landPlant: this.assetsForm.landPlant,
                    mechEquip: this.assetsForm.mechEquip,
                    otherFixedAssets: this.assetsForm.otherFixedAssets,
                    industryPosAnaly: this.industryForm.industryPosAnaly
                }
                let result = await addOperateCase(params);
                if (result) {
                    Toast.success('新增成功');
                    this.$router.go(-1)
                }
            } else if (this.status == 2) {
                let result = await updateOperateCase(this.industryForm);
                if (result) {
                    Toast.success('修改成功');
                    this.$router.go(-1)
                }
            }
            console.log(this.baseform, 'baseformbaseform')
            console.log(this.assetsForm, 'conditionformconditionform')
        },
        handleUp() {
            sessionStorage.setItem('title', '上游情况分析');
            sessionStorage.setItem('upAddandEdit', 'add');
            this.$router.push({ path: '/UpAndDown' })
        },
        goUpDetail(item) {
            let str = JSON.stringify(item)
            sessionStorage.setItem('title', '上游情况分析');
            sessionStorage.setItem('upAddandEdit', 'edit');
            sessionStorage.setItem('upObj', str);
            this.$router.push({ path: '/UpAndDown' })
        },
        handleDown() {
            sessionStorage.setItem('title', '下游情况分析');
            sessionStorage.setItem('upAddandEdit', 'add');
            this.$router.push({ path: '/UpAndDown' })
        },
        goDownDetail(item) {
            let str = JSON.stringify(item)
            sessionStorage.setItem('title', '下游情况分析');
            sessionStorage.setItem('upAddandEdit', 'edit');
            sessionStorage.setItem('upObj', str);
            this.$router.push({ path: '/UpAndDown' })
        }
    }
}
</script>
<style lang="less" scoped>
.wrapper {
    padding-top: 46px;
    min-height: 100vh;
    font-size: 16px;
    padding-bottom: 60px;
}

.title {
    line-height: 45px;
    padding: 5px 15px;
    color: rgb(89, 89, 89);
}

.header {
    display: flex;
    justify-content: space-between;
    padding-right: 15px;
    align-items: center;
}

.header_btn {
    text-align: center;
}

.main_item {
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 15px;
    color: rgb(89, 89, 89);
    line-height: 45px;
    margin-top: 1px;

}

.main_item_left {
    font-size: 14px;
}

.footer {
    text-align: center;
    position: fixed;
    bottom: 0;
    width: 100%;

}



.btn {
    width: 100%;
}

.industryForm {
    margin-top: 10px;
}

.jine {
    display: flex;
    align-items: center;
    background-color: #fff;

    /deep/ .van-cell {
        width: 60%;
    }
}

.danwei {
    font-size: 14px;
}</style>