<template>
  <a-card :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="name">
        <a-input
          id="productInfoEdit-name"
          :maxLength="30"
          v-model="form.name"
          placeholder="请输入产品名称(最多30个字符)"
          allowClear />
      </a-form-model-item>
      <a-form-model-item label="产品编码" prop="contactName">
        <a-input
          id="productInfoEdit-contactName"
          :maxLength="30"
          v-model="form.contactName"
          placeholder="请输入产品编码(最多30个字符)"
          allowClear />
      </a-form-model-item>
      <a-form-model-item label="原厂编码" prop="contactPhone">
        <a-input
          id="productInfoEdit-contactPhone"
          :maxLength="30"
          v-model="form.contactPhone"
          placeholder="请输入原厂编码(最多30个字符)"
          allowClear />
      </a-form-model-item>
      <a-form-model-item label="基本单位" prop="contactPhone">
        <a-input
          id="productInfoEdit-contactPhone"
          :maxLength="30"
          v-model="form.contactPhone"
          placeholder="请输入基本单位(最多30个字符)"
          allowClear />
      </a-form-model-item>
      <a-form-model-item label="辅助单位" prop="contactPhone">
        <a-input
          id="productInfoEdit-contactPhone"
          :maxLength="30"
          v-model="form.contactPhone"
          placeholder="请输入辅助单位(最多30个字符)"
          allowClear />
      </a-form-model-item>
      <a-form-model-item label="产品品牌" prop="contactPhone">
        <a-select placeholder="请选择" allowClear v-model="form.dataSourceNo" :showSearch="true" option-filter-prop="children" :filter-option="filterOption">
        	<a-select-option v-for="item in brandData" :key="item.salesChannelNo" :value="item.salesChannelNo">{{ item.salesChannelName }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="产品类别" required style="margin: 0;">
        <a-row :gutter="15">
          <!-- 产品类别 -->
          <a-col span="8">
            <a-form-model-item prop="provinceCode">
              <a-select id="productInfoEdit-provinceCode" @change="getCityList" v-model="form.provinceCode" placeholder="一级分类">
                <a-select-option v-for="item in addrProvinceList" :value="item.id" :key="item.id + 'a'">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item prop="cityCode">
              <a-select id="productInfoEdit-cityCode" @change="getAreaList" v-model="form.cityCode" placeholder="二级分类">
                <a-select-option v-for="item in addrCityList" :value="item.id" :key="item.id + 'b'">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item prop="districtCode">
              <a-select id="productInfoEdit-districtCode" @change="areaCharged" v-model="form.districtCode" placeholder="三级分类">
                <a-select-option v-for="item in addrDistrictList" :value="item.id" :key="item.id + 'c'">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model-item>
      <a-form-model-item label="经销批发价" prop="name">
        <a-input-number
          id="productInfoEdit-districtCode"
          v-model="form.goldNum"
          :min="0"
          :max="999999"
          :precision="0"
          style="width: 90%;margin-right: 5px;"
          placeholder="请输入经销批发价(0~999999)"
          allowClear /><span>元</span>
      </a-form-model-item>
      <a-form-model-item label="终端价（原售价）" prop="name">
        <a-input-number
          id="productInfoEdit-districtCode"
          v-model="form.goldNum"
          :min="0"
          :max="999999"
          :precision="0"
          style="width: 90%;margin-right: 5px;"
          placeholder="请输入终端价（原售价）(0~999999)"
          allowClear /><span>元</span>
      </a-form-model-item>
      <a-form-model-item label="车主价" prop="name">
        <a-input-number
          id="productInfoEdit-districtCode"
          v-model="form.goldNum"
          :min="0"
          :max="999999"
          :precision="0"
          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>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
// import { goodsSave, goodsFind } from '@/api/goods'
export default {
  name: 'productInfoEdit',
  components: { STable, VSelect },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 }
      },
      form: {
        name: '', // 供应商名称
        provinceCode: undefined, //  省
        provinceName: '',
        cityCode: undefined, // 市
        cityName: '',
        districtCode: undefined, // 区
        districtName: '',
        address: '', //  详细地址
        contactName: '',  //  联系人
        contactPhone: '',  //  联系电话
        remarks: '',  //  主营内容
        state: 0  //  启禁用
      },
      rules: {
        name: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' }
        ],
        provinceCode: [
          { required: true, message: '请选择省', trigger: 'change' }
        ],
        cityCode: [
          { required: true, message: '请选择市', trigger: 'change' }
        ],
        districtCode: [
          { required: true, message: '请选择区/县', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ],
        contactName: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请输入联系人电话', trigger: 'blur' }
        ],
        state: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      },
      addrProvinceList: [], //  省下拉
      addrCityList: [], //  市下拉
      addrDistrictList: [], //  区下拉
      brandData: [],  //  下拉数据
    }
  },
  computed: {
    isEdit () {
      return this.$route.name === 'productInfoEdit'
    }
  },
  methods: {
    //  获取供应商信息
    getGoodsDetail () {
      // goodsFind({ id: this.$route.params.id }).then(res => {
      //   if (res.status == 200) {
      //     this.form = Object.assign(this.form, res.data)
      //   }
      // })
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = values.form
          form.desc = _this.form.desc
          form.id = this.isEdit ? this.$route.params.id : null
          console.log(form,'-----提交信息')
          // goodsSave(form).then(res => {
          //   if (res.status == 200) {
          //     _this.$message.success(res.message)
          //     setTimeout(() => {
          //       _this.$router.push({ path: '/shop/goods/list' })
          //     }, 1000)
          //   }
          // })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    filterOption(input, option) {
    	return (
    		option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    	)
    },
    //  返回
    goBack () {
      this.$router.push({ path: '/productManagement/productInfo/list' })
    },
    // 获取省列表'
    getProvinceList () {
      getProvince().then(res => {
        if (res.status == 200) {
          this.addrProvinceList = res.data || []
        } else {
          this.addrProvinceList = []
        }
      })
    },
    // 获取城市列表
    getCityList (val) {
      this.addrCityList = []
      this.addrDistrictList = []
      this.form.cityCode = undefined
      this.form.districtCode = undefined
      this.form.address = ''
      this.getCityListRequest(val)
    },
    getCityListRequest (val) {
      getCityByPro({
        id: val
      }).then(res => {
        if (res.status == 200) {
          this.addrCityList = res.data || []
        } else {
          this.addrCityList = []
        }
      })
    },
    // 获取区县列表
    getAreaList (val) {
      this.addrDistrictList = []
      this.form.districtCode = undefined
      this.form.address = ''
      this.getAreaListRequest(val)
    },
    getAreaListRequest (val) {
      getDistrictByCity({
        id: val
      }).then(res => {
        if (res.status == 200) {
          this.addrDistrictList = res.data || []
        } else {
          this.addrDistrictList = []
        }
      })
    },
    // 区县变更
    areaCharged (val) {
      this.form.address = ''
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      // vm.getGoodsType()
      // vm.getPartnerQueryList()
      vm.$refs.ruleForm.resetFields()
      if (vm.isEdit) { //  编辑页
        vm.getGoodsDetail()
      }
    })
  }
}
</script>

<style lang="less">
  .productInfoEdit-table-page-wrapper{
    
  }
</style>
