<template>
  <div class="airportInfo scroll-y">
    <div class="wrap">
      <Minheader :is-auth="true" :is-statuser="true" powerData="new_model_button" @addForm="addForm">
        <template #header>
          <div class="status flex-wrap">
            <div class="manageTitle">机型维护</div>
          </div>
        </template>
      </Minheader>
      <div class="app-containers">
        <DataTable :tableHeader="tableCols" :tableData="tableData" :tableBtnGroup="tableBtnGroup" :tableProperty="{ rowKey: 'ID' }" @btnClick="btnClick" />
      </div>
      <Dialog :flag="flag" :type="type" :msgTitle="msgTitle" :delName="tableForm.modelName" @resetForm="resetForm" @delRest="delRest" @submitForm="submitForm" @delRemove="delRemove">
        <div class="diacont">
          <el-form :model="tableForm" :rules="formRules" ref="modelMaintenanceForm">
            <el-row :gutter="24">
              <el-col>
                <el-form-item label="机型ID" size="default" prop="modelID" :rules="formRules.isNotNull">
                  <el-input v-model="tableForm.modelID" placeholder="请输入机型ID" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="机型名称" size="default" prop="modelName" :rules="formRules.isNotNull">
                  <el-input v-model="tableForm.modelName" placeholder="请输入机型名称" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="机型类型" size="default" prop="modelType" :rules="formRules.isNotNull">
                  <el-input v-model="tableForm.modelType" placeholder="请输入机型类型" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="机型编码" size="default" prop="modelCode" :rules="formRules.isNotNull">
                  <el-input v-model="tableForm.modelCode" placeholder="请输入机型编码" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="备注" size="default">
                  <el-input type="textarea" v-model="tableForm.modelDesc" placeholder="请输入备注" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </Dialog>
    </div>
  </div>
</template>
<script setup lang="ts">
import DataTable from "@/components/tableTemp/index.vue";
import Minheader from "@/components/minheader/index.vue";
import Dialog from "@/components/dialog/index.vue";
import { Query, GeneralDataReception } from "@/api/webApi";
import { ElMessage } from "element-plus";
const formRules = useElement().formRules;
const page = ref<number>(0); //分页参数
const dataContent = ref<object>({});
const noMore = ref<boolean>(false);
const rowTitle = ref<string>("");
const tableCols = ref<any>([]); //表头数据
const serviceId = ref<any>("");
const tableObj = ref({}); //增删改数据缓存
const router = useRouter();
const flag = ref<boolean>(false); //弹窗开关
const type = ref<string>(""); //判断是否删除
const msgTitle = ref<string>("新增机型维护"); //弹窗标题
const tableColsCopys = reactive<Object>({}); //弹窗
const tableBtnGroup = ref<any>([
  {
    name: "编辑",
    className: "editBtn",
    param: 2,
    is: "model_editing_button",
  },
  {
    name: "舱位编辑",
    className: "editBtn",
    param: 4,
    is: "cabin_editor_page",
  },
  {
    name: "删除",
    className: "delBtn",
    param: 3,
    is: "model_deletion_button",
  },
]); //单独的编辑
const tableForm = reactive<any>({
  modelID: "",
  modelName: "",
  modelType: "",
  modelDesc: "",
  modelCode: "",
  event: "",
}); //弹窗内容
//列表
const tableData = ref<any>([]);
//表头
const state = reactive({
  list: [
    { label: "机型名称", key: "name" },
    { label: "机型标识符", key: "china" },
    { label: "机型描述", key: "englin" },
  ],
  listLoading: true,
});
//新增
const addForm = () => {
  msgTitle.value = "新增机型维护";
  tableForm.event = 1;
  flag.value = true;
  type.value = "";
};
//取消
const resetForm = () => {
  modelMaintenanceForm.value?.resetFields();
  flag.value = false;
  // tableForm.modelID = "";
  // tableForm.modelName = "";
  // tableForm.modelType = "";
  // tableForm.modelDesc = "";
  // tableForm.modelCode = "";
};
//编辑
const editDialog = (data) => {
  msgTitle.value = "编辑机型维护";
  flag.value = true;
  type.value = "";
  tableForm.name = data.name;
  tableForm.china = data.china;
  tableForm.englin = data.englin;
};
//删除
const eleDialog = () => {
  msgTitle.value = "删除机型维护";
  flag.value = true;
  type.value = "del";
};
const delRemove = () => {
  tableForm.event = 3;
  generalDataReception(tableForm);
};
//删除
const delRest = () => {
  flag.value = false;
};
//舱位跳转
const aloneDialog = () => {
  router.push({ path: "/BasicsData/shippingSpace" });
};
//编辑-删除
const btnClick = (row, index, param) => {
  if (param === 2) {
    msgTitle.value = "编辑机型维护";
    flag.value = true;
    type.value = "";
    tableForm.event = 2;
    tableForm.modelID = index.modelID;
    tableForm.modelName = index.modelName;
    tableForm.modelType = index.modelType;
    tableForm.modelDesc = index.modelDesc;
    tableForm.modelCode = index.modelCode;
  } else if (param === 3) {
    msgTitle.value = "删除机型维护";
    flag.value = true;
    type.value = "del";
    tableForm.event = 3;
    tableForm.modelID = index.modelID;
    tableForm.modelName = index.modelName;
    tableForm.modelType = index.modelType;
    tableForm.modelDesc = index.modelDesc;
    tableForm.modelCode = index.modelCode;
  } else if (param === 4) {
    router.push({
      path: "/BasicsData/modelMaintenance/shippingSpace",
      query: {
        modelID: index.modelID,
        modelName: index.modelName,
      },
    });
  }
};
//获取表格数据
const getQuery = async () => {
  try {
    const { code, returnData } = await Query({
      id: DATACONTENT_ID.modelMainNodeId,
      needPage: ++page.value,
      dataContent: Object.values(dataContent.value),
    });
    if (code === "0") {
      if (returnData.listValues.length === 0) {
        page.value--;
        noMore.value = true;
      }
      const titleColumn = returnData.columnSet.find(
        (item) => item.needShow === 1
      );
      if (titleColumn) {
        rowTitle.value = titleColumn.columnName;
      }
      tableData.value.push(...returnData.listValues);
      tableCols.value = returnData.columnSet;
      tableCols.value.forEach((element) => {
        element.label = element.columnLabel;
        element.key = element.columnName;
        // if (element.columnName === "queryTemplate") {
        //   element.width = "300px";
        // }
      });
      serviceId.value = returnData.submitID;
    } else {
      page.value--;
    }
  } catch (error) {
    page.value--;
  }
};
//确认提交
const modelMaintenanceForm: any = ref(null);
const submitForm = () => {
  modelMaintenanceForm.value.validate((valid: any) => {
    if (valid) {
      generalDataReception(tableForm);
    } else {
      return false;
    }
  });
};
const resetTable = () => {
  page.value = 0;
  noMore.value = false;
  tableData.value = [];
};

const btnAuthMap = [, 'new_model_button', 'airline_editor_button', 'model_deletion_button']
//新增-编辑-删除
const generalDataReception = async (data) => {
  console.log(data);
  try {
    data = {
      ...data,
    };
    const { code } = await GeneralDataReception({
      serviceId: serviceId.value,
      dataContent: JSON.stringify(data),
      btnAuth: btnAuthMap[data.event]
    });
    if (code == 0) {
      ElMessage.success(`操作成功`);
      // this.$message.success("操作成功");
      resetTable();
      getQuery();
      resetForm();
      flag.value = false;
      // rmFlag.value = false;
      tableObj.value = {};
      // this.$router.go(0);
    } else {
      ElMessage.error(`操作失败`);
      // this.$message.error("操作失败");
      // this.flag = false;
      // this.rmFlag = false;
      tableObj.value = {};
      resetForm();
    }
  } catch (error) {
    flag.value = false;
    //  rmFlag.value = false;
    tableObj.value = {};
    resetForm();
  }
};
getQuery();
</script>
<style lang="scss" scoped>
::v-deep .el-form-item__label {
  width: 80px;
}
</style>
