<template>
  <div class="supplierInfoEdit-wrap">
    <a-page-header :ghost="false" :backIcon="false" @back="handleBack" class="supplierInfoEdit-cont">
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="merchantInfoManagement-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
      </template>
    </a-page-header>
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="supplierInfoEdit-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <!-- 基础信息 -->
        <a-card size="small" :bordered="false" class="supplierInfoEdit-cont">
          <a-collapse :activeKey="['1']">
            <a-collapse-panel key="1" header="基础信息">
              <a-row>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="供应商名称" prop="supplierName">
                    <a-input
                      id="supplierInfoEdit-supplierName"
                      :maxLength="30"
                      v-model="form.supplierName"
                      placeholder="请输入供应商名称(最多30个字符)"
                      allowClear />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="联系人1" prop="contact">
                    <a-input v-model.trim="form.contact" id="supplierInfoEdit-contact" :maxLength="30" allowClear placeholder="请输入联系人1(最多30个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="联系手机1" prop="contactMobile">
                    <a-input v-model.trim="form.contactMobile" id="supplierInfoEdit-contactMobile" :maxLength="11" allowClear placeholder="请输入联系手机1(最多11个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="联系角色1" prop="contactRole">
                    <a-input v-model.trim="form.contactRole" id="supplierInfoEdit-contactRole" :maxLength="30" allowClear placeholder="请输入联系角色1(最多30个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="联系电话" prop="supplierTelephone">
                    <a-input v-model.trim="form.supplierTelephone" id="supplierInfoEdit-supplierTelephone" :maxLength="13" allowClear placeholder="请输入联系电话" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="24" :lg="16" :xl="16">
                  <a-form-model-item label="地址" required style="margin: 0;" :label-col="{span:4}" :wrapper-col="{span:20}">
                    <a-row :gutter="20">
                      <!-- 地址 -->
                      <a-col span="8">
                        <a-form-model-item prop="provinceSn">
                          <a-select id="supplierInfoEdit-provinceSn" allowClear @change="getCityList" v-model="form.provinceSn" placeholder="请选择省">
                            <a-select-option v-for="item in addrProvinceList" :value="item.areaSn" :key="item.areaSn + 'a'">{{ item.name }}</a-select-option>
                          </a-select>
                        </a-form-model-item>
                      </a-col>
                      <a-col span="8">
                        <a-form-model-item prop="citySn">
                          <a-select id="supplierInfoEdit-citySn" allowClear @change="getAreaList" v-model="form.citySn" placeholder="请选择市">
                            <a-select-option v-for="item in addrCityList" :value="item.areaSn" :key="item.areaSn + 'b'">{{ item.name }}</a-select-option>
                          </a-select>
                        </a-form-model-item>
                      </a-col>
                      <a-col span="8">
                        <a-form-model-item prop="districtSn">
                          <a-select id="supplierInfoEdit-districtSn" allowClear @change="areaCharged" v-model="form.districtSn" placeholder="请选择区/县">
                            <a-select-option v-for="item in addrDistrictList" :value="item.areaSn" :key="item.areaSn + 'c'">{{ item.name }}</a-select-option>
                          </a-select>
                        </a-form-model-item>
                      </a-col>
                    </a-row>
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="详细地址" prop="address">
                    <a-input
                      id="supplierInfoEdit-address"
                      :maxLength="60"
                      v-model="form.address"
                      placeholder="请输入详细地址(最多60个字符)"
                      allowClear />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="生产周期(天)" prop="produceCycle">
                    <a-input-number
                      id="supplierInfoEdit-produceCycle"
                      v-model="form.produceCycle"
                      :precision="0"
                      :min="1"
                      :max="999999"
                      placeholder="请输入"
                      style="width: 100%;" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="配送周期(天)" prop="deliveryCycle">
                    <a-input-number
                      id="supplierInfoEdit-deliveryCycle"
                      v-model="form.deliveryCycle"
                      :precision="0"
                      :min="1"
                      :max="999999"
                      placeholder="请输入"
                      style="width: 100%;" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="贷款结算周期(天)" prop="settleCycle">
                    <a-input-number
                      id="supplierInfoEdit-settleCycle"
                      v-model="form.settleCycle"
                      :precision="0"
                      :min="1"
                      :max="999999"
                      placeholder="请输入"
                      style="width: 100%;" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="是否合作" prop="cooperateFlag">
                    <a-radio-group
                      name="radioGroup"
                      v-model="form.cooperateFlag"
                      id="supplierInfoEdit-cooperateFlag" >
                      <a-radio :value="1">是</a-radio>
                      <a-radio :value="0">否</a-radio>
                    </a-radio-group>
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-collapse-panel>
          </a-collapse>
        </a-card>
        <!-- 扩展信息 -->
        <a-card size="small" :bordered="false" class="supplierInfoEdit-cont">
          <a-collapse>
            <a-collapse-panel key="1" header="扩展信息">
              <a-row>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="经营方式" prop="busniessMode" help="（例：国有企业）">
                    <a-input
                      v-model.trim="form.busniessMode"
                      id="supplierInfoEdit-busniessMode"
                      :maxLength="30"
                      allowClear
                      placeholder="请输入经营方式(最多30个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="公司规模" prop="busniessScope" help="（例：100-999人）">
                    <a-input-number
                      id="supplierInfoEdit-busniessScope"
                      v-model="form.busniessScope"
                      :precision="0"
                      :min="1"
                      :max="999999"
                      placeholder="请输入"
                      style="width: 100%;" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="成立时间" prop="buildDate">
                    <a-date-picker
                      style="width:100%"
                      id="supplierInfoEdit-buildDate"
                      format="YYYY-MM-DD"
                      placeholder="请选择日期"
                      :disabledDate="disabledDate"
                      allowClear
                      v-model="form.buildDate"/>
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="银行账户" prop="tradeAccountName" help="（例：张三）">
                    <a-input
                      v-model.trim="form.tradeAccountName"
                      id="supplierInfoEdit-tradeAccountName"
                      :maxLength="30"
                      allowClear
                      placeholder="请输入银行账户(最多30个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="开户行" prop="tradeBankName" help="（例：交通银行）">
                    <a-input
                      v-model.trim="form.tradeBankName"
                      id="supplierInfoEdit-tradeBankName"
                      :maxLength="30"
                      allowClear
                      placeholder="请输入开户行(最多30个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="银行账号" prop="tradeBankAccount" help="（例：1123 5677 1112 22342 1123）">
                    <a-input
                      v-model.trim="form.tradeBankAccount"
                      id="supplierInfoEdit-tradeBankAccount"
                      :maxLength="30"
                      allowClear
                      placeholder="请输入银行账号(最多30个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="联系人2" prop="contact2">
                    <a-input v-model.trim="form.contact2" id="supplierInfoEdit-contact2" :maxLength="30" allowClear placeholder="请输入联系人2(最多30个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="联系手机2" prop="contactMobile2">
                    <a-input v-model.trim="form.contactMobile2" id="supplierInfoEdit-contactMobile2" :maxLength="11" allowClear placeholder="请输入联系手机2(最多11个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="联系角色2" prop="contactRole2">
                    <a-input v-model.trim="form.contactRole2" id="supplierInfoEdit-contactRole2" :maxLength="30" allowClear placeholder="请输入联系角色2(最多30个字符)" />
                  </a-form-model-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="24" :lg="16" :xl="16">
                  <a-form-model-item label="备注" prop="remark" :label-col="{span:4}" :wrapper-col="{span:20}">
                    <a-textarea
                      id="merchantInfoManagementEdit-remark"
                      :maxLength="300"
                      v-model="form.remark"
                      placeholder="请输入备注(最多300字符)"
                      allowClear />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-collapse-panel>
          </a-collapse>
        </a-card>
      </a-form-model>
      <a-affix :offset-bottom="0">
        <div style="text-align: center;width: 100%;background-color: #fff;padding: 12px 0;box-shadow: 0 0 20px #dcdee2;">
          <a-button
            type="primary"
            class="button-primary"
            id="supplierInfoEdit-submit-btn"
            size="large"
            @click="handleSubmit"
            style="padding: 0 60px;">提交</a-button>
        </div>
      </a-affix>
    </a-spin>
  </div>
</template>

<script>
import moment from 'moment'
import { getArea } from '@/api/data'
import { VSelect } from '@/components'
import { supplierDetail, supplierSave } from '@/api/supplier.js'
export default {
  components: { VSelect },
  data () {
    return {
      spinning: false,
      formItemLayout: {
        labelCol: {
          span: 8
        },
        wrapperCol: {
          span: 16
        }
      },
      form: {
        supplierName: '',
        contact: '',
        contactMobile: '',
        contactRole: '',
        supplierTelephone: '',
        provinceSn: undefined,
        citySn: undefined,
        districtSn: undefined,
        address: '',
        produceCycle: '',
        deliveryCycle: '',
        settleCycle: '',
        cooperateFlag: undefined,
        busniessMode: '',
        busniessScope: '',
        buildDate: '',
        tradeAccountName: '',
        tradeBankName: '',
        tradeBankAccount: '',
        contact2: '',
        contactMobile2: '',
        contactRole2: '',
        remark: ''
      },
      rules: {
        supplierName: [
          { required: true, message: '请输入供应商名称', trigger: 'change' }
        ],
        contact: [
          { required: true, message: '请输入联系人1', trigger: 'change' }
        ],
        contactMobile: [
          { required: true, message: '请输入联系手机1', trigger: 'change' },
          { pattern: /^[1][0-9]{10}$/, message: '请输入正确的手机号码!' }
        ],
        provinceSn: [
          { required: true, message: '请选择省', trigger: 'change' }
        ],
        citySn: [
          { required: true, message: '请选择市', trigger: 'change' }
        ],
        districtSn: [
          { required: true, message: '请选择区县', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入详细地址', trigger: 'change' }
        ],
        supplierTelephone: [
          { pattern: /^[0-9]{11,13}$/, message: '请输入正确的电话号码!' }
        ],
        contactMobile2: [
          { pattern: /^[1][0-9]{10}$/, message: '请输入正确的手机号码!' }
        ],
        cooperateFlag: [
          { required: true, message: '请选择是否合作', trigger: 'change' }
        ]
      },
      addrProvinceList: [], //  省下拉
      addrCityList: [], //  市下拉
      addrDistrictList: [], //  区下拉
      dateFormat: 'YYYY-MM-DD'
    }
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    // 详情编辑
    getDetail () {
      supplierDetail({ sn: this.$route.params.id }).then(res => {
        if (res.status == 200) {
          this.form = Object.assign(this.form, res.data)
          if (res.data.buildDate) {
            this.form.buildDate = moment(res.data.buildDate, 'YYYY-MM-DD')
          }
          this.form.cooperateFlag = Number(this.form.cooperateFlag)
          this.getArea('city', this.form.provinceSn)
          this.getArea('district', this.form.citySn)
        }
      })
    },
    // 提交
    handleSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          if (form.buildDate) {
            form.buildDate = moment(form.buildDate).format('YYYY-MM-DD') + ' 00:00:00'
          }
          _this.spinning = true
          supplierSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.handleBack()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    // 返回
    handleBack () {
      this.$router.push({ path: '/supplierManagement/supplierInfo/list', query: { closeLastOldTab: true } })
    },
    // 获取城市列表
    getCityList (val) {
      this.addrCityList = []
      this.addrDistrictList = []
      this.form.citySn = undefined
      this.form.districtSn = undefined
      this.form.address = ''
      if (val) {
        this.getArea('city', val)
        this.form.provinceName = this.addrProvinceList.find(item => item.areaSn == val).name
      } else {
        this.form.provinceName = ''
      }
    },
    // 获取区县列表
    getAreaList (val) {
      this.addrDistrictList = []
      this.form.districtSn = undefined
      this.form.address = ''
      if (val) {
        this.getArea('district', val)
        this.form.cityName = this.addrCityList.find(item => item.areaSn == val).name
      } else {
        this.form.cityName = ''
      }
    },
    // 区县变更
    areaCharged (val) {
      this.form.address = ''
      if (val) {
        this.form.districtName = this.addrDistrictList.find(item => item.areaSn == val).name
      } else {
        this.form.districtName = ''
      }
    },
    //  省/市/区
    getArea (leve, sn) {
      let params
      if (leve == 'province') {
        params = { type: '2' }
      } else {
        params = { parentId: sn, type: leve == 'city' ? '3' : '4' }
      }
      getArea(params).then(res => {
        if (res.status == 200) {
          if (leve == 'province') {
            this.addrProvinceList = res.data || []
          } else if (leve == 'city') {
            this.addrCityList = res.data || []
          } else if (leve == 'district') {
            this.addrDistrictList = res.data || []
          }
        } else {
          if (leve == 'province') {
            this.addrProvinceList = []
          } else if (leve == 'city') {
            this.addrCityList = []
          } else if (leve == 'district') {
            this.addrDistrictList = []
          }
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getArea('province')
      vm.$refs.ruleForm.resetFields()
      if (vm.$route.params.id) { //  编辑页
        vm.getDetail()
      }
    })
  }
}
</script>

<style lang="less">
  .supplierInfoEdit-wrap{
    .supplierInfoEdit-cont{
      margin-bottom: 10px;
    }
    .upload{
      width: 100%!important;
    }
    //  文本编辑器  工具栏样式换行
    .supplierInfoEdit-editor{
      .w-e-toolbar{
        flex-wrap: wrap;
        z-index: 0;
      }
    }
  }
</style>
