<template>
  <a-modal
    centered
    class="userEdit-modal"
    :footer="null"
    :maskClosable="false"
    title="绑定设备"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <a-form-model
      id="userEdit-form"
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="formItemLayout.labelCol"
      :wrapper-col="formItemLayout.wrapperCol">
      <a-form-model-item label="设备编码" prop="deviceCode">
        <a-input v-model="form.deviceCode" placeholder="请输入设备编码" />
      </a-form-model-item>
      <a-form-model-item label="设备名称" prop="deviceName">
        <a-input v-model="form.deviceName" placeholder="请输入设备名称" />
      </a-form-model-item>
      <a-form-model-item label="绑定经销商" prop="dealerSn">
        <storeList ref="storeList" @change="custChange"></storeList>
      </a-form-model-item>
      <a-form-model-item label="是否启用" prop="enabledFlag">
        <a-radio-group default-value="0" button-style="solid" v-model="form.enabledFlag">
          <a-radio-button value="1">
            是
          </a-radio-button>
          <a-radio-button value="0">
            否
          </a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item :label-col="{span: 0}" :wrapper-col="{span: 24}" style="text-align: center;">
        <a-button :style="{ marginRight: '8px' }" @click="isShow=false">取消</a-button>
        <a-button type="primary" @click="handleSubmit()" :style="{ marginLeft: '8px' }">确定</a-button>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { VSelect } from '@/components'
import storeList from '@/views/common/storeList.vue'
import { handDeviceSave, handDeviceDetail, handDeviceEdit } from '@/api/handDevice'
export default {
  components: { VSelect, storeList },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        dealerSn: undefined, // 客户SN
        deviceCode: '', // 设备编码
        deviceName: '', // 设备名称
        enabledFlag: '0'//  启用标记
      },
      rules: {
        deviceCode: [ { required: true, message: '请输入设备编码', trigger: 'blur' } ],
        deviceName: [ { required: true, message: '请输入设备名称', trigger: 'blur' } ],
        dealerSn: [ { required: true, message: '请选择绑定经销商', trigger: 'change' } ],
        enabledFlag: [ { required: true, message: '请选择是否启用', trigger: 'change' } ]
      }
    }
  },
  methods: {
    custChange (val) {
      this.form.dealerSn = val.key
    },
    // 保存
    handleSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(_this.form))
          if (!_this.itemId) {
            handDeviceSave(params).then(res => {
              if (res.status == 200) {
                _this.$message.success(res.message)
                _this.$emit('confirm', _this.form)
                _this.isShow = false
              }
            })
          } else {
            params.id = _this.itemId
            handDeviceEdit(params).then(res => {
              if (res.status == 200) {
                _this.$message.success(res.message)
                _this.$emit('confirm', _this.form)
                _this.isShow = false
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取详情
    getDetail () {
      handDeviceDetail({ id: this.itemId }).then(res => {
        if (res.status == 200) {
          this.form.dealerSn = res.data.dealerSn
          this.form.deviceCode = res.data.deviceCode
          this.form.deviceName = res.data.deviceName
          this.form.enabledFlag = res.data.enabledFlag
          this.$refs.storeList.getInfo({ key: res.data.dealerSn, name: res.data.dealer.dealerName })
        } else {
          this.$refs.ruleForm.resetFields()
        }
      })
    },
    // 重置数据
    reset () {
      this.form = {
        dealerSn: undefined, // 客户SN
        deviceCode: '', // 设备编码
        deviceName: '', // 设备名称
        enabledFlag: '0'//  启用标记
      }
      this.$refs.storeList.resetForm()
      this.$refs.ruleForm.resetFields()
    },
    pageInit (data) {
      this.getDetail()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.reset()
      }
    }
  }
}
</script>

<style>
</style>
