<template>
  <div class="airportInfo scroll-y">
    <div class="wrap">
      <Minheader :is-auth="true" :is-statuser="true" @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.fullName" @resetForm="resetForm" @delRest="delRest" @submitForm="submitForm" @delRemove="delRemove">
        <div class="diacont">
          <el-form :model="tableForm" :rules="formRules" ref="airlineCompanyForm">
            <el-row :gutter="24">
              <el-col>
                <el-form-item label="中文名称" size="default" prop="fullName" :rules="formRules.isNotNull">
                  <el-input v-model="tableForm.fullName" placeholder="请输入中文名称" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="中文简称" size="default" prop="abbreviation" :rules="formRules.isNotNull">
                  <el-input v-model="tableForm.abbreviation" placeholder="请输入中文简称" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="英文全称" size="default">
                  <el-input v-model="tableForm.englishFullName" placeholder="请输入英文全称" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="英文简称" size="default">
                  <el-input v-model="tableForm.englishAbbreviation" placeholder="请输入英文简称" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="航司二字码" size="default" prop="ITATCode" :rules="formRules.isNotNull">
                  <el-input v-model="tableForm.ITATCode" placeholder="请输入航司二字码" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="航司三字码" size="default" prop="ICAOCode" :rules="formRules.isNotNull">
                  <el-input v-model="tableForm.ICAOCode" placeholder="请输入航司三字码" />
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="备注" size="default">
                  <el-input type="textarea" v-model="tableForm.remarks" 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([]); //表头数据
const serviceId = ref<String>("");
const tableObj = ref({}); //增删改数据缓存
const flag = ref<Boolean>(false); //弹窗开关
const type = ref<String>(""); //判断是否删除
const msgTitle = ref<String>("新增航司信息维护"); //弹窗标题
const tableColsCopys = reactive<Object>({}); //弹窗
const tableForm = reactive({
  fullName: "",
  abbreviation: "",
  englishAbbreviation: "",
  ITATCode: "",
  ICAOCode: "",
  remarks: "",
  event: "",
  airlineCompanyID: "",
  englishFullName: "",
}); //弹窗内容
//列表
const tableData = ref([]);
//表头
const state = reactive({
  list: [
    { label: "名称", key: "name" },
    { label: "中文简称", key: "china" },
    { label: "英文简称", key: "englin" },
    { label: "航司二字码", key: "two" },
    { label: "航司三字码", key: "three" },
    { label: "备注", key: "text" },
  ],
  listLoading: true,
});
const tableBtnGroup = ref([
  {
    name: "编辑",
    className: "editBtn",
    param: 2,
  },
  {
    name: "删除",
    className: "delBtn",
    param: 3,
  },
]);
//新增
const addForm = () => {
  msgTitle.value = "新增航司信息维护";
  tableForm.event = 1;
  flag.value = true;
  type.value = "";
};
//取消
const resetForm = () => {
  flag.value = false;
  tableForm.fullName = "";
  tableForm.abbreviation = "";
  tableForm.englishAbbreviation = "";
  tableForm.ITATCode = "";
  tableForm.ICAOCode = "";
  tableForm.remarks = "";
  tableForm.airlineCompanyID = "";
  tableForm.englishFullName = "";
};
//编辑-删除
const btnClick = (row, index, param) => {
  if (param === 2) {
    msgTitle.value = "编辑航司信息维护";
    flag.value = true;
    type.value = "";
    tableForm.event = 2;
    tableForm.fullName = index.fullName;
    tableForm.abbreviation = index.abbreviation;
    tableForm.englishAbbreviation = index.englishAbbreviation;
    tableForm.ITATCode = index.ITATCode;
    tableForm.ICAOCode = index.ICAOCode;
    tableForm.remarks = index.remarks;
    tableForm.airlineCompanyID = index.airlineCompanyID;
    tableForm.englishFullName = index.englishFullName;
  } else if (param === 3) {
    msgTitle.value = "删除航司信息维护";
    flag.value = true;
    type.value = "del";
    tableForm.event = 3;
    tableForm.fullName = index.fullName;
    tableForm.abbreviation = index.abbreviation;
    tableForm.englishAbbreviation = index.englishAbbreviation;
    tableForm.ITATCode = index.ITATCode;
    tableForm.ICAOCode = index.ICAOCode;
    tableForm.remarks = index.remarks;
    tableForm.airlineCompanyID = index.airlineCompanyID;
  } else if (param === 4) {
  }
};
//删除
const eleDialog = () => {
  msgTitle.value = "删除航司信息维护";
  flag.value = true;
  type.value = "del";
};
const delRemove = () => {
  tableForm.event = 3;
  generalDataReception(tableForm);
};
//删除
const delRest = () => {
  flag.value = false;
};
//获取表格数据
const getQuery = async () => {
  try {
    const { code, returnData } = await Query({
      id: DATACONTENT_ID.airlineTableId,
      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 airlineCompanyForm: any = ref(null);
const submitForm = () => {
  airlineCompanyForm.value.validate((valid: any) => {
    if (valid) {
      generalDataReception(tableForm);
    } else {
      return false;
    }
  });
};
const resetTable = () => {
  page.value = 0;
  noMore.value = false;
  tableData.value = [];
};
//新增-编辑-删除
const generalDataReception = async (data) => {
  try {
    data = {
      ...data,
    };
    const { code } = await GeneralDataReception({
      serviceId: serviceId.value,
      dataContent: JSON.stringify(data),
    });
    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();
  }
};
onMounted(() => {
  getQuery();
});
</script>
<style lang="scss" scoped>
::v-deep .el-form-item__label {
  width: 100px;
}
.app-containers {
  height: calc(100vh - 180px);
}
</style>
