<template>
  <div class="airportInfo scroll-y">
    <div class="wrap">
      <Minheader
        :is-auth="true"
        :is-statuser="true"
        powerData="alert_alarm_settings_add_button"
        @addForm="addForm"
        @searchForms="searchForms"
      >
        <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
        width="600px"
        :flag="flag"
        :type="type"
        :msgTitle="msgTitle"
        :delName="delName"
        @resetForm="resetForm"
        @delRest="delRest"
        @submitForm="submitForm"
        @delRemove="delRemove"
      >
        <div class="diacont">
          <el-form :model="tableForm" :rules="formRules" ref="warningSetForm">
            <el-row :gutter="24">
              <!-- <el-col :span="12">
                <el-form-item label="策略名称" size="default">
                  <el-input
                    v-model="tableForm.name"
                    placeholder="请输入策略名称"
                  />
                </el-form-item>
              </el-col> -->
              <el-col :span="12">
                <el-form-item
                  label="航司二字码"
                  size="default"
                  prop="IATACode"
                  :rules="formRules.isNotNull"
                >
                  <el-input
                    v-model="tableForm.IATACode"
                    placeholder="请输入航司二字码"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="起飞机场" size="default">
                  <el-select
                    style="width: 100%"
                    v-model="tableForm.departmentAirport"
                    class="input-shadow"
                    filterable
                    default-first-option
                    clearable
                    placeholder="请选择起飞机场"
                  >
                    <el-option
                      v-for="item in tableOptionser"
                      :key="item.v ? item.v : item.planDepartureApt"
                      :label="item.k ? item.k : item.planDepartureApt"
                      :value="item.v ? item.v : item.planDepartureApt"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="降落机场" size="default">
                  <el-select
                    style="width: 100%"
                    v-model="tableForm.landingAirport"
                    class="input-shadow"
                    filterable
                    default-first-option
                    clearable
                    placeholder="请选择降落机场"
                  >
                    <el-option
                      v-for="item in tableOptionser"
                      :key="item.v ? item.v : item.planDepartureApt"
                      :label="item.k ? item.k : item.planDepartureApt"
                      :value="item.v ? item.v : item.planDepartureApt"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item
                  label="航班号"
                  size="default"
                  prop="flightNo"
                  :rules="formRules.isNotNull"
                >
                  <el-input
                    v-model="tableForm.flightNo"
                    placeholder="请输入航班号"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="生效时间"
                  size="default"
                  prop="startDate"
                  :rules="formRules.isNotNull"
                >
                  <el-date-picker
                    v-model="tableForm.startDate"
                    :rows="1"
                    type="date"
                    format="YYYY-MM-DD"
                    value-format="YYYY-MM-DD"
                    placeholder="选择日期时间"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="失效时间"
                  size="default"
                  prop="endDate"
                  :rules="formRules.isNotNull"
                >
                  <el-date-picker
                    format="YYYY-MM-DD"
                    value-format="YYYY-MM-DD"
                    v-model="tableForm.endDate"
                    :rows="1"
                    type="date"
                    placeholder="选择日期时间"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <!-- <el-col>
                <el-form-item label="备注" size="default">
                  <el-input
                    type="textarea"
                    v-model="tableForm.text"
                    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 router = useRouter();
const flag = ref<Boolean>(false); //弹窗开关
const type = ref<String>(""); //判断是否删除
const msgTitle = ref<String>("新增告警预警维护"); //弹窗标题
const tableColsCopys = reactive<Object>({}); //弹窗\
const tableOptionser = ref<Array>([]); //弹窗下拉
const submitID = ref<String>("");
const delName = ref<String>("");
const listqueryTemplateID = ref<String>("");
const tableForm = reactive({
  IATACode: "",
  departmentAirport: "",
  landingAirport: "",
  flightNo: "",
  startDate: "",
  endDate: "",
  alarmSceneId: "",
  event: "",
}); //弹窗内容
//列表
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,
    is: "edit_alert_alarm_settings_button",
  },
  {
    name: "删除",
    className: "delBtn",
    param: 3,
    is: "delete_alert_alarm_settings_button",
  },
]);
//获取弹框-下拉数据
const getSelectData = async (id, name) => {
  const { code, returnData } = await Query({
    id,
    dataContent: name ? [name] : name === null ? [null] : [],
  });
  if (code == 0) {
    tableOptionser.value = returnData.listValues;
  } else {
    tableOptionser.value = [];
  }
};
//新增
const addForm = () => {
  msgTitle.value = "新增告警预警策略";
  flag.value = true;
  type.value = "";
  tableForm.event = 1;
};
//取消
const resetForm = () => {
  flag.value = false;
  tableForm.IATACode = "";
  tableForm.departmentAirport = "";
  tableForm.landingAirport = "";
  tableForm.flightNo = "";
  tableForm.startDate = "";
  tableForm.endDate = "";
  tableForm.event = "";
  delName.value = "";
};
//编辑
// const editDialog = (data) => {
//   msgTitle.value = "编辑航司信息维护";
//   flag.value = true;
//   type.value = "";
//   tableForm.name = data.name;
//   tableForm.china = data.china;
//   tableForm.englin = data.englin;
//   tableForm.two = data.two;
//   tableForm.three = data.three;
//   tableForm.text = data.text;
// };
//编辑-删除
const btnClick = (row, index, param) => {
  if (param === 2) {
    router.push({
      path: "/systemSettings/warningEdit",
      query: {
        alarmSceneId: index.alarmSceneId,
        submitID: submitID.value,
        listqueryTemplateID: listqueryTemplateID.value,
      },
    });
  } else if (param === 3) {
    msgTitle.value = "删除告警预警维护";
    flag.value = true;
    type.value = "del";
    tableForm.event = 3;
    delName.value = index.flightNo;
    tableForm.alarmSceneId = index.alarmSceneId;
    tableForm.IATACode = index.IATACode;
    tableForm.departmentAirport = index.departmentAirport;
    tableForm.landingAirport = index.landingAirport;
    tableForm.flightNo = index.flightNo;
    tableForm.startDate = index.startDate;
    tableForm.endDate = index.endDate;
  } 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 (data) => {
  try {
    const { code, returnData } = await Query({
      id: DATACONTENT_ID.sysServiceWarningId,
      needPage: ++page.value,
      dataContent: data ? [data] : [],
    });
    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;
      }
      submitID.value = returnData.submitID;
      tableData.value.push(...returnData.listValues);
      tableCols.value = returnData.columnSet;
      returnData.columnSet.forEach((item) => {
        if (item.listqueryTemplateID && item.columnName === "landingAirport") {
          listqueryTemplateID.value = item.listqueryTemplateID;
          getSelectData(item.listqueryTemplateID);
        }
      });
      tableCols.value.forEach((element) => {
        element.label = element.columnLabel;
        element.key = element.columnName;
      });
      serviceId.value = returnData.submitID;
    } else {
      page.value--;
    }
  } catch (error) {
    page.value--;
  }
};
//确认提交
const warningSetForm: any = ref(null);
const submitForm = () => {
  warningSetForm.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();
  }
};
//清除
const searchForms = (data) => {
  page.value = 0;
  tableData.value = [];
  getQuery(data);
};
// getQuery();
</script>
<style lang="scss" scoped>
::v-deep .el-form-item__label {
  width: 100px;
}
</style>
