<template>
  <!-- 车型：创建、编辑 -->
  <div>
    <h4>{{popupTitle}}</h4>
    <el-form :model="form" :rules="rules" ref="form" label-width="90px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="文章标题" prop="title">
            <el-input v-model="form.title" type="text" placeholder="请输入文章标题"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="文章摘要" prop="epitome">
            <el-input v-model="form.epitome" type="text"  placeholder="请输入文章摘要"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="title" prop="tagTitle">
            <el-input v-model="form.tagTitle" type="text"  placeholder="请输入文章摘要"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="description" prop="description">
            <el-input v-model="form.description" type="text"  placeholder="请输入文章摘要"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="keywords" prop="keywords">
            <el-input v-model="form.keywords" type="text"  placeholder="请输入文章摘要"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="作者" prop="author">
            <el-input v-model="form.author" type="text" placeholder="请输入文章作者"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="发布人" prop="publisher">
            <el-input v-model="form.publisher"  type="text"placeholder="请输入发布人"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="是否上架">
        <el-radio class="radio" v-model="form.status" label="1">是</el-radio>
        <el-radio class="radio" v-model="form.status" label="0">否</el-radio>
      </el-form-item>
      <el-row>
        <el-col :span="8">
          <el-form-item label="发布网站">
            <el-radio class="radio" v-model="form.type" label="0">所有网站</el-radio>
            <el-radio class="radio" v-model="form.type" label="1">新欣房车官网</el-radio>
            <el-radio class="radio" v-model="form.type" label="2">滴房车官网</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="封面图">
        <el-upload
          class="upload-demo"
          :headers="getHeaderWithToken"
          :action="BASE_API+'/api/universal/file/app/unauth/admin/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :on-progress="uploadProcess"
          list-type="picture">
          <el-progress v-show="imgFlag == true" type="circle" :percentage="percent"
                       style="margin-top: 20px"></el-progress>
          <img v-if="$utils.isString(form.coverImage) && !$utils.isEmpty(form.coverImage) && !imgFlag" :src="form.coverImage"
               style="width:300px;max-height:300px;">
          <i v-else-if="!imgFlag" class="el-icon-plus avatar-uploader-icon"
             style="lineHeight:100px;width:300px;height: 100px;border: 1px dashed #ccc;"></i>
        </el-upload>
      </el-form-item>
      <el-row>
      <el-col :span="8">
      <el-form-item label="alt">
        <el-input v-model="form.alt" type="text" placeholder="请输入描述"></el-input>
      </el-form-item>
      </el-col>
      </el-row>
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="文章内容" name="first">
          <div class="editor-container" v-if="activeName=='first'">
            <UE :editorId="activeName" :defaultMsg="form.text" :config=config ref="ue"
                @ready="editorReadyEvent"></UE>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" v-if="popupTitle=='创建'" @click="create('form')">确 定</el-button>
      <el-button type="primary" v-if="popupTitle=='编辑'" @click="update('form')">确 定</el-button>
    </div>

  </div>
</template>
<style>
  textarea {
    display: none;
  }

  .el-upload-list {
    display: flex;
    width: 100px;
  }

  .el-upload-list li {
    margin-left: 10px;
  }
</style>
<script>
  import UE from '../../modal/Ueditor';//百度ue富文本

  import {
    add,
    update,
    getOne
  } from 'src/api/officialWebsite/news';
  import {
    getToken
  } from '../../../utils/auth';
  import {mapGetters} from 'vuex';

  import ElRow from "element-ui/packages/row/src/row";
  import ElCol from "element-ui/packages/col/src/col";
  import ElFormItem from "../../../../node_modules/element-ui/packages/form/src/form-item.vue";
  import ElRadio from "../../../../node_modules/element-ui/packages/radio/src/radio.vue";

  export default {
    props: ["oneRow","popupTitle"],//
    name: 'popup',
    components: {
      ElRadio,
      ElFormItem,
      ElCol,
      ElRow,
      UE
    },
    data() {
      return {
        imgPosterFlag: false,
        percent_poster: 0,
        BASE_API: process.env.BASE_API,
        percent: 0,//上传进度
        imgFlag: false,
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 350
        },
        fileList2: [],
        activeName: '',
        form: {
          title: undefined,
          status: '0',
          type: '0',
          epitome: undefined,
          text:"",
          author:undefined,
          publisher:undefined,
            alt:undefined
        },
        rules: {
          title: {
            type: 'string',
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          },
          epitome: {
            type: 'string',
            required: true,
            message: '请输入文章摘要',
            trigger: 'blur'
          },

        },
      }
    },
    created() {

    },

    mounted() {
      this.getAllTag();
      // this.cleanForm();
    },
    computed: {
      ...mapGetters([
        'elements',
        'vehicleWarningMsgType'
      ]),
      getHeaderWithToken() {
        return {Authorization: getToken()};
      }
    },
    methods: {
      getAllTag() {
        if(this.popupTitle == "编辑"){
          let row = this.oneRow;
          this.getOne(row.id);
        } else {
          this.activeName = 'first';
        }
      },
      /**
       * 封面图上传进度
       * */
      uploadProcess(event, file, fileList) {
        this.imgFlag = true;
        console.log(event.percent);
        this.percent = Math.floor(event.percent);
      },
      /**
       * 封面图上传
       * **/
      handleAvatarSuccess(res, file) {
          console.log(res)
        this.percent = 0;
        this.imgFlag = false;
        this.form.coverImage = res.data;
      },

      /**
       * 创建
       * */
      create(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            add(this.form).then(response => {
              if (response.status === 200) {
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                });
                this.$emit("oneDialogEvent", true);
              } else {
                this.$notify({
                  title: '创建失败',
                  message: '操作失败！',
                  type: 'error',
                  duration: 2000
                });
              }
            });
          } else {
            return;
          }
        });
      },
      /**
       * 编辑
       * */
      update(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            update(this.form).then(response => {
              if (response.status === 200) {
                this.$notify({
                  title: '成功',
                  message: '编辑成功',
                  type: 'success',
                  duration: 2000
                });
                this.$emit("oneDialogEvent", true);
              } else {
                this.$notify({
                  title: '编辑失败',
                  message: '操作失败！',
                  type: 'error',
                  duration: 2000
                });
              }
            });
          } else {
            return;
          }
        });
      },
      /**
       * 获取一条车型数据
       * */
      getOne(id) {
        let query = {id:id,urlType:2}
        getOne(query).then(response => {
        if(this.$utils.isInteger(response.data.status)) {
          response.data.status=response.data.status.toString()
          response.data.type=response.data.type.toString()
          }
          response.data.text=response.data.text?response.data.text:''
          this.form = response.data;
          this.activeName = 'first';
        })
      },
      /**
       * 深拷贝
       */
      objDeepCopy(source) {
        var sourceCopy = source instanceof Array ? [] : {};
        for (var item in source) {
          sourceCopy[item] = typeof source[item] === 'object' ? objDeepCopy(source[item]) : source[item];
        }
        return sourceCopy;
      },
      /**
       * 富文本内容
       * @param step
       */
      editorReadyEvent(instance) {
        let t = this;
        instance.addListener('contentChange', () => {
          t.form.text = instance.getContent();
        });
      },
      /**
       * 关闭编辑
       */
      cancel(){
        this.cleanForm();
        this.$emit("oneDialogEvent", false);
      },
      /**
       * 弹框数据
       */
      cleanForm() {
        this.activeName = '';
        this.form = {
          title: undefined,
          status: '0',
          type: '0',
          epitome: undefined,
          text:"",
          author:undefined,
          publisher:undefined,
            alt:undefined
        }
      }
    }
  }
</script>
<style>
  .flex-aic {
    display: flex;
    align-items: center;
  }

  .left-item {
    width: 300px;
    border-left: 1px solid #999;
    border-right: 1px solid #999;
    border-bottom: 1px solid #999;
    padding: 3px;
  }

  .right-item {
    width: 400px;
    border-right: 1px solid #999;
    border-bottom: 1px solid #999;
    padding: 3px;
  }
</style>
