<template>
  <divider-container title="内容信息">
    <el-form ref="ruleForm" :model="form" :rules="rules">
      <el-row :gutter="10">
        <el-col class="txt-right" :span="2">图标</el-col>
        <el-col :span="22">
          <el-form-item prop="imgFile">
            <upload v-model="form.imgFile" params="prefix=/article" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col class="txt-right" :span="2">分享图标：</el-col>
        <el-col :span="22">
          <el-form-item>
            <upload v-model="form.shareImgFile" params="prefix=/share" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col class="txt-right" :span="2">内容文案</el-col>
        <el-col :span="22">
          <el-form-item prop="text">
            <RichText v-model="form.text" />
            <!-- <tinymce v-model="form.text" :height="300" /> -->
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col class="txt-right" :span="2">关联医生</el-col>
        <el-col :span="22">
          <el-form-item prop="doctorId">
            <DoctorSelect v-model="form.doctorId" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </divider-container>
</template>

<script>
import { UPLOAD_URL_DEMO } from '@/const/urlKey'
// import Tinymce from '@/components/Tinymce'
import DoctorSelect from './DoctorSelect'
import { cutUrl } from '@/utils'

export default {
  name: 'ArticleItemText',

  components: { DoctorSelect },

  props: {
    info: {
      type: Object,
      default: () => ({})
    },
    check: {
      type: Boolean,
      default: false
    }
  },

  data() {
    const validateImg = (rule, value, callback) => {
      if (!Array.isArray(value)) return callback(new Error('数据出错，请重新上传'))
      if (value.length < 1) return callback(new Error('请先上传图片'))
      return callback()
    }

    return {
      form: {
        shareImgFile: this.info.shareUrl || [{ url: UPLOAD_URL_DEMO }],
        text: this.info.text,
        doctorId: this.info.doctorId,
        imgFile: this.info.coverUrl || [{ url: UPLOAD_URL_DEMO }],
        able: false
      },
      rules: {
        text: [{ required: true, message: '请输入文案', trigger: 'blur' }],
        imgFile: [{ validator: validateImg, trigger: 'change' }],
        shareImgFile: [{ validator: validateImg, trigger: 'change' }],
        doctorId: [{ required: true, message: '请选择医生', trigger: 'change' }]
      }
    }
  },

  watch: {
    check: {
      handler(val) {
        if (val) {
          this.$nextTick(() => {
            this.handleSubmit()
          })
        }
      },
      immediate: true
    },
    'form.shareImgFile': {
      handler(val) {
        this.$refs['ruleForm'].validateField('shareImgFile')
      },
      deep: true
    }
  },

  mounted() {
  },

  methods: {
    handleSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        this.form.able = valid
        if (!valid) {
          this.$message.error('请检查内容信息是否填写完成~')
        } else {
          this.$emit('submit', {
            shareUrl: cutUrl(this.form.shareImgFile[0].url),
            contentType: 'text',
            contentText: this.form.text,
            coverUrl: cutUrl(this.form.imgFile[0].url),
            doctorId: this.form.doctorId
          })
        }
      })
      this.$emit('reset-check')
    }
  }
}
</script>

<style scoped>
.mt-60 {
  margin-top: 60px;
}
.btn-right {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.mcol-20 .el-row {
  font-size: 14px;
  line-height: 1.7;
  padding-top: 14px;
}
.search-box {
  border: 1px #DCDEE2 solid;
  padding: 5px;
}
.search-box .search-input {
  margin: 0 5px;
  max-width: 200px;
}

.txt-right {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  font-weight: bold;
}
</style>
