<style scoped lang="scss">

</style>
<template>
  <div class="system-dialog-container">
    <el-dialog
        :title="state.dialog.title"
        v-model="state.dialog.isShowDialog"
        width="600px"
        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="130px"
          class="mt5">
        <el-form-item label="设备名称" prop="deviceName" class="wd350">
          <el-input
              v-model="state.ruleForm.deviceName"
              placeholder="设备名称"
              clearable
              class="wd200">
          </el-input>
        </el-form-item>
        <el-form-item label="工位序号" prop="sequence" class="wd350">
          <el-input-number
              v-model="state.ruleForm.sequence"
              placeholder="工位序号"
              :min="1"
              class="wd200">
          </el-input-number>
        </el-form-item>
        <el-form-item label="所在网点" prop="stationId" class="wd350">
          <ext-select
              class="wd200"
              v-model="state.ruleForm.stationId"
              placeholder="所在网点"
              url="washStation/list"
              label-key="stationName"
              value-key="stationId">
          </ext-select>
        </el-form-item>
        <el-form-item label="网点ID" prop="stationId" class="wd350">
          <el-input
              v-model="state.ruleForm.stationId"
              placeholder="网点ID"
              clearable
              readonly
              class="w100">
          </el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state" class="wd350">
          <ext-d-select type="WashDevice.status" v-model="state.ruleForm.status" class="wd200"></ext-d-select>
        </el-form-item>

        <el-form-item label="是否有泡沫" prop="hasFoam" class="wd250">
          <ext-d-select type="WashDevice.foam" v-model="state.ruleForm.hasFoam" class="wd200"></ext-d-select>
        </el-form-item>
        <el-form-item label="是否有水" prop="hasWater" class="wd250">
          <ext-d-select type="WashDevice.water" v-model="state.ruleForm.hasWater" class="wd200"></ext-d-select>
        </el-form-item>

        <el-form-item label="是否支持PA" prop="hasPa" class="wd250">
          <ext-d-select type="yes_no" v-model="state.ruleForm.hasPa" class="wd200"></ext-d-select>
        </el-form-item>

        <el-form-item label="板载温度" prop="temperatureChip" class="wd350">
          <el-input
              v-model="state.ruleForm.temperatureChip"
              placeholder="板载温度传感器的温度值"
              clearable
              class="w100">
          </el-input>
        </el-form-item>

        <el-form-item label="故障原因" prop="faultReason" class="w100">
          <el-input
              v-model="state.ruleForm.faultReason"
              placeholder="故障原因"
              clearable
              type="textarea"
              class="w100">
          </el-input>
        </el-form-item>
        <!--        <el-form-item label="设备主状态机的当前状态(状态可能增加或减少，仅供调试参考。)" prop="fsmState" class="wd350">
                  <el-input
                      v-model="state.ruleForm.fsmState"
                      placeholder="设备主状态机的当前状态(状态可能增加或减少，仅供调试参考。)"
                      clearable
                      class="w100">
                  </el-input>
                </el-form-item>-->
        <el-form-item label="功能" prop="functions" class="w100">
          <el-input
              v-model="state.ruleForm.functions"
              placeholder="功能"
              clearable
              class="w100">
          </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="WashDeviceDialog">
import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {Msg} from "/@/utils/message";
import {$body, $get} from "/@/utils/request";
import u from '/@/utils/u'
import ExtDSelect from "/@/components/form/ExtDSelect.vue";
import ExtSelect from "/@/components/form/ExtSelect.vue";


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

// 定义变量内容
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  ? "washDevice/modify" : "washDevice/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(`washDevice/detail/${id}`).then((res: any) => {
    state.ruleForm = res;
  })
}

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


</script>