<script setup lang="ts">
import { FormRules, ElForm } from "element-plus";
import { reactive, ref } from "vue";
import { httpUpdate, httpAdd, httpDetail } from "/@/api/operate/setComCard";
import { responseHandle } from "/@/utils/responseHandle";
import { useNav } from "/@/layout/hooks/useNav";
import { Company, Card } from "/@/components/RemoteSelect";
const remoteCompanySelectRef = ref<InstanceType<typeof Company>>(null);
const remoteCardSelectRef = ref<InstanceType<typeof Card>>(null);
enum FROM_TYPE {
  create = "create",
  edit = "edit",
  view = "view"
}
const { logout } = useNav();
const showModel = ref(false);
const Video_id = ref("");
const TYPE = ref<FROM_TYPE>(FROM_TYPE.create);
const formRef = ref<InstanceType<typeof ElForm>>(null);
const loading = ref(false);
const titleType = ref("");
const project_name = ref("");
const id = ref("");
const emit = defineEmits(["reload"]);
const initform = {
  company_id: "",
  card_id: "",
  id: ""
};
const ruleForm = ref({ ...initform });
const rules = reactive<FormRules>({
  company_id: [{ required: true, trigger: "change", message: "请选择公司" }],
  card_id: [
    {
      required: true,
      trigger: "change",
      message: "请选择卡类型"
    }
  ]
});

const initData = async () => {
  const { code, data, message } = await httpDetail({ id: id.value });
  responseHandle({
    code,
    message,
    logout,
    handler: () => {
      Object.keys(ruleForm.value).forEach(key => {
        ruleForm.value[key] = data[key] ? data[key] + "" : "";
      });
      const { combination } = data;
      const arr = combination.split("_");
      if (remoteCompanySelectRef.value && arr.length === 2) {
        (remoteCompanySelectRef.value as any).initalData({
          title: arr[0]
        });
      }
      if (remoteCardSelectRef.value && arr.length === 2) {
        (remoteCardSelectRef.value as any).initalData({
          title: arr[1]
        });
      }
    }
  });
};
async function show(node: any, did: string, isCreate: string) {
  id.value = did;
  loading.value = true;
  project_name.value = "";
  Video_id.value = "";
  Object.keys(ruleForm.value).forEach(key => {
    ruleForm.value[key] = initform[key];
  });
  TYPE.value = isCreate;
  switch (TYPE.value) {
    case "create":
      titleType.value = "新建公司卡类型";
      break;
    case "edit":
      titleType.value = "编辑公司卡类型";
      break;
    case "view":
      titleType.value = "公司卡类型详情";
      break;
    default:
      titleType.value = "新建公司卡类型";
  }

  showModel.value = true;
  if (TYPE.value !== "create") {
    await initData();
  }
  loading.value = false;
}
function handleUpdate() {
  const data = {
    ...ruleForm.value
  };

  return {
    data,
    api: httpUpdate
  };
}

function handleCreate() {
  const data = {
    ...ruleForm.value
  };

  return {
    data,
    api: httpAdd
  };
}
function handleSave() {
  formRef.value.validate(async vaild => {
    if (vaild) {
      if (loading.value) return;
      loading.value = true;
      const handler =
        TYPE.value === FROM_TYPE.create ? handleCreate : handleUpdate;

      loading.value = true;
      const { api, data } = handler();
      const { message, code } = await api(data);
      loading.value = false;
      responseHandle({
        code,
        message,
        logout,
        handler: () => {
          showModel.value = false;
          emit("reload");
        }
      });
    }
  });
}

defineExpose({
  show
});
</script>

<template>
  <el-dialog
    v-model="showModel"
    :close-on-press-escape="false"
    center
    append-to-body
    destroy-on-close
    :width="'650px'"
    :title="titleType"
    v-loading="loading"
  >
    <el-form
      ref="formRef"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      style="margin-top: -10px"
      class="demo-ruleForm"
      status-icon
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="公司" prop="company_id">
            <Company
              ref="remoteCompanySelectRef"
              v-model="ruleForm.company_id"
              :disabled="TYPE === 'view'"
              :showDisabled="true"
              placeholder="公司"
            /> </el-form-item
        ></el-col>
        <el-col :span="24">
          <el-form-item label="卡类型" prop="card_id">
            <Card
              ref="remoteCardSelectRef"
              v-model="ruleForm.card_id"
              :disabled="TYPE === 'view'"
              :showDisabled="true"
              placeholder="卡类型"
            /> </el-form-item
        ></el-col>

        <el-col :span="24" class="flex justify-end">
          <el-button
            v-if="TYPE !== 'view'"
            :loading="loading"
            :disabled="loading"
            type="primary"
            @click="handleSave"
            >保存</el-button
          >
          <el-button @click="showModel = false">取消</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>
