<style scoped lang="scss">

</style>
<template>
  <div class="system-dialog-container">
    <el-dialog
        :title="state.dialog.title"
        v-model="state.dialog.isShowDialog"
        width="820px"
        draggable
        destroy-on-close
        :close-on-click-modal="false"
        align-center>
      <el-form
          inline
          :model="state.ruleForm"
          :rules="rules"
          label-position="top"
          ref="formRef"
          size="default"
          label-width="100px"
          class="mt5">
        <el-form-item label="地址" prop="address">
          <el-input
              v-model="state.ruleForm.address"
              placeholder="地址"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="营业时间描述" prop="businessHours">
          <el-input
              v-model="state.ruleForm.businessHours"
              placeholder="营业时间描述"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="公司id" prop="companyId">
          <el-input
              v-model="state.ruleForm.companyId"
              placeholder="公司id"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="站点坐标" prop="location">
          <el-input
              v-model="state.ruleForm.location"
              placeholder="站点坐标"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="停车费描述：eg:洗车费用满10元减免1小时停车费" prop="parkingFee">
          <el-input
              v-model="state.ruleForm.parkingFee"
              placeholder="停车费描述：eg:洗车费用满10元减免1小时停车费"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="工位数量" prop="parkingNum">
          <el-input
              v-model="state.ruleForm.parkingNum"
              placeholder="工位数量"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="站点照片" prop="pictures">
          <el-input
              v-model="state.ruleForm.pictures"
              placeholder="站点照片"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
              v-model="state.ruleForm.remark"
              placeholder="备注"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="服务电话" prop="serviceTel">
          <el-input
              v-model="state.ruleForm.serviceTel"
              placeholder="服务电话"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="站点引导" prop="siteGuide">
          <el-input
              v-model="state.ruleForm.siteGuide"
              placeholder="站点引导"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="站点id" prop="stationId">
          <el-input
              v-model="state.ruleForm.stationId"
              placeholder="站点id"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="站点名称" prop="stationName">
          <el-input
              v-model="state.ruleForm.stationName"
              placeholder="站点名称"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="站点状态：Unknown：未知，Normal：正常运营，Offline：关闭下线，Repair：维护中" prop="stationStatus">
          <el-input
              v-model="state.ruleForm.stationStatus"
              placeholder="站点状态：Unknown：未知，Normal：正常运营，Offline：关闭下线，Repair：维护中"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="站点电话" prop="stationTel">
          <el-input
              v-model="state.ruleForm.stationTel"
              placeholder="站点电话"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="站点类型：Public：公共站 2：Private：内部站（不对外开放）" prop="stationType">
          <el-input
              v-model="state.ruleForm.stationType"
              placeholder="站点类型：Public：公共站 2：Private：内部站（不对外开放）"
              clearable
              class="wd350">
          </el-input>
        </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="WashStationDialog">
import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {Msg} from "/@/utils/message";
import {$body, $get} from "/@/utils/request";
import u from '/@/utils/u'

// 引入组件

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

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


// 打开弹窗
const open = (action: string = 'add', row: any) => {
  state.dialog.title = u.dialog.actions[action].title + "『洗车站点表』"
  state.dialog.submitTxt = u.dialog.actions[action].btn + "『洗车站点表』"
  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 ? "washStation/modify" : "washStation/add"
      $body(url, state.ruleForm).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(`washStation/detail/${id}`).then((res: any) => {
    state.ruleForm = res;
  })
}

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


</script>