<template>
  <div>
    <el-drawer
      class="test"
      :model-value="true"
      @close="() => emit('onClose')"
      with-header="true"
      size="35%"
    >
      <template #header="{ titleId }">
        <p :id="titleId">{{ title }}</p>
      </template>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        size="default"
        status-icon
      >
        <el-form-item label="车间名称" prop="name">
          <el-select v-model="ruleForm.name" placeholder="请选择车间" style="width: 200px">
            <el-option
              v-for="item in workshopList"
              :key="item.workshopCode"
              :label="item.workshopName"
              :value="item.workshopName"
              :disabled="!item.isEnable"
              @click="changeWorkShopCode(item.workshopCode, item.isEnable)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属公司" prop="seniorScene">
          <el-input v-model="ruleForm.seniorScene" disabled />
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="ruleForm.isDisabled"
            :active-value="IS_DISABLED.FALSE"
            :inactive-value="IS_DISABLED.TRUE"
            class="switchUse"
          />
        </el-form-item>
      </el-form>
      <div style="position: absolute; left: 108px; bottom: 67px">
        <el-button v-if="!props.detail.id" @click="resetDraw">重置</el-button>
        <el-button v-if="!props.detail.id" type="primary" @click="addNewType(ruleFormRef)">
          提交
        </el-button>
        <el-button v-if="props.detail.id" type="primary" @click="editedSub(ruleFormRef)">
          提交
        </el-button>
      </div>
    </el-drawer></div
  >
</template>

<script setup lang="ts">
  import { computed, reactive, ref, defineProps, defineEmits, watch } from 'vue';
  import { IS_DISABLED } from '@/types/scene/constant.ts';
  import { type FormInstance, type FormRules } from 'element-plus';
  import { addWorkshop, editWorkshop } from '@/api/scene/scene.ts';
  import { getWorkshopInfoList } from '@/api/workShopData';

  type WorkshopListType = {
    isEnable: boolean;
    workshopCode: string;
    workshopName: string;
  };

  const workshopList = ref<WorkshopListType[]>([]);
  getWorkshopInfoList().then((res) => {
    workshopList.value = res;
  });

  const changeWorkShopCode = (workshopCode: string, isEnable: boolean) => {
    if (isEnable === false) return;
    ruleForm.code = workshopCode;
  };

  const props = defineProps<{
    detail: {
      id?: number;
      code?: string;
      name?: string;
      seniorScene?: string;
      isDisabled?: IS_DISABLED;
      companyId?: number;
    };
  }>();

  //将表单数据传至父组件
  const emit = defineEmits<{
    (e: 'onOk'): unknown;
    (e: 'onClose'): unknown;
  }>();

  //表格中的规则
  const ruleFormRef = ref<FormInstance>();

  const title = computed(() => {
    if (props.detail.id) {
      return '编辑车间';
    }
    return '添加车间';
  });

  interface RuleForm {
    name: string; //名称
    code: string; //车间code
    seniorScene: string; //上级场景
    isDisabled?: IS_DISABLED; //状态
  }
  const ruleForm = reactive<RuleForm>({
    name: '',
    code: '',
    seniorScene: '',
    isDisabled: IS_DISABLED.FALSE,
  });
  const rules = reactive<FormRules>({
    name: [
      { required: true, message: '车间名称不能为空', trigger: 'blur' },
      { min: 1, max: 100, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    seniorScene: [
      { required: true, message: '', trigger: 'blur' },
      { min: 1, max: 100, message: 'Length should be 1 to 100', trigger: 'blur' },
    ],
  });

  //编辑时需要传入子组件的数据
  watch(
    () => props.detail,
    (data) => {
      ruleForm.name = data.name!;
      ruleForm.seniorScene = data.seniorScene!;
      ruleForm.isDisabled = data.isDisabled;
    },
    { immediate: true },
  );

  //添加新车间的提交
  const addNewType = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.validate((valid) => {
      if (!valid) {
        return;
      }
      const newAddData = {
        companyId: props.detail.companyId,
        name: ruleForm.name,
        code: ruleForm.code,
        isDisabled: ruleForm.isDisabled,
      };

      addWorkshop(newAddData)
        .then(() => {
          console.log('成功添加车间');
        })
        .catch((err) => {
          console.log(err);
        })
        .finally(() => {
          emit('onOk');
        });
    });
  };

  //编辑公司时的提交
  const editedSub = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.validate((valid) => {
      if (!valid) {
        return;
      }

      const editNewWorkshopData = {
        id: props.detail.id,
        code: ruleForm.code,
        name: ruleForm.name,
        isDisabled: ruleForm.isDisabled,
      };

      editWorkshop(editNewWorkshopData)
        .then(() => {
          console.log('成功修改车间');
        })
        .catch((err) => {
          console.log(err);
        })
        .finally(() => {
          emit('onOk');
        });
    });
  };

  //重置编辑框
  const resetDraw = () => {
    ruleForm.name = '';
    ruleForm.code = '';
    ruleForm.isDisabled = IS_DISABLED.FALSE;
  };
</script>

<style scoped>
  .tag-select {
    margin-right: 10px;
    margin-bottom: 10px;
    border-radius: 4px;
    border: 1px solid rgba(0, 0, 0, 0.15);
  }
</style>
