<template>
  <a-card size="small" :bordered="false" class="productInfoEdit-table-page-wrapper">
    <a-form-model
      id="productInfoEdit-form"
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="formItemLayout.labelCol"
      :wrapper-col="formItemLayout.wrapperCol"
    >
      <a-form-model-item label="产品名称" prop="productName">
        <a-input
          id="productInfoEdit-productName"
          :maxLength="30"
          v-model="form.productName"
          placeholder="请输入产品名称(最多30个字符)"
          allowClear />
      </a-form-model-item>
      <a-form-model-item label="产品编码" prop="productCode">
        <a-input
          id="productInfoEdit-productCode"
          :maxLength="30"
          :disabled="!!$route.params.id"
          v-model="form.productCode"
          placeholder="请输入产品编码(最多30个字符)"
          allowClear />
      </a-form-model-item>
      <a-form-model-item label="原厂编码" prop="origCode">
        <a-input
          id="productInfoEdit-origCode"
          :maxLength="30"
          v-model="form.origCode"
          placeholder="请输入原厂编码(最多30个字符)"
          allowClear />
      </a-form-model-item>
      <a-form-model-item label="基本单位" prop="unit">
        <a-input
          id="productInfoEdit-unit"
          :maxLength="30"
          v-model="form.unit"
          placeholder="请输入基本单位(最多30个字符)"
          allowClear />
      </a-form-model-item>
      <a-form-model-item label="产品品牌" prop="productBrandSn">
        <a-select id="productInfoEdit-productBrand" placeholder="请输入名称或拼音查询，如果没有请点击 '+' 新增" :disabled="!!$route.params.id" allowClear v-model="form.productBrandSn" :showSearch="true" option-filter-prop="children" :filter-option="filterOption" style="width: 90%;">
        	<a-select-option v-for="item in productBrandList" :key="item.productBrandSn" :value="item.productBrandSn" :disabled="item.enabledFlag==0">{{ item.productBrandName }}</a-select-option>
        </a-select>
        <a-button @click="openModal=true" icon="plus" size="small" id="productInfoEdit-add-btn" style="margin-left: 5px;"></a-button>
      </a-form-model-item>
      <a-form-model-item label="产品分类" prop="productType">
        <a-cascader :disabled="!!$route.params.id" @change="changeProductType" :options="productTypeList" :fieldNames="{ label: 'productTypeName', value: 'productTypeSn', children: 'children' }" id="productInfoEdit-productType" placeholder="请选择产品分类" allowClear v-model="form.productType" />
      </a-form-model-item>
      <a-form-model-item label="经销批发价" prop="wholesalePrice">
        <a-input-number
          id="productInfoEdit-wholesalePrice"
          v-model="form.wholesalePrice"
          :min="0"
          :max="999999"
          :precision="2"
          style="width: 90%;margin-right: 5px;"
          placeholder="请输入经销批发价(0~999999)"
          allowClear /><span>元</span>
      </a-form-model-item>
      <a-form-model-item label="终端价" prop="terminalPrice">
        <a-input-number
          id="productInfoEdit-terminalPrice"
          v-model="form.terminalPrice"
          :min="0"
          :max="999999"
          :precision="2"
          style="width: 90%;margin-right: 5px;"
          placeholder="请输入终端价(0~999999)"
          allowClear /><span>元</span>
      </a-form-model-item>
      <a-form-model-item label="车主价" prop="carOwnerPrice">
        <a-input-number
          id="productInfoEdit-carOwnerPrice"
          v-model="form.carOwnerPrice"
          :min="0"
          :max="999999"
          :precision="2"
          style="width: 90%;margin-right: 5px;"
          placeholder="请输入车主价(0~999999)"
          allowClear /><span>元</span>
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;">
        <a-button type="primary" @click="handleSubmit" id="productInfoEdit-btn-submit">保存</a-button>
        <a-button @click="goBack" style="margin-left: 15px" id="productInfoEdit-btn-back">返回列表</a-button>
      </a-form-model-item>
    </a-form-model>
    <!-- 新增/编辑产品品牌 -->
    <product-brand-edit-modal :openModal="openModal" @ok="handleOk" @close="openModal=false" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import productBrandEditModal from '../productBrand/editModal.vue'
import { dealerProductBrandQuery } from '@/api/dealerProductBrand'
import { dealerProductTypeQuery } from '@/api/dealerProductType'
import { dealerProductSave, dealerProductDetail } from '@/api/dealerProduct'
export default {
  name: 'productInfoEdit',
  components: { STable, VSelect, productBrandEditModal },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 }
      },
      form: {
        productName: '', // 产品名称
        productCode: '',  //  产品编码
        origCode: '',  //  原厂编码
        unit: '',  //  基本单位
        productBrandSn: undefined,  //  产品品牌
        productTypeSn3: '',  //  产品三级分类
        productType: [], //  产品分类
        wholesalePrice: '',  //  经销价
        terminalPrice: '',  //  终端价
        carOwnerPrice: '',  //  车主价
      },
      rules: {
        productName: [
          { required: true, message: '请输入产品名称', trigger: 'blur' }
        ],
        productCode: [
          { required: true, message: '请输入产品编码', trigger: 'blur' }
        ],
        productBrandSn: [
          { required: true, message: '请选择产品品牌', trigger: 'change' }
        ],
        productType: [
          { required: true, message: '请选择产品分类', trigger: 'change' }
        ],
      },
      productBrandList: [],  //  品牌下拉数据
      productTypeList: [],  //  分类下拉数据
      openModal: false,  //  新增编辑  弹框
    }
  },
  methods: {
    //  获取产品信息
    getGoodsDetail () {
      dealerProductDetail({ id: this.$route.params.id }).then(res => {
        if (res.status == 200) {
          this.form = Object.assign(this.form, res.data)
          const productTypeSn1 = res.data.productTypeSn1 ? res.data.productTypeSn1 : ''
          const productTypeSn2 = res.data.productTypeSn2 ? res.data.productTypeSn2 : ''
          const productTypeSn3 = res.data.productTypeSn3 ? res.data.productTypeSn3 : ''
          this.form.productType = [productTypeSn1, productTypeSn2, productTypeSn3]
        }else{
          this.$refs.ruleForm.resetFields()
        }
      })
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          form.id = _this.$route.params.id ? _this.$route.params.id : undefined
          delete form.productType
          dealerProductSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.$router.push({ path: '/productManagement/productInfo/list' })
              }, 1000)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  产品分类  change
    changeProductType(val, opt){
      this.form.productTypeSn3 = val[2] ? val[2] : ''
    },
    //  新增品牌  成功
    handleOk(){
      //  获取品牌数据列表，并赋值
      this.getProductBrand('val')
    },
    filterOption(input, option) {
    	return (
    		option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    	)
    },
    //  返回
    goBack () {
      this.$router.push({ path: '/productManagement/productInfo/list' })
    },
    //  产品品牌  列表
    getProductBrand(val){
      dealerProductBrandQuery({}).then(res => {
        if(res.status == 200){
          this.productBrandList = res.data
          if(val){  //  需将新增加的品牌回填到产品品牌的地方，即不需要用户再选一下
            this.form.productBrandSn = this.productBrandList[0].productBrandSn
          }
        }else{
          this.productBrandList = []
        }
      })
    },
    //  产品分类  列表
    getProductType(){
      dealerProductTypeQuery({}).then(res => {
        if(res.status == 200){
          this.productTypeList = res.data
        }else{
          this.productTypeList = []
        }
      })
    },
  },
  mounted(){  // 不使用beforeRouteEnter：由于页面路由动态id导致页面表现方式不同但路由相同，则会出现数据不重新请求问题
    this.getProductBrand()
    this.getProductType()
    this.$refs.ruleForm.resetFields()
    if (this.$route.params.id) { //  编辑页
      this.getGoodsDetail()
    }
  }
}
</script>

<style lang="less"></style>
