<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <el-button @click="back">返 回</el-button>
        <el-button type="success" @click="submitForm">暂 存</el-button>
        <el-button type="primary" @click="submitForm">发 布</el-button>
      </el-col>
    </el-row>
    <el-divider></el-divider>
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="产品名称" prop="title">
            <el-input v-model="form.title" placeholder="请输入产品名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="所属分类" prop="catgId">
            <div class="dl-flex-column">
              <el-input v-model="form.catgId" placeholder="请输入产品名称"/>
              <div class="dl-add-catg">添加产品分类</div>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" type="number" placeholder="请输入排序"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="产品主图" prop="mainPic">
            <el-tag style="cursor: pointer">图片库选择</el-tag>
            <image-upload @uploadedImg="uploadedImg" v-model="form.mainPic" :limit="1"/>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="产品图库" prop="pics">
            <el-tag style="cursor: pointer">图片库选择</el-tag>
            <image-upload @uploadedImg="uploadedImg" v-model="form.pics" :limit="9"/>
          </el-form-item>
        </el-col>
      </el-row>
      <!--      <el-form-item label="页面title" prop="prodTitle">-->
      <!--        <el-input v-model="form.prodTitle" placeholder="请输入页面title"/>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="页面keyword" prop="prodKeyword">-->
      <!--        <el-input v-model="form.prodKeyword" type="textarea" placeholder="请输入内容"/>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="页面description" prop="prodDescription">-->
      <!--        <el-input v-model="form.prodDescription" type="textarea" placeholder="请输入内容"/>-->
      <!--      </el-form-item>-->
      <el-row>
        <el-col :span="18">
          <el-form-item label="产品简介" prop="description">
            <div class="dl-flex-column">
              <el-input style="width: 80%" ref="descriptionInput" v-model="form.description" type="textarea"
                        placeholder="请输入内容" @blur="handleBlur"
              />
              <el-tag style="cursor: pointer;margin-left: 10px" @click="insertBr">插入换行符</el-tag>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="首页推荐" prop="ifReco">
            <el-radio-group v-model="form.ifReco">
              <el-radio
                v-for="dict in dict.type.true_or_false"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="产品内容">
        <editor v-model="form.content" :min-height="192"/>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-row>
      <el-col :span="24">
        <el-button @click="back">返 回</el-button>
        <el-button type="success" @click="saveTmp">暂 存</el-button>
        <el-button type="primary" @click="submitForm">发 布</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listProdNew, getProdNew, delProdNew,getMaxSort, addProdNew, updateProdNew } from '@/api/busi/prod'

export default {
  name: 'prodForm',
  dicts: ['true_or_false'],
  data() {
    return {
      //光标位置--简介
      cursorPos: null,
      // 表单参数
      form: {
        id: null,
        catgId: null,
        title: null,
        prodTitle: null,
        prodKeyword: null,
        prodDescription: null,
        newsFrom: null,
        mainPic: null,
        pics: null,
        description: null,
        content: null,
        sort: null,
        ifReco: 0,
        ifPublic: 0,
        tenantId: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: null,
        delFlag: null,
        //当前上传的所有图片
        fileList:[],
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: '请输入产品名称', trigger: 'blur' }
        ],
        catgId: [
          { required: true, message: '请选择产品分类', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' }
        ],
        mainPic: [
          { required: true, message: '请上传产品主图', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入产品简介', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData(){
      getMaxSort({}).then(response => {
        this.form.sort = response.data
      })
    },
    /**
     * 返回上一页
     */
    back() {
      history.back()
    },
    //图片上传回调
    uploadedImg(fileList){
      this.form.fileList = this.form.fileList.concat(fileList)
    },
    handleBlur(e) {
      this.cursorPos = e.srcElement.selectionStart
    },
    /**
     * 插入换行符
     */
    insertBr() {
      let str = '<br/>'
      if (!this.form.description) {
        this.form.description = str
      } else {
        const start = this.form.description.substring(0, this.cursorPos)
        const end = this.form.description.substring(this.cursorPos)
        this.form.description = `${start}${str}${end}`
      }
    },
    /** 暂存 */
    saveTmp(){
      if (this.form.id != null) {
        updateProdNew(this.form).then(response => {
          this.$modal.msgSuccess('修改成功')
          setTimeout(()=>{
            history.back()
          },1000)
        })
      } else {
        addProdNew(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          setTimeout(()=>{
            history.back()
          },1000)
        })
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.ifPublic=1
          this.saveTmp()
        }
      })
    },
  }
}
</script>

<style scoped>
.dl-add-catg {
  cursor: pointer;
  width: 130px;
  text-align: center;
  color: #1890ff;
}
</style>
