<template>
  <div class="airportInfo scroll-y">
    <div class="wrap">
      <Minheader
        :is-auth="true"
        :is-statuser="true"
        powerData="add_data_item_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
        width="800px"
        :flag="flag"
        :type="type"
        :msgTitle="msgTitle"
        @resetForm="resetForm"
        @delRest="delRest"
        @submitForm="submitForm"
        @delRemove="delRemove"
      >
        <div class="diacont">
          <el-form
            :model="tableForm"
            :rules="formRules"
            ref="datastructureTermForm"
          >
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item
                  label="数据项名称"
                  size="default"
                  prop="ItemName"
                  :rules="formRules.isNotNull"
                >
                  <el-input
                    v-model="tableForm.ItemName"
                    placeholder="请输入数据项名称"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="数据项类型" size="default">
                  <!-- <el-input
                    v-model="tableForm.dataType"
                    placeholder="请输入数据项类型"
                  /> -->
                  <el-select
                    style="width: 100%"
                    v-model="tableForm.dataType"
                    class="input-shadow"
                    filterable
                    default-first-option
                    clearable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in tableOptioner"
                      :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"
                  prop="outputIdentifier"
                  :rules="formRules.isNotNull"
                >
                  <el-input
                    v-model="tableForm.outputIdentifier"
                    placeholder="请输入输出标识符"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <!-- <el-form-item label="描述" size="default">
                  <el-input v-model="tableForm.two" placeholder="请输入描述" />
                </el-form-item> -->
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="唯一项"
                  size="default"
                  prop="isKey"
                  :rules="formRules.isNotNull"
                >
                  <el-select
                    style="width: 100%"
                    v-model="tableForm.isKey"
                    class="input-shadow"
                    filterable
                    default-first-option
                    clearable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in tableOptionser"
                      :key="item.v"
                      :label="item.k"
                      :value="item.v"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="必填项"
                  size="default"
                  prop="isMust"
                  :rules="formRules.isNotNull"
                >
                  <el-select
                    style="width: 100%"
                    v-model="tableForm.isMust"
                    class="input-shadow"
                    filterable
                    default-first-option
                    clearable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in tableOptionser"
                      :key="item.v"
                      :label="item.k"
                      :value="item.v"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="多条拆分"
                  size="default"
                  prop="isSplite"
                  :rules="formRules.isNotNull"
                >
                  <el-select
                    style="width: 100%"
                    v-model="tableForm.isSplite"
                    class="input-shadow"
                    filterable
                    default-first-option
                    clearable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in tableOptionser"
                      :key="item.v"
                      :label="item.k"
                      :value="item.v"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <!-- <el-col>
                <el-form-item label="取值表达式" size="default">
                  <el-input
                    type="textarea"
                    v-model="tableForm.text2"
                    placeholder="请输入取值表达式"
                  />
                </el-form-item>
              </el-col> -->
              <!-- <el-col>
                <el-form-item label="实时计算表达式" size="default">
                  <el-input
                    type="textarea"
                    v-model="tableForm.text3"
                    placeholder="请输入实时计算表达式"
                  />
                </el-form-item>
              </el-col> -->
              <!-- <el-col>
                <el-form-item label="计算结果标识符" size="default">
                  <el-input
                    v-model="tableForm.text4"
                    placeholder="请输入计算结果标识符"
                  />
                </el-form-item>
              </el-col> -->
              <!-- <el-col>
                <el-form-item label="计算参数" size="default">
                  <el-input
                    v-model="tableForm.text5"
                    placeholder="请输入计算参数"
                  />
                </el-form-item>
              </el-col> -->
              <el-col :span="12">
                <el-form-item label="计算数据源" size="default">
                  <el-select
                    style="width: 100%"
                    v-model="tableForm.dataSourceID"
                    class="input-shadow"
                    filterable
                    default-first-option
                    clearable
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in tableOption"
                      :key="item.v"
                      :label="item.k"
                      :value="item.v"
                    >
                    </el-option>
                  </el-select>
                </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 router = useRouter();
const route = useRoute();
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 tableOptionser = ref<Array>([
  {
    k: "是",
    v: "1",
  },
  {
    k: "否",
    v: "0",
  },
]); //弹窗下拉
const tableOption = ref([]); //弹窗下拉
const tableOptioner = ref([]); //弹窗下拉
const tableForm = reactive({
  ItemID: "",
  dataStructureID: route.query.dataStructureID,
  ItemName: "",
  outputIdentifier: "",
  dataType: "",
  isKey: "",
  isMust: "",
  isSplite: "",
  dataSourceName: "",
  dataSourceID: "",
  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" },
    { label: "计算数据源", key: "text1" },
  ],
  listLoading: true,
});
const tableBtnGroup = ref([
  {
    name: "编辑",
    className: "editBtn",
    param: 2,
    is: "data_item_editing_button",
  },
  {
    name: "删除",
    className: "delBtn",
    param: 3,
    is: "data_item_deletion_button",
  },
]);
//新增
const addForm = () => {
  msgTitle.value = "新增数据项";
  tableForm.event = 1;
  flag.value = true;
  type.value = "";
};
//取消
const resetForm = () => {
  flag.value = false;
  tableForm.ItemID = "";
  tableForm.ItemName = "";
  tableForm.outputIdentifier = "";
  tableForm.dataType = "";
  tableForm.isKey = "";
  tableForm.isMust = "";
  tableForm.isSplite = "";
  tableForm.dataSourceName = "";
};
//编辑
// 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) {
    msgTitle.value = "编辑数据项";
    flag.value = true;
    type.value = "";
    tableForm.event = 2;
    tableForm.ItemID = index.ItemID;
    tableForm.ItemName = index.ItemName;
    tableForm.outputIdentifier = index.outputIdentifier;
    tableForm.dataType = index.dataType;
    tableForm.isKey = index.isKey === "唯一" ? "1" : "0";
    tableForm.isMust = index.isMust === "必填" ? "1" : "0";
    tableForm.isSplite = index.isSplite === "拆分" ? "1" : "0";
    tableForm.dataSourceID = index.dataSourceID;
    tableForm.dataSourceName = index.dataSourceName;
  } else if (param === 3) {
    msgTitle.value = "删除数据项";
    flag.value = true;
    type.value = "del";
    tableForm.event = 3;
    tableForm.ItemID = index.ItemID;
    tableForm.ItemName = index.ItemName;
    tableForm.outputIdentifier = index.outputIdentifier;
    tableForm.dataType = index.dataType;
    tableForm.isKey = index.isKey;
    tableForm.isMust = index.isMust;
    tableForm.isSplite = index.isSplite;
    tableForm.dataSourceName = index.dataSourceName;
  } 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 getSelectData = async (id, name, columnName) => {
  const { code, returnData } = await Query({
    id,
    dataContent: name ? [name] : name === null ? [null] : [],
  });
  if (code == 0) {
    if (columnName === "dataSourceName") {
      tableOption.value = returnData.listValues;
    } else if (columnName === "dataType") {
      tableOptioner.value = returnData.listValues;
    }
  } else {
    return [];
  }
};
//获取表格数据
const getQuery = async () => {
  try {
    const { code, returnData } = await Query({
      id: DATACONTENT_ID.dataitemTabId,
      needPage: ++page.value,
      dataContent: [route.query.dataStructureID],
    });
    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;
      returnData.columnSet.forEach((item) => {
        if (item.listqueryTemplateID && item.columnName === "dataSourceName") {
          getSelectData(item.listqueryTemplateID, "", "dataSourceName");
        }
        if (item.listqueryTemplateID && item.columnName === "dataType") {
          getSelectData(item.listqueryTemplateID, "", "dataType");
        }
      });
      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 datastructureTermForm: any = ref(null);
const submitForm = () => {
  datastructureTermForm.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 {
    let obj = {
      dataStructureID: route.query.dataStructureID,
    };
    data = {
      ...data,
      ...obj,
    };
    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();
  }
};
getQuery();
</script>
<style lang="scss" scoped>
::v-deep .el-form-item__label {
  width: 110px;
}
.app-containers {
  height: calc(100vh - 180px);
}
</style>
