<template>
  <a-modal
    centered
    class="productBrandEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="960">
    <!-- 表单 -->
    <div class="modalContext">
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="productBrandEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="品牌名称" prop="brandName">
            <a-input
              id="productBrandEdit-brandName"
              :maxLength="30"
              v-model.trim="form.brandName"
              placeholder="请输入产品品牌名称(最多30个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="品牌分类" prop="brandType">
            <v-select code="BRAND_TYPE" id="productBrandEdit-brandType" v-model="form.brandType" allowClear placeholder="请选择品牌分类"></v-select>
          </a-form-model-item>
          <a-form-model-item label="排序字母" prop="sortChar">
            <a-select placeholder="请选择排序字母" id="productBrandEdit-sortChar" allowClear v-model="form.sortChar">
              <a-select-option v-for="item in sortCharList" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="品牌图片" prop="imageUrl">
            <Upload
              class="upload"
              id="productBrandEdit-imageUrl"
              v-model="form.imageUrl"
              ref="imageUrl"
              @change="changeHomeImage"
              listType="picture-card" ></Upload>
            <span class="upload-desc">说明：图片比例1:1，尺寸为120*120px，可上传1张。</span>
          </a-form-model-item>
          <a-form-model-item label="品牌介绍" prop="brandIntroduce">
            <editor id="productBrandEdit-brandIntroduce" ref="editor" class="productBrandEdit-brandIntroduce" @on-change="editorChange" :cache="false"></editor>
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </div>
    <div class="btn-cont">
      <a-button id="productBrandEdit-back" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
      <a-button type="primary" id="productBrandEdit-save" @click="handleSave">保存</a-button>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { VSelect, Upload } from '@/components'
import Editor from '@/components/WEeditor'
import dataList from '@/libs/commonData'
// 接口
import { productBrandSnDetail, productBrandSave } from '@/api/productBrand'
export default {
  name: 'ProductBrandEditModal',
  mixins: [commonMixin],
  components: { VSelect, Upload, Editor },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {// 当前产品品牌sn
      type: [Number, String],
      default: ''
    },
    nowData: {// 当前产品品牌信息
      type: Object,
      default: null
    }
  },
  computed: {
    // 弹框标题
    modalTit () {
      return (this.itemSn ? '编辑' : '新增') + '产品品牌'
    }
  },
  data () {
    return {
      spinning: false,
      sortCharList: dataList.sortCharList, // 排序字符数据
      isShow: this.openModal, //  是否打开弹框
      // label 表单 布局设置
      formItemLayout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 19 }
      },
      // 表单数据
      form: {
        brandName: '', // 品牌名称
        brandType: undefined, // 品牌分类
        sortChar: undefined, // 排序字母
        imageUrl: '', // 品牌图片
        brandIntroduce: ''// 品牌介绍
      },
      // 表单验证规则
      rules: {
        brandName: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' }
        ],
        brandType: [
          { required: true, message: '请选择品牌分类', trigger: 'change' }
        ],
        sortChar: [
          { required: true, message: '请选择排序字母', trigger: 'change' }
        ],
        imageUrl: [
          { required: true, message: '请上传品牌图片', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    //  获取详情数据
    getDetail () {
      productBrandSnDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          const data = res.data
          this.form = Object.assign(this.form, data)
          this.$refs.imageUrl.setFileList(data.imageUrl)
          this.$refs.editor.setHtml(res.data.brandIntroduce)
        } else {
          this.$refs.ruleForm.resetFields()
        }
      })
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(_this.form))
          formData.id = undefined
          formData.brandSn = _this.itemSn ? _this.itemSn : undefined
          _this.spinning = true
          productBrandSave(formData).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              _this.isShow = false
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    //  图片上传
    changeHomeImage (file) {
      this.form.imageUrl = file
    },
    //  文本编辑器
    editorChange (html, text) {
      this.form.brandIntroduce = html
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.imageUrl.setFileList('')
        this.$refs.editor.setHtml('')
        this.$refs.ruleForm.resetFields()
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less">
  .productBrandEdit-modal{
    .upload{
      width: 100%!important;
    }
    //  商品图片描述
    .upload-desc{
      font-size: 12px;
      color: #808695;
    }
    //  文本编辑器  工具栏样式换行
    .productBrandEdit-editor{
      .w-e-toolbar{
        flex-wrap: wrap;
      }
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
    .modalContext{
      padding: 0 40px;
      max-height:630px;
      overflow-y: scroll;
    }
  }
</style>
