<template>
  <a-modal
    centered
    class="productBrandEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancle="isShow=false"
    :width="960">
    <!-- 表单 -->
    <div>
      <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>
        <div class="btn-cont">
          <a-button type="primary" id="product-brand-edit-modal-save" @click="handleSave">保存</a-button>
          <a-button id="product-brand-edit-modal-back" @click="isShow = false" style="margin-left: 15px;">返回列表</a-button>
        </div>
      </a-spin>
    </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: {
      type: [Number, String],
      default: ''
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  computed: {
    modalTit () {
      return (this.itemSn ? '编辑' : '新增') + '产品品牌'
    }
  },
  data () {
    return {
      sortCharList: dataList.sortCharList,
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      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{
    .ant-modal-body {
    	padding: 40px 40px 24px;
    }
    .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;
    }
  }
</style>
