<template>
    <el-container style="flex-grow:1">
        <el-main class="containMain">
            <div class="contentTitle">离场登记</div>
            <el-form ref="form" :model="formData" label-width="80px" style="margin-top:20px;">
                <el-form-item label="" label-width="0">
                    <el-input v-model="formData.cowID" prefix-icon="el-icon-search" style="width:300px" maxlength="8"></el-input>
                    <el-button type="primary" @click="getNowCow" style="width:90px;margin-left:12px">确认</el-button>
                </el-form-item>
                <div class="sub" v-if="tableData.length>0">
                    操作牛号：{{tableData[0].value}}
                </div>
                <el-form-item label="离场方式">
                    <el-select v-model="formData.LeftClass" placeholder="请选择">
                        <el-option v-for="item in LeftClass" :key="item['名称']" :label="item['名称']" :value="item['名称']">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="离场去向">
                    <el-select v-model="formData.LeftWhere" placeholder="请选择">
                        <el-option v-for="item in LeftWhere" :key="item['名称']" :label="item['名称']" :value="item['名称']">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="离场原因">
                    <el-select v-model="formData.reason" placeholder="请选择">
                        <el-option v-for="item in reasons" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="处理意见">
                    <el-select v-model="formData.remark" placeholder="请选择">
                        <el-option v-for="item in remarks" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="主动淘汰?">
                    <el-radio-group v-model="formData.isActived">
                        <el-radio label="1">是</el-radio>
                        <el-radio label="0">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="残值">
                    <el-input v-model="formData.remainValue" style="width:220px"></el-input>
                </el-form-item>

                <div style="margin-top:80px">
                    <el-button @click="onSave" type="primary" style="width:98px;" size="mediu">确认保存</el-button>

                </div>
            </el-form>
        </el-main>
        <el-aside class="containAside" width="450px">
            <div class="asideInfo">
                <div class="contentTitle">牛只信息</div>
                <el-table class="handleTable" border :data="tableData" style="width: 100%" :height='(allHeight-70)+"px"' size='small'>
                    <el-table-column prop="key" label="参数">
                    </el-table-column>
                    <el-table-column prop="value" label="当前牛只信信息">
                    </el-table-column>
                </el-table>
            </div>
        </el-aside>
    </el-container>

    <!-- <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small"
                 style="max-width: 500px;">
            <el-form-item prop="icon" label="头像">
                <single-upload v-model="formData.icon"></single-upload>
            </el-form-item>
            <el-form-item prop="username" label="用户名">
                <el-input v-model="formData.username"></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="手机">
                <el-input v-model="formData.phone"></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="密码" v-if="!$route.query.id">
                <el-input v-model="formData.password"></el-input>
            </el-form-item>
            <el-form-item prop="roleId" label="角色">
                <el-select v-model="formData.roleId" multiple placeholder="请选择" style="width: 100%;">
                    <el-option
                        v-for="item in roles"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="departId" label="部门">
                <el-cascader
                    :options="departs"
                    v-model="formData.departId">
                </el-cascader>
            </el-form-item>
            <el-form-item prop="birthday" label="生日">
                <el-date-picker
                    v-model="formData.birthday"
                    format="yyyy-MM-dd"
                    value-format="timestamp">
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="createTime" label="创建时间">
                <el-date-picker
                    v-model="formData.createTime"
                    type="datetime"
                    format="yyyy-MM-dd HH:mm:ss"
                    value-format="timestamp">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div> -->
</template>
<script>
import formValidator from '../formValidator'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
    created() {

        this.getHaleInfo('wsCow', 'getUserDict', ['LeftReason'], 'list').then((data) => {
            this.allReason = data
            this.formData.reason = data[0]['类别']
        })

        this.getHaleInfo('wsCow', 'getUserDict', ['LeftClass'], 'list').then((data) => {
            this.LeftClass = data
        })

        this.getHaleInfo('wsCow', 'getUserDict', ['LeftWhere'], 'list').then((data) => {
            this.LeftWhere = data
        })
    },
    data() {
        return {
            saving: false,
            formData: {
                cowID: '',
                LeftClass: '淘汰',
                LeftWhere: '无害化处理',
                reason: '',
                remark: '',
                isActived: "0",
                remainValue: '1',
                operators: ''
            },
            rules: {
                icon: [
                    { required: true, message: '请上传头像', trigger: 'blur' },
                ],
                username: [
                    { required: true, message: '请输入昵称', trigger: 'blur' },
                ],
                phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            if (!value) {
                                callback(new Error('请输入手机号'));
                            } else if (/^1[3-9]\d{9}$/.test(value)) {
                                callback();
                            } else {
                                callback(new Error('请输入正确的手机号'));
                            }
                        }, trigger: 'blur'
                    }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ],
                roleId: [
                    { required: true, message: '请选择角色', trigger: 'blur' },
                ]
            },
            roles: [],
            departs: [],
            allReason: [],
            LeftClass: [],
            LeftWhere: [],
            tableData: []
        }
    },
    computed: {
        ...mapState(['userInfo', 'allHeight']),
        reasons() {
            var list = []
            this.allReason.forEach(item => {
                if (list.indexOf(item['类别']) == -1) {
                    list.push(item['类别'])
                }
            })
            return list
        },
        remarks() {
            var list = []
            if (this.formData.reason) {
                this.allReason.forEach(item => {
                    if (item['类别'] == this.formData.reason) {
                        list.push(item['名称'])
                    }
                })
            }
            return list
        }
    },
    watch: {
        'formData.reason'() {
            setTimeout(() => {
                this.formData.remark = this.remarks[0]
            }, 100)
        },
        'formData.cowID'() {
            if (this.formData.cowID.length != 8) {
                return
            }
            this.getNowCow()
        }
    },
    methods: {
        onSave() {
            if (!this.formData.cowID) {
                this.$message({
                    message: '请录入离场牛只牛号',
                    type: 'warning'
                });
                return
            }
            this.formData.operators = this.userInfo.username
            var list = []
            Object.keys(this.formData).forEach(item => {
                if (item == 'isActive') {
                    list.push(this.formData.isActive == '1' ? true : false)
                }
                else {
                    list.push(this.formData[item])
                }

            })
            this.getHaleInfo('wsCow', 'addLeftInfo', list, 'json').then((data) => {
                // console.log(data)
                if (data.rtnCode == -1) {
                    this.$message({
                        message: data.rtnMessage,
                        type: 'warning'
                    });
                    this.formData.cowID = ''
                }
                else {
                    this.$message({
                        message: '离场信息录入成功',
                        type: 'warning'
                    });
                    this.formData.cowID = ''
                }
            })
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));
            this.$http.post({
                url: this.formData.id ? '/userInfo/update' : '/userInfo/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                    this.$router.go(-1);
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        getNowCow() {

            this.getCowInfo(this.formData.cowID).then((data) => {
                // console.log(data)
                var list = []
                Object.keys(data).forEach(item => {
                    list.push({
                        key: item,
                        value: data[item]
                    })
                })

                // console.log(list)

                this.tableData = list
            }).catch(() => {
                this.formData.cowID = ''
            })
        }
    }
}
</script>
<style lang="less" scoped>
.containMain {
    background: rgba(255, 255, 255, 1);
    border-radius: 4px;
    padding: 15px;
    // margin-right: 10px;
    .contentTitle {
        border-bottom: 1px solid #f2f4f5;
    }
    // min-width: 450px;

    .sub{
        font-size:14px;
font-weight:600;
color:rgba(102,102,102,1);
line-height:20px;
margin-bottom: 40px;
padding-left: 20px;
    }
}

.contentTitle {
    font-size: 16px;
    font-weight: bold;
    color: rgba(0, 0, 0, 1);
    line-height: 22px;
    letter-spacing: 1px;
    padding-bottom: 14px;
}

.containAside {
    padding: 0;
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    .asideInfo {
        flex-grow: 1;
        background: rgba(255, 255, 255, 1);
        border-radius: 4px;
        padding: 15px;
    }
}
</style>
