<template>
    <el-dialog
        title="基础信息"
        :visible.sync="show"
        :modal-append-to-body="false"
        :close-on-click-modal="false"
        :center="true"
        v-dialogDrag
        custom-class="dialogclass"
        width="440px"
        @close="handleClose"
    >
        <el-form 
            :model="formData" 
            :rules="rules" 
            ref="baseinfoForm"
            class="baseinfo-form-wrap"
        >  
            <el-form-item prop="type">
                <el-radio-group v-model="formData.type" :disabled="id" @change="handleUpdateBaseInfo">
                    <el-radio :label="1">新增报告</el-radio>
                    <el-radio :label="2">继承报告</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item prop="classify">
                <el-cascader
					ref="cascader"
					:options="classifyArr"
					v-model="formData.classify"
					placeholder="请选择分类"
					size="medium"
                    style="width:340px"
                    @change="handleUpdateBaseInfo"
				/>
            </el-form-item>
            <el-form-item prop="title">
                <el-input placeholder="请输入标题" v-model="formData.title" style="width:340px"></el-input>
            </el-form-item>
            <el-form-item prop="abstract">
                <el-input type="textarea" placeholder="请输入摘要" v-model="formData.abstract" style="width:340px"></el-input>
            </el-form-item>
            <el-form-item prop="author">
				<el-select
					v-model="formData.author"
					multiple
					placeholder="请选择作者"
					size="medium"
					style="width: 340px"
				>
					<el-option
						v-for="(item, i) in authorlist"
						:key="i"
						:label="item.ReportAuthor"
						:value="item.ReportAuthor"
					></el-option>
				</el-select>
			</el-form-item>
            <el-form-item prop="frequency">
                <el-select
					v-model="formData.frequency"
					placeholder="请选择频度"
					size="medium"
					style="width: 340px"
				>
                    <el-option label="年度" value="年度"></el-option>
                    <el-option label="半年度" value="半年度"></el-option>
                    <el-option label="季度" value="季度"></el-option>
                    <el-option label="月度" value="月度"></el-option>
                    <el-option label="双周度" value="双周度"></el-option>
                    <el-option label="周度" value="周度"></el-option>
                    <el-option label="日度" value="日度"></el-option>
                    <el-option label="不定时" value="不定时"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="time">
                <el-date-picker
					v-model="formData.time"
					type="date"
					value-format="yyyy-MM-dd"
					placeholder="选择日期"
					size="medium"
					:clearable="false"
					style="width: 340px"
				></el-date-picker>
            </el-form-item>
        </el-form>
        <div style="text-align:center;margin-top:60px;margin-bottom:40px">
            <el-button type="primary" plain style="width:120px" @click="handleClose">取消</el-button>
            <el-button type="primary" style="width:120px" @click="handleConfirm">确定</el-button>
        </div>
    </el-dialog>
</template>

<script>
import {apiSmartReport}  from '@/api/modules/smartReport'
export default {
    name:"BaseInfo",
    model:{
        prop:'show',
        event:'showChange'
    },
    props:{
        show:{
            type:Boolean,
            default:false
        },
        id:{
            type:Number,
            default:0
        }
    },
    watch: {
        show(n){
            if(!n){
                this.formData.type=1
                this.formData.classify=[]
                this.formData.title=''
                this.formData.abstract=''
                this.formData.author=['FICC团队']
                this.formData.frequency='日度'
                this.formData.time=this.$moment().format('YYYY-MM-DD')
            }else{
                if(this.id){
                    apiSmartReport.reportDetail({
                        SmartReportId:Number(this.id)
                    }).then(res=>{
                        if(res.Ret===200){
                            this.formData.type=res.Data.AddType
                            this.formData.classify=[res.Data.ClassifyIdFirst,res.Data.ClassifyIdSecond]
                            this.formData.title=res.Data.Title
                            this.formData.abstract=res.Data.Abstract
                            this.formData.author=res.Data.Author ? res.Data.Author.split(',') : []
                            this.formData.frequency=res.Data.Frequency
                            this.formData.time=res.Data.CreateTime
                        }
                    })
                }
            }
        }  
    },
    data() {
        return {
            rules:{
                type:[{ required: true, message: '请选择报告类型', trigger: 'change' }],
                classify:[{ required: true, message: '请选择报告分类', trigger: 'change' }],
                title:[{ required: true, message: '请填写报告标题', trigger: 'blur' }],
            },
            formData:{
                type:1,
                classify:[],
                title:'',
                abstract:'',
                author:['FICC团队'],
                frequency:'日度',
                time:this.$moment().format('YYYY-MM-DD')||''
            },
            classifyArr:[],
            authorlist:[]
        }
    },
   
    methods: {
        handleClose(){
            this.$emit('showChange', false)
        },

        handleConfirm(){
            this.$refs.baseinfoForm.validate((valid)=>{
                if(valid){

                    const params={
                        AddType: this.formData.type,
                        ClassifyIdFirst: this.formData.classify[0]?this.formData.classify[0]:0,
                        ClassifyNameFirst: '',
                        ClassifyIdSecond:this.formData.classify[1]?this.formData.classify[1]:0,
                        ClassifyNameSecond:'',
                        Title: this.formData.title,
                        Abstract: this.formData.abstract,
                        Author:
                            this.formData.author.length > 0
                                ? this.formData.author.join(',')
                                : '',
                        Frequency: this.formData.frequency,
                        CreateTime: this.formData.time,
                    }
                    this.classifyArr.forEach(item=>{
                        if(item.value===params.ClassifyIdFirst){
                            params.ClassifyNameFirst=item.label
                            const arr=item.children||[]
                            arr.forEach(_item=>{
                                if(_item.value===params.ClassifyIdSecond){
                                    params.ClassifyNameSecond=_item.label
                                }
                            })
                        }
                    })
                    // 编辑
                    if(this.id){
                        this.$emit('save',params)
                        return
                    }
                    apiSmartReport.reportAdd(params).then(res=>{
                        if(res.Ret===200){
                            this.handleClose()
                            let { href } = this.$router.resolve({
                                path: "/smartReportEdit",
                                query: { id: res.Data.SmartReportId },
                            });
                            window.open(href, "_blank");
                        }
                    })

                }
            })
        },

        handleUpdateBaseInfo(){
            if(this.formData.type===1){
                if(this.formData.classify.length===2){
                    this.formData.title=this.getSelectClassifyName()[1]
                }
                return
            }
            //获取上次报告
            apiSmartReport.getLastReport({
                ClassifyIdFirst:this.formData.classify[0],
                ClassifyIdSecond:this.formData.classify[1]
            }).then(res=>{
                if(res.Ret!==200) return
                if (res.Data == null) {
					this.$message.warning('此分类暂无报告');
					return false;
				}
                this.formData.title=res.Data.Title
                this.formData.abstract=res.Data.Abstract
                this.formData.author=res.Data.Author ? res.Data.Author.split(',') : ''
                this.formData.frequency=res.Data.Frequency
            })
        },

        // 获取选择的分类名称
        getSelectClassifyName(){
            let arr=[]
            this.classifyArr.forEach(item=>{
                if(this.formData.classify[0]&&item.value===this.formData.classify[0]){
                    arr.push(item.label)
                    if(item.children&&item.children.length>0){
                        item.children.forEach(_item=>{
                            if(this.formData.classify[1]&&_item.value===this.formData.classify[1]){
                                arr.push(_item.label)
                            }
                        })
                    }
                }
            })
            return arr
        },

        // 获取分类
        getclassifylist() {
            let params = { CurrentIndex: 0, PageSize: 1000, KeyWord: "",HideDayWeek:1,/*不显示晨报/周报*/ };
            apiSmartReport.classifyList(params).then((res) => {
                if (res.Ret == 200 && Array.isArray(res.Data.List)) {
                    this.classifyArr = [];
                    res.Data.List.forEach((item, index) => {
                        let newitem = {
                            label: item.ClassifyName,
                            value: item.Id,
                        };
                        if (item.Child) {
                            let childnode = [];
                            item.Child.forEach((itemchild, i) => {
                                childnode.push({
                                    label: itemchild.ClassifyName,
                                    value: itemchild.Id,
                                });
                            });
                            newitem.children = childnode;
                        }
                        this.classifyArr.push(newitem);
                    });
                }
            });
        },
        // 获取作者
        getreportauthor() {
			apiSmartReport.reportAuthor({}).then((res) => {
				if (res.Ret == 200) {
					this.authorlist = res.Data.List || [];
				}
			});
		},
    },
    mounted(){
        this.getclassifylist()
        this.getreportauthor()
    },
}
</script>

<style lang="scss">
.baseinfo-form-wrap{
    .el-input{
        width: 100%;
    }
    .el-form-item{
        width: 340px;
        margin-left: auto;
        margin-right: auto;
    }
}
</style>