<template>
  <div>
    <el-drawer
      ref="companyDrawerRef"
      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="90px"
        class="demo-ruleForm"
        size="default"
        status-icon
      >
        <el-form-item label="公司名称" prop="name">
          <el-input
            v-model="ruleForm.name"
            style="width: 200px"
            disabled
            placeholder="上海飞机制造有限公司"
          />
        </el-form-item>
        <el-form-item label="所在省市" prop="regionCode" required>
          <el-select v-model="ruleForm.regionCode" placeholder="请选择省市" style="width: 200px">
            <el-option
              v-for="item in provinceList"
              :key="item.regionCode"
              :label="item.regionName"
              :value="item.regionCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="坐标经度" prop="longitude"
          ><el-input v-model="ruleForm.longitude" type="number" style="width: 200px"></el-input
        ></el-form-item>
        <el-form-item label="坐标纬度" prop="latitude"
          ><el-input v-model="ruleForm.latitude" type="number" style="width: 200px"></el-input
        ></el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="ruleForm.remark" style="width: 200px" />
        </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="resetDrawCom">重置</el-button>
        <el-button type="primary" v-if="!props.detail.id" @click="newCompanyAdd"> 提交 </el-button>
        <el-button type="primary" v-if="props.detail.id" @click="editedSub(ruleFormRef)">
          提交
        </el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
  import { computed, reactive, ref, defineProps, defineEmits, watch } from 'vue';
  import { addCompany, editCompany } from '@/api/scene/scene.ts';
  // import { IS_DISABLED } from '@/types/scene/constant.ts';
  import type { FormInstance, FormRules } from 'element-plus';
  import { getProvinceList } from '@/api/system/region';
  import { useRequest } from 'vue-hooks-plus';

  const props = defineProps<{
    detail: {
      id?: number; // 传不传id关系到是不是修改
      name?: string;
      // isDisabled?: IS_DISABLED;
      remark?: string;
      longitude?: number; //经度
      latitude?: number; //纬度
      regionCode?: string;
      companyId?: number;
    };
  }>();

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

  const emit = defineEmits<{
    (e: 'onOk'): unknown;
    (e: 'onClose'): unknown;
  }>();

  //表格中的规则
  const ruleFormRef = ref<FormInstance>();
  //匹配规则
  // const pattern = /经度\s*[:：]\s*([0-9.]+)\s*[;；]\s*纬度\s*[:：]\s*([0-9.]+)/;

  interface RuleForm {
    name: string; //名称
    regionCode?: string; //省市
    remark?: string; //备注
    longitude?: number; //经度
    latitude?: number; //纬度
    // isDisabled?: IS_DISABLED;
  }

  const ruleForm = reactive<RuleForm>({
    name: '上海飞机制造有限公司',
    // isDisabled: IS_DISABLED.FALSE,
    remark: '',
    longitude: undefined,
    latitude: undefined,
    regionCode: '',
  });
  const rules = reactive<FormRules>({
    // name: [
    //   { required: true, message: '公司名称不能为空', trigger: 'blur' },
    //   { min: 1, max: 100, message: '长度在1-100之间', trigger: 'blur' },
    // ],
    regionCode: [{ required: true, message: '省市不能为空', trigger: 'blur' }],
  });

  const { data: provinceList } = useRequest(getProvinceList);

  //编辑时需要传入子组件的数据
  watch(
    () => props.detail,
    (data) => {
      // ruleForm.name = data.name!; //上飞定制不可变
      // ruleForm.isDisabled = data.isDisabled;
      ruleForm.remark = data.remark;
      ruleForm.latitude = data.latitude;
      ruleForm.longitude = data.longitude;
      ruleForm.regionCode = data.regionCode;
    },
    { immediate: true },
  );

  //添加新公司的提交
  const newCompanyAdd = () => {
    if (!ruleFormRef.value) return;
    ruleFormRef.value.validate((valid) => {
      if (!valid) {
        return;
      }

      const newComData = {
        name: ruleForm.name,
        // isDisabled: ruleForm.isDisabled,
        remark: ruleForm.remark,
        longitude: ruleForm.longitude,
        latitude: ruleForm.latitude,
        regionCode: ruleForm.regionCode,
      };

      //提交数据并重置关闭el-draw
      addCompany(newComData)
        .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 editNewCom = {
        id: props.detail.id,
        name: ruleForm.name,
        // isDisabled: ruleForm.isDisabled,
        remark: ruleForm.remark,
        longitude: ruleForm.longitude,
        latitude: ruleForm.latitude,
        regionCode: ruleForm.regionCode,
      };
      // 编辑上传
      editCompany(editNewCom)
        .then(() => {
          console.log('编辑成功');
        })
        .finally(() => {
          emit('onOk');
        });
    });
  };

  //重置编辑框
  const resetDrawCom = () => {
    ruleForm.name = '';
    // ruleForm.isDisabled = IS_DISABLED.FALSE;
    ruleForm.regionCode = '';
    ruleForm.remark = '';
    ruleForm.latitude = undefined;
    ruleForm.longitude = undefined;
  };
</script>

<style scoped></style>
