<template>
  <div class="authPass-wrap">
    <a-page-header :ghost="false" :backIcon="false" class="authPass-back">
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="authPass-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
      </template>
    </a-page-header>
    <a-spin :spinning="spinning" tip="Loading...">
      <a-card size="small" :bordered="false" class="authPass-table-page-wrapper">
        <a-form-model
          id="authPass-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-row :gutter="15">
            <a-col :span="12">
              <a-form-model-item label="门头照片" prop="storeImage">
                <Upload
                  class="upload"
                  id="noticeEdit-imgPaths"
                  v-model="form.storeImage"
                  ref="storeImage"
                  :fileSize="10"
                  :maxNums="1"
                  @remove="removeImage1"
                  @change="changeImage1"
                  listType="picture-card"></Upload>
                <div class="upload-desc">说明：单张大小小于10Mb，最多1张。</div>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="营业执照" prop="licenseImage">
                <Upload
                  class="upload"
                  id="noticeEdit-imgPaths"
                  v-model="form.licenseImage"
                  ref="licenseImage"
                  :fileSize="10"
                  :maxNums="1"
                  @remove="removeImage2"
                  @change="changeImage2"
                  listType="picture-card"></Upload>
                <div class="upload-desc">说明：单张大小小于10Mb，最多1张。</div>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="门店名称" prop="storeName">
                <a-input id="authPass-storeName" :maxLength="30" v-model.trim="form.storeName" placeholder="请输入门店名称(最多30个字符)" allowClear />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="15">
            <a-col :span="12">
              <a-form-model-item label="客户地址" required style="margin: 0;">
                <a-row :gutter="15">
                  <!-- 客户地址 -->
                  <a-col span="6">
                    <a-form-model-item prop="addrProvince">
                      <a-select id="authPass-addrProvince" allowClear @change="getCityList" v-model="form.addrProvince" 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="6">
                    <a-form-model-item prop="addrCity">
                      <a-select id="authPass-addrCity" allowClear @change="getAreaList" v-model="form.addrCity" 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="6">
                    <a-form-model-item prop="addrDistrict">
                      <a-select id="authPass-addrDistrict" allowClear @change="areaCharged" v-model="form.addrDistrict" 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 :span="12">
              <a-form-model-item label="详细地址" prop="addrDetail">
                <a-textarea id="authPass-addrDetail" :maxLength="100" v-model.trim="form.addrDetail" placeholder="请输入详细地址(最多100个字符)" allowClear />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="15">
            <a-col :span="12">
              <a-form-model-item label="联系人姓名" prop="contactName">
                <a-input
                  id="authPass-contactName"
                  :maxLength="20"
                  v-model.trim="form.contactName"
                  @change="filterEmpty"
                  placeholder="请输入联系人姓名(最多20个字符)"
                  allowClear />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="联系人手机" prop="contactPhone">
                <a-input
                  id="authPass-contactPhone"
                  :maxLength="30"
                  disabled
                  v-model.trim="form.contactPhone"
                  allowClear />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="结算方式" prop="settleType">
                <v-select
                  code="SHELF_SETTLE_TYPE"
                  id="authPass-settleType"
                  v-model="form.settleType"
                  allowClear
                  placeholder="请选择结算方式"
                ></v-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="关联客户" prop="customerSn">
                <custList ref="custList" :isEnabled="true" @change="custChange"></custList>
              </a-form-model-item>
            </a-col>
            <a-col :span="12" v-if="form.settleType == 'CREDIT'">
              <a-form-model-item label="授信额度" prop="creditLimit">
                <a-input-number
                  :min="100"
                  :max="9999999"
                  style="width:100%"
                  :precision="2"
                  v-model="form.creditLimit"
                  placeholder="请输入大于100的数字（最多允许两位小数）"></a-input-number>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;margin-top: 30px;">
            <a-button type="primary" @click="saveForm" size="large" id="authPass-btn-submit" style="padding: 0 60px;">审核通过</a-button>
          </a-form-model-item>
        </a-form-model>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect, Upload } from '@/components'
import { getAreaCgj } from '@/api/data'
import custList from '@/views/common/custList.vue'
import { xprhStoreApplyDetail, xprhStoreApplyAudit } from '@/api/approveStore'
export default {
  name: 'ApproveStoreAuthPass',
  components: { STable, VSelect, custList, Upload },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        storeName: '', // 门店名称
        contactPhone: '', //  联系手机
        contactName: '', //  联系人
        addrProvince: undefined, //  省
        addrProvinceName: '',
        addrCity: undefined, // 市
        addrCityName: '',
        addrDistrict: undefined, // 区
        addrDistrictName: '',
        addrDetail: '', //  详细地址
        storeImage: '', // 门头照片
        licenseImage: '', // 营业执照
        settleType: '', //  结算方式
        customerSn: undefined, //  关联客户
        creditLimit: '', //  授信额度
        delearSn: undefined,
        applySn: undefined,
        auditStatus: 'PASS' // 通过
      },
      rules: {
        storeImage: [{ required: true, message: '请上传门头照片', trigger: 'change' }],
        storeName: [{ required: true, message: '请输入门店名称', trigger: 'change' }],
        addrProvince: [{ required: true, message: '请选择省', trigger: 'change' }],
        addrCity: [{ required: true, message: '请选择市', trigger: 'change' }],
        addrDistrict: [{ required: true, message: '请选择区/县', trigger: 'change' }],
        addrDetail: [{ required: true, message: '请输入详细地址', trigger: 'change' }],
        contactName: [{ required: true, message: '请输入联系人姓名', trigger: 'change' }],
        settleType: [{ required: true, message: '请选择收结算方式', trigger: 'change' }],
        customerSn: [{ required: true, message: '请选择关联客户', trigger: 'change' }],
        creditLimit: [{ required: true, message: '请输入授信额度', trigger: 'change' }]
      },
      addrProvinceList: [], //  省下拉
      addrCityList: [], //  市下拉
      addrDistrictList: [], //  区下拉
      settleStyleList: [] //  收款方式  下拉数据
    }
  },
  methods: {
    filterEmpty () {
      let str = this.form.storeName
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.storeName = str
    },
    // 关联客户
    custChange (obj, row) {
      this.form.customerSn = row ? row.customerSn : undefined
    },
    //  图片上传
    changeImage1 (file) {
      this.form.storeImage = file
    },
    removeImage1 () {
      this.form.storeImage = ''
      this.$refs.storeImage.setFileList('')
    },
    //  图片上传
    changeImage2 (file) {
      this.form.licenseImage = file
    },
    removeImage2 () {
      this.form.licenseImage = ''
      this.$refs.licenseImage.setFileList('')
    },
    //  获取客户信息
    getDetail () {
      xprhStoreApplyDetail({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          if (res.data.addrProvince) { this.getArea('city', res.data.addrProvince) }
          if (res.data.addrDistrict) { this.getArea('district', res.data.addrCity) }
          this.form = Object.assign(this.form, res.data)
          this.$refs.storeImage.setFileList(res.data.storeImage)
          this.$refs.licenseImage.setFileList(res.data.licenseImage)
          this.form.auditStatus = 'PASS' // 通过
        } else {
          this.$refs.ruleForm.resetFields()
        }
      })
    },
    //  保存
    saveForm () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$confirm({
            title: '操作提示',
            content: <div><div>审核通过后，即可成为认证门店,</div><div>确认审核通过吗？</div></div>,
            centered: true,
            closable: true,
            onOk () {
              _this.handleSubmit()
            }
          })
        } else {
          return false
        }
      })
    },
    handleSubmit (e) {
      const _this = this
      _this.spinning = true
      xprhStoreApplyAudit(_this.form).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.handleBack()
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    },
    //  返回列表
    handleBack () {
      this.$router.push({ name: 'approveStoreList' })
    },
    // 获取城市列表
    getCityList (val) {
      this.addrCityList = []
      this.addrDistrictList = []
      this.form.addrCity = undefined
      this.form.addrDistrict = undefined
      if (val) {
        this.getArea('city', val)
        this.form.addrProvinceName = this.addrProvinceList.find(item => item.areaSn == val).name
      } else {
        this.form.addrProvinceName = ''
      }
    },
    // 获取区县列表
    getAreaList (val) {
      this.addrDistrictList = []
      this.form.addrDistrict = undefined
      if (val) {
        this.getArea('district', val)
        this.form.addrCityName = this.addrCityList.find(item => item.areaSn == val).name
      } else {
        this.form.addrCityName = ''
      }
    },
    // 区县变更
    areaCharged (val) {
      if (val) {
        this.form.addrDistrictName = this.addrDistrictList.find(item => item.areaSn == val).name
      } else {
        this.form.addrDistrictName = ''
      }
    },
    //  省/市/区
    getArea (leve, sn) {
      let params
      if (leve == 'province') {
        params = { type: '2' }
      } else {
        params = { parentId: sn, type: leve == 'city' ? '3' : '4' }
      }
      getAreaCgj(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 = []
          }
        }
      })
    },
    // 获取基础数据
    getBaseData () {
      this.getArea('province') //  省市区
      this.$refs.ruleForm.resetFields()
    },
    initPage () {
      this.getBaseData()
      //  编辑页
      if (this.$route.params.sn) {
        this.getDetail()
      }
    }
  },
  mounted () {
    console.log('mounted')
    this.initPage()
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      console.log('beforeRouteEnter')
    })
  }
}
</script>

<style lang="less">
.authPass-wrap {
  height: 100%;
  >.ant-spin-nested-loading{
    height: calc(100% - 52px);
	  overflow-y: auto;
    .ant-spin-container{
      height: 100%;
    }
    .upload-desc {
      color:#999;
      clear:both;
    }
  }
  .ant-form-item-children{
    display:block;
  }
	.authPass-back {
		margin-bottom: 10px;
	}
  .authPass-table-page-wrapper{
    height: 100%;
  }
}
</style>
