<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-row :gutter="20">
        <el-col :md="12">
          <el-form-item label="商品id" prop="ProductId">
            <el-input v-model="ruleForm.ProductId" placeholder="请输入商品id" :disabled="addEditJudgment"/>
          </el-form-item>
        </el-col>
        <el-col :md="12">
          <el-form-item label="商品分类" prop="ClassName">
            <el-cascader
              v-model="parentids"
              :options="commodityTypeLists"
              :props="typeDropProps"
              clearable
              filterable
              change-on-select
              @change="commodityTypeChange"
              ref="myCascader"
              :placeholder="updateForm.categoriesPlaceholder"
              style="width: 100%;"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :md="12">
          <el-form-item label="商品名称" prop="ProductName">
            <el-input v-model="ruleForm.ProductName" placeholder="请输入商品名称"/>
          </el-form-item>
        </el-col>
        <el-col :md="12">
          <el-form-item label="商品副标题" prop="ProductShortName">
            <el-input v-model="ruleForm.ProductShortName" placeholder="请输入商品副标题"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :md="12">
          <el-form-item label="款号" prop="ProductNumber">
            <el-input v-model="ruleForm.ProductNumber" placeholder="请输入款号"/>
          </el-form-item>
        </el-col>
        <el-col :md="12">
          <el-form-item label="款号名称/品名" prop="ProductNumberName">
            <el-input v-model="ruleForm.ProductNumberName" placeholder="请输入款号名称/品名"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :md="12">
          <el-form-item label="市场价格" prop="MarketPrice">
            <el-input v-model.number="ruleForm.MarketPrice" placeholder="请输入市场价格"/>
          </el-form-item>
        </el-col>
        <el-col :md="12">
          <el-form-item label="会员价格" prop="MemberPrice">
            <el-input v-model.number="ruleForm.MemberPrice" placeholder="请输入会员价格"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :md="12">
          <el-form-item label="特惠价格" prop="SpecialPrice">
            <el-input v-model.number="ruleForm.SpecialPrice" placeholder="请输入特惠价格"/>
          </el-form-item>
        </el-col>
        <el-col :md="12">
          <el-form-item label="拼音简写" prop="PinyinShort">
            <el-input v-model="ruleForm.PinyinShort" placeholder="请输入拼音简写"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :md="12">
          <el-form-item label="商品标签" prop="tag">
            <el-select v-model="ruleForm.tag" placeholder="请选择商品标签" class="form_select" clearable>
              <el-option
                v-for="item in commodityLabelLists"
                :key="item.F_TagName"
                :label="item.F_TagName"
                :value="item.F_TagName">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="12">
          <el-form-item label="产地" prop="Address">
            <el-input v-model="ruleForm.Address" placeholder="请输入产地"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :md="12">
          <el-form-item label="厂家" prop="Factory">
            <el-input v-model="ruleForm.Factory" placeholder="请输入厂家"/>
          </el-form-item>
        </el-col>
        <el-col :md="12">
          <el-form-item label="供应商" prop="Supplier">
            <el-input v-model="ruleForm.Supplier" placeholder="请输入供应商"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :md="12">
          <el-form-item label="上架时间" prop="OnSaleBegin">
            <el-date-picker v-model="ruleForm.OnSaleBegin" type="datetime" placeholder="请选择上架时间" class="form_date" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :md="12">
          <el-form-item label="下架时间" prop="OnSaleEnd">
            <el-date-picker v-model="ruleForm.OnSaleEnd" type="datetime" placeholder="请选择下架时间" class="form_date" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :md="12">
          <el-form-item label="是否赠品" prop="IsSale">
            <el-select v-model="ruleForm.IsSale" placeholder="是否赠品" class="form_select">
              <el-option label="赠品" value="0"></el-option>
              <el-option label="不是赠品" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="12">
          <el-form-item label="描述" prop="Des">
            <el-input v-model="ruleForm.Des" placeholder="请输入描述"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item>
            <el-button type="primary" @click="submitForm">保存</el-button>
            <el-button v-if="judgmentAddEditReset" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { getCommodityList, addCommodityList, editCommodityList } from '@/api/commodityManagement/commodityList'
import { getCommodityLabel } from '@/api/commodityManagement/commodityLabel'
import { getCommodityType } from '@/api/commodityManagement/commodityType'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      parentids:[],
      commodityTypeLists: [], //商品类型下拉
      // 自定义菜单下拉数据的key值
      typeDropProps: {
        value: 'id',
        label: 'text'
      },
      commodityLabelLists: [], //商品标签下拉
      ruleForm: {
        ProductId: '', //商品id
        ClassId: '', //商品类型id
        ClassName: '', //商品类型名称
        ProductName: '', //商品名称
        ProductShortName: '', //副标题
        ProductNumber: '', //款号
        ProductNumberName: '', //款号名称/品名
        MarketPrice: '', //市场价格
        MemberPrice: '', //会员价格
        SpecialPrice: '', //特惠价格
        PinyinShort: '', //拼音简写
        tag: '', //商品标签
        Address: '', //产地
        Des: '', //描述
        Factory: '', //厂家
        Supplier: '', //供应商
        OnSaleBegin: '', //上架时间
        OnSaleEnd: '', //下架时间
        IsSale: "1", //是否赠品
      },
      rules: {
        ProductId: [{
          required: true,
          trigger: 'blur',
          message: '请输入商品id',
        }],
        ClassName: [{
          required: true,
          trigger: 'change',
          message: '请选择商品分类',
        }],
        ProductName: [{
          required: true,
          trigger: 'blur',
          message: '请输入商品名称',
        }],
        ProductShortName: [{
          required: true,
          trigger: 'blur',
          message: '请输入商品副标题',
        }],
        ProductNumber: [{
          required: true,
          trigger: 'blur',
          message: '请输入款号',
        }],
        ProductNumberName: [{
          required: true,
          trigger: 'blur',
          message: '请输入款号名称/品名',
        }],
        MarketPrice: [{
          required: true,
          trigger: 'blur',
          message: '请输入市场价格',
        },{
          type: 'number',
          message: '市场价格必须为数字'
        }],
        MemberPrice: [{
          required: true,
          trigger: 'blur',
          message: '请输入会员价格',
        },{
          type: 'number',
          message: '会员价格必须为数字'
        }],
        SpecialPrice: [{
          required: true,
          trigger: 'blur',
          message: '请输入特惠价格',
        },{
          type: 'number',
          message: '特惠价格必须为数字'
        }],
        PinyinShort: [{
          required: true,
          trigger: 'blur',
          message: '请输入拼音简写',
        }],
      },
      updateForm: {
        categoriesPlaceholder: "请选择商品分类"
      },
      loading: false,
      addEditJudgment: false, //商品id编辑不能修改
      judgmentAddEditReset: true, //编辑不显示重置按钮
    }
  },
  created() {
    this.getCommodityTypeSelect()
    this.getCommodityLabelSelect()
    Promise.all([])
      .then(() => {
        if (this.rowid) {
          this.judgmentAddEditReset = false
          this.addEditJudgment = true
          this.getDetail(this.rowid)
        }
      })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.content = filterContent.delHtmlTag(this.ruleForm.content)// 保存内容时去掉 html 标签
          this.ruleForm.tag = this.ruleForm.tag
          this.loading = true
          // 添加
          if (!this.rowid) {
            addCommodityList(this.ruleForm).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$parent.$layer.close(this.layerid)
                this.$parent.$parent.getList() // 重新加载父级数据
                this.$message.success('恭喜你，添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editCommodityList(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$message.success('恭喜你，编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.parentids = []
      this.$refs.ruleForm.resetFields()
    },
    // 详情
    getDetail(rid) {
      const params = {
        ProductId: rid,
        issale: -1,
      }
      getCommodityList(params).then(response => {
        if (response.state.toLowerCase() === 'success') {
          let parentidsArray = []
          const res = response.data.Rows[0];
          this.ruleForm.ProductId = res.F_ProductId //商品id
          this.ruleForm.ClassId = res.F_ClassId //商品类型id
          this.ruleForm.ClassName = res.F_ClassName //商品类型名称
          this.updateForm.categoriesPlaceholder = res.F_ClassName
          this.ruleForm.ProductName = res.F_ProductName //商品名称
          this.ruleForm.ProductShortName = res.F_ProductShortName //副标题
          this.ruleForm.ProductNumber = res.F_ProductNumber //款号
          this.ruleForm.ProductNumberName = res.F_ProductNumberName //款号名称/品名
          this.ruleForm.MarketPrice = res.F_MarketPrice //市场价格
          this.ruleForm.MemberPrice = res.F_MemberPrice //会员价格
          this.ruleForm.SpecialPrice = res.F_SpecialPrice //特惠价格
          this.ruleForm.PinyinShort = res.F_PinyinShort //拼音简写
          this.ruleForm.tag = res.F_Tag //商品标签
          this.ruleForm.Address = res.F_Address //产地
          this.ruleForm.Des = res.F_Des //描述
          this.ruleForm.Factory = res.F_Factory //厂家
          this.ruleForm.Supplier = res.F_Supplier //供应商
          this.ruleForm.OnSaleBegin = res.F_OnSaleBegin //上架时间
          this.ruleForm.OnSaleEnd = res.F_OnSaleEnd //下架时间
          this.ruleForm.IsSale = res.F_IsSale + "" //是否赠品
        }
      })
    },
    getCommodityTypeSelect() {
      this.loading = true
      return new Promise(resolve => {
        const data = {
          pageindex: 1, // int 第几页
          pagesize: 100, // int 每页几条信息
          ParentId: 0,
        }
        getCommodityType(data).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.commodityTypeLists = response.data
          }
        })
        resolve()
      })
    },
    getCommodityLabelSelect() {
      this.loading = true
      return new Promise(resolve => {
        const data = {
          TagName: '', // int 第几页
          CLass: 3, // int 每页几条信息
        }
        getCommodityLabel(data).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.commodityLabelLists = response.data
          }
        })
        resolve()
      })
    },
    commodityTypeChange(item) {
      this.ruleForm.ClassId = this.parentids[this.parentids.length - 1]
      this.ruleForm.ClassName = this.$refs['myCascader'].currentLabels[this.parentids.length - 1]
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form {
  .form_select{
  	width: 100%;
  }
  .form_date {
    width: 100%
  }
}
</style>