<template>
    <fullscreen :fullscreen.sync="fullscreen" class="container">
        <div class="container-box">
            <toolList @iconCli='iconCli' :tooltit='tooltit'></toolList>
            <div class="search">
                <!-- <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch> -->
                <el-button class="btn-check" size="medium" type="primary" @click="dialogCheck(3)" v-if="userInfo.loginNo=='jiangshuai'">添加
                </el-button>
            </div>
            <div class="tabbox">
                <el-table height="calc(100% - 40px)" class="com-table" ref="multipleTable" :data="tableData"
                    tooltip-effect="dark" size="small" border style="width: 100%" v-loading="loading">
                    <el-table-column prop="name" label="供应商">
                    </el-table-column>
                    <el-table-column prop="code" label="供应商编码">
                    </el-table-column>

					<el-table-column prop="frameContractName" label="合同名称">
                    </el-table-column>
                    <el-table-column prop="frameContractNo" label="合同编号">
                    </el-table-column>
					<el-table-column prop="cost" label="框架总额">
                    </el-table-column>
                    <el-table-column prop="lastCost" label="框架剩余总额">
                    </el-table-column>
                    <el-table-column prop="schedule" label="框架总额进度">
					</el-table-column>
                    <el-table-column prop="preCost" label="预占总额">
					</el-table-column>
                    <el-table-column prop="billCost" label="下单总额">
					</el-table-column>
					<!-- <el-table-column prop="buyTypeName" label="采购类型">
					</el-table-column> -->
					<!-- <el-table-column prop="contractEndTime" label="截止时间">
					</el-table-column> -->
                    <el-table-column label="操作" width="160px" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(1,scope.row)">查看</el-button>
                            <el-button size="mini" type="primary" @click="dialogCheck(2,scope.row)" v-if="userInfo.loginNo=='jiangshuai'">修改</el-button>
                            <el-button size="mini" type="danger" @click="delLine(scope.row)" v-if="userInfo.loginNo=='jiangshuai'">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination class="pageBox" @current-change="currchange" layout="prev, pager, next" background
                    :total="total">
                </el-pagination>
            </div>
        </div>

        <el-dialog :title="titname + '供应商'" :visible.sync="dialogStatus" width="50%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-click-modal="false" :before-close="closedia" :append-to-body="true" >
            <div v-loading="loadinged">
                <el-form :model="infolist" ref="infolist" :rules="rules">

                    <div class="info-line">
                        <el-form-item prop="name">
                            <span>供应商</span>
                            <el-input v-model="infolist.name" placeholder="供应商名称" :disabled="disableStatus"></el-input>
                        </el-form-item>
						<el-form-item>
                            <span>供应商编码</span>
                            <el-input v-model="infolist.code" placeholder="供应商编码" :disabled="disableStatus"></el-input>
                        </el-form-item>
						<el-form-item prop="cost">
						    <span>框架总额</span>
						    <el-input v-model="infolist.cost" placeholder="框架总额" :disabled="disableStatus"></el-input>
						</el-form-item>
                        <el-form-item prop="lastCost">
						    <span>剩余总额</span>
						    <el-input v-model="infolist.lastCost" placeholder="剩余总额" :disabled="disablesStatus"></el-input>
						</el-form-item>
                        <el-form-item prop="schedule">
						    <span>总额进度</span>
						    <el-input v-model="infolist.schedule" placeholder="总额进度" disabled></el-input>
						</el-form-item>
						<el-form-item prop="frameContractName">
						    <span>合同名称</span>
						    <el-input v-model="infolist.frameContractName" placeholder="合同名称" :disabled="disableStatus"></el-input>
						</el-form-item>
						<el-form-item prop="frameContractNo">
						    <span>合同编号</span>
						    <el-input v-model="infolist.frameContractNo" placeholder="合同编号" :disabled="disableStatus"></el-input>
						</el-form-item>
						<!-- <el-form-item prop="contractSignTime">
						    <span>签订时间</span>
						    <el-date-picker v-model="infolist.contractSignTime" type="datetime" placeholder="签订时间" :disabled="disableStatus">
						    </el-date-picker>
						</el-form-item>
						<el-form-item prop="contractEndTime">
						    <span>截止时间</span>
						    <el-date-picker v-model="infolist.contractEndTime" type="datetime" placeholder="截止时间" :disabled="disableStatus">
						    </el-date-picker>
						</el-form-item> -->
                        <el-form-item >
						    <span>预占总额</span>
						    <el-input v-model="infolist.preCost"  placeholder="预占总额" :disabled="disablesStatus">
						    </el-input>
						</el-form-item>
                        <el-form-item >
						    <span>下单总额</span>
						    <el-input v-model="infolist.billCost"  placeholder="下单总额" :disabled="disablesStatus">
						    </el-input>
						</el-form-item>
               
						<!-- <el-form-item prop="type">
						    <span>采购类型</span>
						    <el-select :disabled="disableStatus" :popper-append-to-body="false"
						    	v-model="infolist.buyTypeName" placeholder="采购类型">
						    	<el-option v-for="item in meterOpt" :key="item.id" :label="item.name"
						    		:value="item.name">
						    	</el-option>
						    </el-select>
						</el-form-item> -->
                    </div>
                </el-form>

                <div slot="footer" class="dialog-footer myfooter">
                    <el-button type="primary" @click="dialogCli(2)" v-if="titname !== '查看'">确 定</el-button>
                    <el-button @click="dialogCli(1)" v-if="titname === '查看'">确 定</el-button>
                    <el-button @click="dialogCli(1)">取 消</el-button>
                </div>
            </div>
        </el-dialog>

        <myMessage :messTit='messTit' @closeMessage="closeMessage" :centerDialogVisible="centerDialogVisible" v-if="centerDialogVisible"></myMessage>
    </fullscreen>

</template>
<script>
    //import mySearch from "../../../components/search.vue";
    import myMessage from "../../../components/myMessage.vue"
    import toolList from '../../../components/toolList'
    import { MessageBox } from "element-ui";

    export default {
        components: {
            //mySearch,
            myMessage,
            toolList,
            MessageBox
        },
        data() {
            const name = (rule, value, callback) => {
                if (!this.infolist.name) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            // const buyTypeName = (rule, value, callback) => {
            //     if (!this.infolist.buyTypeName) {
            //         callback(new Error('不能为空'))
            //     } else {
            //         callback()
            //     }
            // }
			const cost = (rule, value, callback) => {
                if (!this.infolist.cost) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const lastCost = (rule, value, callback) => {
                if (!this.infolist.lastCost) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            // const schedule = (rule, value, callback) => {
            //     if (!this.infolist.schedule) {
            //         callback(new Error('不能为空'))
            //     } else {
            //         callback()
            //     }
            // }
            const frameContractName = (rule, value, callback) => {
                if (!this.infolist.frameContractName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
			const frameContractNo = (rule, value, callback) => {
                if (!this.infolist.frameContractNo) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
			// const contractSignTime = (rule, value, callback) => {
            //     if (!this.infolist.contractSignTime) {
            //         callback(new Error('不能为空'))
            //     } else {
            //         callback()
            //     }
            // }
            // const contractEndTime = (rule, value, callback) => {
            //     if (!this.infolist.contractEndTime) {
            //         callback(new Error('不能为空'))
            //     } else {
            //         callback()
            //     }
            // }
            return {
                disablesStatus:false,
                rules: {
                    name: [{
                        required: true,
                        trigger: 'blur',
                        validator: name
                    }],
                    cost: [{
                        required: true,
                        trigger: 'blur',
                        validator: cost
                    }],
                    lastCost: [{
                        required: true,
                        trigger: 'blur',
                        validator: lastCost
                    }],
                    // schedule: [{
                    //     required: true,
                    //     trigger: 'blur',
                    //     validator: schedule
                    // }],
					frameContractName: [{
                        required: true,
                        trigger: 'blur',
                        validator: frameContractName
                    }],
                    frameContractNo: [{
                        required: true,
                        trigger: 'blur',
                        validator: frameContractNo
                    }],
					contractSignTime: [{
                        required: true,
                        trigger: 'change',
                        // validator: contractSignTime
                        message: '不能为空',
                    }],
					contractEndTime: [{
                        required: true,
                        trigger: 'change',
                        message: '不能为空',
                        // validator: contractEndTime
                    }],
                    // buyTypeName: [{
                    //     required: true,
                    //     trigger: 'change',
                    //     validator: buyTypeName
                    // }],
                },
                // searchList: [
                //     {
                //         type: 'input',
                //         tit: '供应商名称',
                //         value: '',
                //         width: '48%',
                //     },{
                //         type: 'sel',
                //         tit: '采购类型',
                //         value: '',
                //         width: '48%',
				// 		options:[
				// 			{
				// 				dataCode:'1',
				// 				dataName:'一采'
				// 			},
				// 			{
				// 				dataCode:'2',
				// 				dataName:'二采'
				// 			}
				// 		]
                //     }
                // ],
                tooltit:'供应商管理',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [],
                dialogStatus: false,
                disableStatus: false,
                titname: '',
                infolist: {},
                userInfo: {},
                params: {},
                centerDialogVisible: false,
                messTit: '',
                delid: '',
                loading:false,
                loadinged:false,
				proviceopt:[
					{
						id:'0',
						name:'省公司'
					},
					{
						id:'1',
						name:'地市公司'
					},
				],
				meterOpt:[
					{
						id:'1',
						name:'一采'
					},
					{
						id:'2',
						name:'二采'
					}
				],
                supplierNameOrigin: ""
            }
        },
        methods: {
            closedia() {
                this.infolist = {};
                this.dialogStatus = false;
            },
            // //搜索数据
            // searchInfo(v) {
            //     this.params = {};
            //     v[0] ? this.params.name = v[0] : '';
            //     v[1] ? this.params.buyTypeCode = v[1] : '';
            //     this.getList(this.params, this.pageSize);
            // },

            //获取列表
            getList(v, n) {
                this.pageSize = n;
                let _this = this;
                this.loading = true;
                this.tableData = [];
                this.$http({
                    url: "/market/cadvSupplier/queryPage",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                    },
                    data: v,
                }).then((res) => {
                    this.tableData = res.data.data;
                    this.total = res.data.totalRecord;
                    this.loading = false;
                });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //申请
            dialogCheck(v, n) {
                this.dialogStatus = true;
                this.infolist = Object.assign({}, n);
                if (v === 1) {
                    this.titname = '查看';
                    this.disableStatus = true;
                    this.disablesStatus = true;
                    return
                } else if (v === 2) {
                    this.titname = '修改';
                    this.disableStatus = false;
                    this.disablesStatus = true;
                    this.supplierNameOrigin = this.infolist.name

                } else if (v === 3) {
                    this.titname = '添加';
                    this.disableStatus = false;
                    this.disablesStatus = false;
                }
                this.infolist.opNo = this.userInfo.loginNo;
                this.infolist.opName = this.userInfo.loginName;
            },
            //添加
            dialogCli(v) {
                if (v === 1) {
                    this.infolist = {};
                    this.dialogStatus = false;
                    return
                } else {
					// this.infolist.buyTypeCode = this.infolist.buyTypeName == '一采'?'1':'2';
                    this.infolist.opTime = this.$formatDate(new Date(), "YYYY-MM-DD");
                    this.infolist.contractSignTime = this.infolist.contractSignTime?this.$formatDate(this.infolist.contractSignTime, "YYYY-MM-DD"):'';
                    this.infolist.contractEndTime = this.infolist.contractEndTime?this.$formatDate(this.infolist.contractEndTime, "YYYY-MM-DD"):'';
                    if (this.titname === '添加') {

                            let supplierNameArr = this.tableData.filter(item => {
                                    return item.name == this.infolist.name
                                });
                            if(parseFloat(this.infolist.cost) < parseFloat(this.infolist.lastCost)){
                                MessageBox.alert("框架总额应>=框架剩余总额", "温馨提示", {
                                    confirmButtonText: "确定"
                                })
                            }else if(supplierNameArr.length != 0){
                                MessageBox.alert("该供应商已存在", "温馨提示", {
                                    confirmButtonText: "确定"
                                });
                            }else{
                                this.submitInfo("/market/cadvSupplier/add");
                            }



                    } else if (this.titname === '修改') {
                            let supplierNameArr = this.tableData.filter(item => {
                                    return item.name == this.infolist.name
                                });
                            if(parseFloat(this.infolist.cost) < parseFloat(this.infolist.lastCost)){
                                MessageBox.alert("框架总额应>=框架剩余总额", "温馨提示", {
                                    confirmButtonText: "确定"
                                })
                            }else if(this.supplierNameOrigin != this.infolist.name && supplierNameArr.length != 0){
                                MessageBox.alert("该供应商已存在", "温馨提示", {
                                    confirmButtonText: "确定"
                                });
                            }else{
                                this.submitInfo("/market/cadvSupplier/update", v);
                            }
                    }
                }
            },
            submitInfo(u) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        this.loadinged = true;
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: this.infolist,
                        }).then((res) => {
                            this.loadinged = false;
                            if (res.data.result === 1) {
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'error'
                                });
                            } else if(res.data.result === -1){
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'fail'
                                });
                            }else {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.infolist = {};
                                _this.dialogStatus = false;
                                _this.getList({}, _this.pageSize);
                            }

                        });
                    }
                })
            },
            closeMessage(v) {
                this.centerDialogVisible = false;
                let _this = this;
                if (v === 1) {
                    _this.$http({
                        url: "/market/cadvSupplier/del",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            code: this.delid
                        },
                    }).then((res) => {
                        if (res.data.result === 1) {
                            _this.$message({
                                message: res.data.desc,
                                type: 'error'
                            });
                        } else {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.getList(this.params, this.pageSize);
                        }
                    });
                }
            },
            //删除
            delLine(v) {
                this.centerDialogVisible = true;
                this.messTit = '即将删除此条数据, 是否删除?';
                this.delid = v.code;
            },
            //文件返回值
            uploadBack(v) {
            },
            //功能栏
            iconCli(v) {
                if (v === 1) {
                    this.getList(this.params, this.pageSize);
                }
                if (v === 2) {
                    this.fullscreen = !this.fullscreen
                }
            },
            getUser() {
                this.userInfo = JSON.parse(window.sessionStorage.userInfo);

            }
        },
        mounted() {
            this.getList({}, 1);
            this.getUser();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .onetab {
        margin-bottom: 20px;
        padding: 0 20px;
    }

    .titbox {
        div {
            float: right;

            i {
                font-size: 22px;
                margin-left: 20px;
                cursor: pointer;
            }
        }
    }

    .tabbox {
        margin-top: 15px;
    }

    .pageBox {
        text-align: right;
        margin-top: 10px;
    }

    .info-line {
        width: 100%;
        display: block;
        padding-left: 20px;

        div {
            width: 50%;
            display: inline-block;
        }

        span {
            width: 80px;
            display: inline-block;
            text-align: left;

            i {
                color: red;
                display: inline-block;
                padding-right: 5px;
            }
        }

        .el-select,
        .el-input {
            width: calc(100% - 100px);
        }
    }

    .online {
        width: 100%;

        .el-select {
            width: calc(100% - 100px);
        }

        span {
            vertical-align: top;
        }

        .el-textarea {
            width: calc(100% - 100px);
        }
    }
    .container .search{
        justify-content: flex-end
    }
    .myfooter{
        display: flex;
        justify-content: flex-end;
    }
</style>
