<template>
  <div class="supplierInfoEdit-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" @back="handleBack" class="supplierInfoEdit-cont">
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="supplierInfoEdit-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
        </template>
      </a-page-header>
      <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.trim="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 :id="'supplierInfoEdit-provinceSn'+item.areaSn" 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 :id="'supplierInfoEdit-citySn'+item.areaSn" 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 :id="'supplierInfoEdit-districtSn'+item.areaSn" 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.trim="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="99999999"
                      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="99999999"
                      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="99999999"
                      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-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
                  <a-form-model-item label="ERP编码">
                    <a-input id="supplierInfoEdit-erpcode" v-model.trim="form.kdSupplierCode" :maxLength="50" allowClear placeholder="请输入ERP编码" />
                  </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="99999999"
                      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="supplierInfoEdit-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-spin>
    <div class="affix-cont">
      <a-button
        type="primary"
        class="button-primary"
        :disabled="spinning"
        id="supplierInfoEdit-submit-btn"
        size="large"
        @click="handleSubmit"
        style="padding: 0 60px;">提交</a-button>
    </div>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
// 组件
import { VSelect } from '@/components'
// 接口
import { getArea } from '@/api/data'
import { supplierDetail, supplierSave } from '@/api/supplier.js'
export default {
  name: 'SupplierInfoEdit',
  mixins: [commonMixin],
  components: { VSelect },
  data () {
    return {
      spinning: false,
      // 表单 label布局
      formItemLayout: {
        labelCol: {
          span: 8
        },
        wrapperCol: {
          span: 16
        }
      },
      form: {
        supplierName: '', // 供应商名称
        contact: '', // 联系人1
        contactMobile: '', // 联系人1手机
        contactRole: '', // 联系人1角色
        supplierTelephone: '', // 供应商电话
        provinceSn: undefined, // 省
        citySn: undefined, // 市
        districtSn: undefined, // 区
        address: '', // 详细地址
        produceCycle: '', // 生产周期(天)
        deliveryCycle: '', // 交货周期(天)
        settleCycle: '', // 结算周期(天)
        cooperateFlag: undefined, // 是否合作
        kdSupplierCode: '', // 快递供应商编码
        busniessMode: '', // 经营模式
        busniessScope: '', // 经营范围
        buildDate: '', // 成立日期
        tradeAccountName: '', // 贸易账号名称
        tradeBankName: '', // 贸易账号开户行
        tradeBankAccount: '', // 贸易账号账号
        contact2: '', // 联系人2
        contactMobile2: '', // 联系人2手机
        contactRole2: '', // 联系人2角色
        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({ name: 'supplierInfoList', query: { closeLastOldTab: true } })
    },
    // 获取城市列表
    getCityList (val) {
      this.addrCityList = []
      this.addrDistrictList = []
      this.form.citySn = undefined
      this.form.districtSn = undefined
      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
      if (val) {
        this.getArea('district', val)
        this.form.cityName = this.addrCityList.find(item => item.areaSn == val).name
      } else {
        this.form.cityName = ''
      }
    },
    // 区县变更
    areaCharged (val) {
      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 = []
          }
        }
      })
    },
    // 初始化页面
    pageInit () {
      this.getArea('province')
      this.$refs.ruleForm.resetFields()
      if (this.$route.params.id) { //  编辑页
        this.getDetail()
      }
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .supplierInfoEdit-wrap{
    position: relative;
    height: 100%;
    padding-bottom: 51px;
    box-sizing: border-box;
    >.ant-spin-nested-loading{
      overflow-y: scroll;
      height: 100%;
    }
    .supplierInfoEdit-cont{
      margin-bottom: 10px;
    }
    .upload{
      width: 100%!important;
    }
    //  文本编辑器  工具栏样式换行
    .supplierInfoEdit-editor{
      .w-e-toolbar{
        flex-wrap: wrap;
        z-index: 0;
      }
    }
  }
</style>
