<style scoped lang="scss">

</style>
<template>
  <div class="system-dialog-container">
    <el-dialog
        :title="state.dialog.title"
        v-model="state.dialog.isShowDialog"
        width="420px"
        draggable
        destroy-on-close
        :close-on-click-modal="false"
        align-center>
      <el-form
          inline
          :model="state.ruleForm"
          :rules="state.rules"
          label-position="top"
          ref="formRef"
          size="default"
          label-width="100px"
          class="mt5">
        <el-form-item label="站点id" prop="stationId" class="wd300">
          <el-input
              v-model="state.ruleForm.stationId"
              placeholder="站点id"
              clearable
              readonly
              class="w100">
          </el-input>
        </el-form-item>
        <el-form-item label="充电桩枪口编号" prop="connectorId" class="wd300">
          <el-input
              v-model="state.ruleForm.connectorId"
              placeholder="充电桩枪口编号"
              clearable
              readonly
              class="w100">
          </el-input>
        </el-form-item>
        <el-form-item label="绑定平台" prop="platformName" class="wd300">

          <ext-select
              v-model="state.relation.platformName"
              placeholder="平台"
              clearable
              url="platform/list"
              urlMethod="get"
              data-key="list"
              label-key="platformName"
              value-key="platformName"
              class="w100 mr10">
          </ext-select>
        </el-form-item>

      </el-form>

      <template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button :loading="state.btnLoading" type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="EquipmentInfoDialog">
import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {Msg} from "/@/utils/message";
import {$body, $get} from "/@/utils/request";
import u from '/@/utils/u'
import ExtSelect from "/@/components/form/ExtSelect.vue";

// 引入组件

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const formRef = ref();
//定义初始变量，重置使用
const initState = () => ({
  ruleForm: {
    id: 0
  },
  btnLoading: false,
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
  rules: {},
  relation: {}
})

// 定义变量内容
const state = reactive(initState());


// 打开弹窗
const open = (action: string = 'add', row: any) => {
  state.dialog.title = "绑定平台"
  state.dialog.submitTxt =  "确定"
  state.dialog.isShowDialog = true;
  if (action !== 'add') {
    loadData(row.id);
  } else {
    state.ruleForm = Object.assign(state.ruleForm, row);
  }
};
// 关闭弹窗
const onClose = () => {
  state.dialog.isShowDialog = false;
  Object.assign(state, initState())
};
// 取消
const onCancel = () => {
  onClose();
};
// 提交
const onSubmit = () => {
  formRef.value.validate((v: boolean) => {
    if (v) {
      state.btnLoading = true;
      const url = state.ruleForm.id > 0 ? "equipment-relation/bind" : "equipmentInfo/add"
      let data ={
        equipmentId:state.ruleForm.equipmentId,
        connectorId:state.ruleForm.connectorId,
        platformName:state.relation.platformName
      }
      $body(url, data).then(() => {
        state.btnLoading = false;
        Msg.message('操作成功');
        console.log('submit!')
        onClose();
        emit('refresh');
      })
    } else {
      state.btnLoading = false;
      Msg.message('请先完整填写表单', 'error');
    }
  })
};

const handleFormChange = (formData: any) => {
  console.log(formData)
}

// 初始化数据
const loadData = (id: number) => {
  $get(`connector/detail/${id}`).then((res: any) => {
    state.ruleForm = res;
    loadEquipmentRelation();
  })
}

const loadEquipmentRelation = () => {
  $get(`equipment-relation/detail/${state.ruleForm.connectorId}`).then((res: any) => {
    state.relation = res;
  })
}

// 暴露变量
defineExpose({
  open
});


</script>
