<template>
  <a-modal
    centered
    class="updateCooperate-modal"
    :footer="null"
    :maskClosable="false"
    :title="title"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          id="updateCooperate-form"
        >
          <a-form-model-item label="商户名称">
            {{ form.dealerName }}
          </a-form-model-item>
          <a-form-model-item label="合作状态" prop="cooperateFlag">
            <v-select code="DEALER_COOPERATE_FLAG" id="updateCooperate-cooperateFlag" v-model="form.cooperateFlag" allowClear placeholder="请选择合作状态"></v-select>
          </a-form-model-item>
          <a-form-model-item label="生效时间" prop="cooperateEffectiveTime">
            <a-date-picker
              style="width:100%"
              id="updateCooperate-cooperateEffectiveTime"
              v-model="form.cooperateEffectiveTime"
              :format="dateFormat"
              placeholder="请选择日期" />
          </a-form-model-item>
          <div style="display: flex;justify-content: center;padding: 30px 0;">
            <a-button id="updateCooperate-cancel-btn" @click="cancel">取消</a-button>
            <a-button id="updateCooperate-submit-btn" style="margin-left: 15px;" type="primary" @click="onSubmit">确定</a-button>
          </div>
        </a-form-model>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
// 组件
import { VSelect } from '@/components'
// 接口
import { updateCooperate } from '@/api/dealer'
export default {
  name: 'UpdateCooperate',
  mixins: [commonMixin],
  components: { VSelect },
  props: {
    openUcModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openUcModal, //  是否打开弹框
      spinning: false,
      title: '合作设置', // 表格标题
      dateFormat: 'YYYY-MM-DD', // 日期格式
      labelCol: { span: 5 }, // form表单label布局
      wrapperCol: { span: 16 },
      // 提交数据
      form: {
        dealerName: '', // 商户名称
        cooperateFlag: undefined, // 合作状态
        cooperateEffectiveTime: null// 生效时间
      },
      // 表单验证规则
      rules: {
        cooperateFlag: [
          { required: true, message: '请选择合作状态', trigger: 'change' }
        ],
        cooperateEffectiveTime: [
          { required: true, message: '请选择生效时间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    // 赋值
    setData (row) {
      this.form.dealerSn = row.dealerSn
      this.form.dealerName = row.dealerName
      this.form.cooperateFlag = row.cooperateFlag
      this.form.cooperateEffectiveTime = row.cooperateEffectiveTime
    },
    // 确定提交
    onSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(_this.form))
          params.cooperateEffectiveTime = moment(params.cooperateEffectiveTime).format('YYYY-MM-DD') + ' 00:00:00'
          _this.spinning = true
          updateCooperate(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    // 取消
    cancel () {
      this.$emit('close')
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openUcModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>

<style lang="less">
  .merchantInfoManagement-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
  }
</style>
