<template>
  <div class="edit-strategy-report-wrap">
    <el-form
      :model="aeForm"
      :rules="formRules"
      ref="aeForm"
      label-position="right"
      label-width="90px"
      class="demo-aeForm"
      id="login-container"
      @submit.native.prevent
    >
      <el-form-item prop="classifynameArr" label="分类">
        <el-cascader
          @change="userclassidreportdetail"
          :options="optionsArr"
          v-model="aeForm.classifynameArr"
          placeholder="类型筛选"
          size="medium"
          style="width: 500px"
          disabled
        ></el-cascader>
      </el-form-item>
      <el-form-item prop="title" label="标题">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入"
          v-model="aeForm.title"
          size="medium"
          style="width: 500px"
        ></el-input>
      </el-form-item>
      <el-form-item prop="abstract" label="摘要">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入"
          v-model="aeForm.abstract"
          size="medium"
          style="width: 500px"
        ></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-select
          v-model="aeForm.author"
          multiple
          placeholder="请选择"
          size="medium"
          style="width: 500px"
        >
          <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 label="频度">
        <el-select
          v-model="aeForm.frequency"
          placeholder="请选择"
          size="medium"
          style="width: 500px"
        >
          <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 label="创建时间">
        <el-date-picker
          v-model="aeForm.create_time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          size="medium"
          :clearable="false"
          style="width: 500px"
        ></el-date-picker>
      </el-form-item>
      <div style="text-align:center;margin-top:30px;margin-bottom:30px">
          <el-button type="primary" plain @click="close">取消</el-button>
          <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { authorEn } from '@/api/modules/reportAuthorApi.js';
import * as reportEnInterface from '@/api/modules/reportEnApi';
import { strategyReportInterence } from '@/api/api.js'
export default {
  props: {
    id: {
      type: Number,
      default: 0
    }
  },
  watch: {
    'id':{
        handler(n){
            if(n){
                this.getreportdetail()
            }
        },
        immediate:true
    }
  },
  data() {
    return {
      aeForm: {
        classifynameArr: [],
        title: '',
        abstract: '',
        author: ['Horizon Insights FICC Team'],
        frequency: '',
        create_time: '',
      },
      formRules: {
        classifynameArr: [
          {
            required: true,
            message: '',
            trigger: 'blur',
          },
        ],
        title: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
        abstract: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
      },
      optionsArr: [],
      authorlist: []
    }
  },
  created() {
    this.getclassifylist()
    this.getreportauthor()
  },
  methods: {
    getclassifylist() {
      //获取分类列表
      let params = { CurrentIndex: 1, PageSize: 1000, KeyWord: '' };
      reportEnInterface.classifyList(params).then((res) => {
        if (res.Ret == 200 && Array.isArray(res.Data.List)) {
          this.optionsArr = [];
          res.Data.List.forEach(item => {
            let newitem = {
              label: item.ClassifyName,
              value: JSON.stringify({
                l: item.ClassifyName,
                v: parseInt(item.Id),
              }),
              HasTeleconference: item.HasTeleconference
            };
            if (item.Child && item.Child.length > 0) {
              let childnode = [];
              item.Child.forEach((itemchild, i) => {
                childnode.push({
                  label: itemchild.ClassifyName,
                  value: JSON.stringify({
                    l: itemchild.ClassifyName,
                    v: parseInt(itemchild.Id),
                  }),
                  HasTeleconference: itemchild.HasTeleconference
                });
              });
              newitem.children = childnode;
            } else {
              newitem.children = undefined;
            }
            this.optionsArr.push(newitem);
          });
        }
      });
    },
    getreportauthor() {
      authorEn({}).then((res) => {
        if (res.Ret == 200) {
          this.authorlist = res.Data.List || [];
        }
      });
    },
    async getreportdetail() {
      const res = await reportEnInterface.reportDetail({ ReportId: parseInt(this.id) })
      if (res.Ret === 200) {
        let data = res.Data;
        this.aeForm = {
          classifynameArr:
            data.ClassifyIdSecond && data.ClassifyNameSecond
              ? [
                JSON.stringify({
                  l: data.ClassifyNameFirst,
                  v: parseInt(data.ClassifyIdFirst),
                }),
                JSON.stringify({
                  l: data.ClassifyNameSecond,
                  v: parseInt(data.ClassifyIdSecond),
                }),
              ]
              : [
                JSON.stringify({
                  l: data.ClassifyNameFirst,
                  v: parseInt(data.ClassifyIdFirst),
                }),
              ],
          title: data.Title,
          abstract: data.Abstract,
          author: data.Author ? data.Author.split(',') : '',
          frequency: data.Frequency,
          create_time: data.CreateTime,
        };
      }
    },

    handleConfirm(){
        this.$refs.aeForm.validate((valid) => {
          if (valid) {
            strategyReportInterence.reportEdit({
                ReportId: this.id, //英文研报ID
                Title: this.aeForm.title, //标题
                Abstract: this.aeForm.abstract, //摘要
                Author: this.aeForm.author.length > 0
						? this.aeForm.author.join(',')
						: '', //作者
                Frequency: this.aeForm.frequency, //频度
                CreateTime: this.aeForm.create_time //创建时间
            }).then(res=>{
                if(res.Ret===200){
                    this.$message.success('编辑成功')
                    this.$emit('refresh')
                    this.close()
                }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
    },

    close(){
        this.$emit('close')
    }

  },
}
</script>

<style>
.edit-strategy-report-wrap .el-input {
  width: 100%;
}
</style>