<template>
  <div class="bigBox">

    <div class="right">
      <!-- 头部 -->
      <div class="titleBox">
        <div class="title">{{ props.title }}</div>
        <div class="btnBox">
          <el-button style="margin-left: 10px" type="primary" @click="handleSave(ruleFormRef)" v-if="props.saveBtnShow">
            <el-icon>
              <Search />
            </el-icon>
            <span>保存</span>
          </el-button>
          <el-button type="primary" @click="CloseEvent">
            <el-icon>
              <Back />
            </el-icon>
            <span>返回</span>
          </el-button>
        </div>
      </div>
      <div class="box_cont">
        <div class="searchBox" ref="searchBox">
          <el-form class="mosty-from-wrap" ref="ruleFormRef" label-width="130px" :model="data.fromObj" :inline="true"
            :rules="rules" :disabled="!props.saveBtnShow">
            <el-form-item label="消防站名称:" prop="firestationname">
              <el-input v-model="data.fromObj.firestationname" clearable style="width: 1150px" />
            </el-form-item>
            <el-form-item label="队员人数:" prop="peoplenumber">
              <el-input v-model="data.fromObj.peoplenumber" type='number' clearable style="width: 250px" />
            </el-form-item>
            <el-form-item label="部位名称:" prop="allname">
              <el-select v-model="data.fromObj.allname" style="width: 500px" @change="cahngebw" clearable
                placeholder="请选择部位名称：">
                <el-option v-for="item in bwlist" :key="item.id" :label="item.areaname" :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="地址:">
              <el-input v-model="data.fromObj.address" clearable style="width: 1150px" />
            </el-form-item>
            <el-form-item label="备注:">
              <el-input v-model="data.fromObj.bz" clearable style="width: 1150px" />
            </el-form-item>
            <el-collapse v-model="activeNames" style='width:100%;'>
              <el-collapse-item title="消防站器材信息列表" name="1">
                <div>
                  <el-button class="addbtn" type="primary" @click="addShowinput">
                    <el-icon>
                      <CirclePlus />
                    </el-icon>
                    <span>新增</span>
                  </el-button>
                  <el-button class="addbtn" type="primary" @click="submittab">
                    <el-icon>
                      <Select />
                    </el-icon>
                    <span>保存</span>
                  </el-button>
                  <el-button class="addbtn" type="primary" @click="deletetab">
                    <el-icon>
                      <DeleteFilled />
                    </el-icon>
                    <span>删除</span>
                  </el-button>
                </div>
                <div class="tabbox">
                  <el-table :data="data.fromObj.tbMiniFireStationChildList" border stripe style="width: 100%"
                    :height="tableHeight" :key="keyCount" @row-click="handlecheckChange">
                    <el-table-column sortable prop="equipmentname" align="center" label="名称">
                      <template #default="{ row }">
                        <input v-if="row.input == true" type="text" v-model="row.equipmentname" />
                        <span v-else>{{ row.equipmentname }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column sortable prop="equipmentmodel" label="型号">
                      <template #default="{ row }">
                        <input v-if="row.input == true" type="text" v-model="row.equipmentmodel" />
                        <span v-else>{{ row.equipmentmodel }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column sortable prop="equipmentspecifications" align="center" label="规格">
                      <template #default="{ row }">
                        <input v-if="row.input == true" type="text" v-model="row.equipmentspecifications" />
                        <span v-else>{{ row.equipmentspecifications }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column sortable prop="equipmentnumber" align="center" label="数量">
                      <template #default="{ row }">
                        <input v-if="row.input == true" type="number" v-model="row.equipmentnumber" />
                        <span v-else>{{ row.equipmentnumber }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column sortable prop="equipmentcompany" align="center" label="单位">
                      <template #default="{ row }">
                        <el-select v-if="row.input == true" v-model="row.equipmentcompany" style="width: 100%" clearable
                          placeholder="请选择单位：">
                          <el-option v-for="item in D_BZ_WPDW" :key="item" :label="item.label" :value="item.value" />
                        </el-select>
                        <dict-tag v-else :options="D_BZ_WPDW" :value="row.equipmentcompany" :tag="false" />
                      </template>
                    </el-table-column>
                    <el-table-column sortable prop="username" align="center" label="责任人">
                      <template #default="{ row }">
                        <input v-if="row.input == true" type="text" v-model="row.username" />
                        <span v-else>{{ row.username }}</span>
                      </template>
                    </el-table-column>

                    <el-table-column sortable prop="bz" align="center" label="备注">
                      <template #default="{ row }">
                        <input v-if="row.input == true" type="text" v-model="row.bz" />
                        <span v-else>{{ row.bz }}</span>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </el-collapse-item>
            </el-collapse>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {
  ref,
  onMounted,
  defineProps,
  defineEmits,
  reactive,
  watch,
  watchEffect,
  getCurrentInstance
} from "vue";
import { ElMessage } from "element-plus";
import { getApi, deleteApi, putApi, postApi } from "@/api/tobAcco_api.js";
const { proxy } = getCurrentInstance();
const { D_BZ_SBSSZT, D_BZ_WPDW, D_TB_JCBZLB, D_BZ_JXSBLX, D_BZ_JCZT } =
  proxy.$dict(
    "D_BZ_SBSSZT",
    "D_BZ_WPDW",
    "D_TB_JCBZLB",
    "D_BZ_JXSBLX",
    "D_BZ_JCZT"
  );
const props = defineProps({
  title: {
    type: String,
    default: ""
  },
  saveBtnShow: {
    type: Boolean,
    default: true
  },
  editObj: {
    type: Object,
    default: () => { }
  }
});
const rules = reactive({
  firestationname: [{ required: true, message: "消防站名称不能为空", trigger: "blur" }],
  peoplenumber: [{ required: true, message: "队员人数不能为空", trigger: "blur" }],
  allname: [{ required: true, message: "部位名称不能为空", trigger: "blur" }],
});
const activeNames = ref(1);
const emits = defineEmits(["close"]);
const currentRow = ref(); //选中的表格数据
const currentRowjc = ref(); //选中的表格数据

const jcjlTable = ref([]);

const CloseEvent = () => {
  if (props.saveBtnShow) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        // 关闭上一次的表单校验
        ruleFormRef.value.clearValidate();
        emits("close");
      })
      .catch(() => {
        // catch error
      })
  } else {
    // 关闭上一次的表单校验
    ruleFormRef.value.clearValidate();
    emits("close");
  }

};

onMounted(() => {
  activeNames.value = ["1", "2"];
  getbwdata();
});
const data = reactive({
  fromObj: {
    tbMiniFireStationChildList: []
  }
});

//新增表格
function addShowinput() {
  let tab = {
    equipmentname: "",
    equipmentmodel: "",
    equipmentspecifications: "",
    equipmentnumber: "",
    equipmentcompany: "",
    username: "",
    bz: "",
    input: true
  };
  if (data.fromObj.tbMiniFireStationChildList && data.fromObj.tbMiniFireStationChildList.length) {
    if (data.equipmentname != "" && data.equipmentmodel != "") {
      data.fromObj.tbMiniFireStationChildList.push(tab);
    }
  } else {
    data.fromObj.tbMiniFireStationChildList.push(tab);
  }
}
//保存表格数据
function submittab() {
  data.fromObj.tbMiniFireStationChildList.forEach((el) => {
    el.input = false;
  });
}
//删除表格数据
function deletetab() {
  data.fromObj.tbMiniFireStationChildList.forEach((item, index) => {
    if (currentRowjc.value == item) {
      data.fromObj.tbMiniFireStationChildList.splice(index, 1);
    }
  });
}
//选中当前表格行
function handlecheckChange(val) {
  currentRowjc.value = val;
  if (props.saveBtnShow) {
    data.fromObj.tbMiniFireStationChildList.forEach((el) => {
      if (el == val) {
        el.input = true;
      }
    });
  }
}
const allname = ref("");
const bwlist = ref([]);
//根据操作人 获取部位信息
function getbwdata() {
  getApi({}, "/mosty-jcgl/part/queryScenePartByOrgid").then((res) => {
    bwlist.value = res;
  });
}
//获取当前部位
const cahngebw = (val) => {
  bwlist.value.forEach((item) => {
    if (item.id == val) {
      data.fromObj.partid = item.id;
      data.fromObj.partname = item.areaname;
    }
  });
};
watchEffect(() => {
  if (props.editObj) {
    data.fromObj = props.editObj;
    data.fromObj.tbMiniFireStationChildList = data.fromObj
      .tbFireControlRoomChildList
      ? data.fromObj.tbMiniFireStationChildList
      : [];
    data.fromObj.allname = data.fromObj.partname;
  }
});
const ruleFormRef = ref();
// 保存
const handleSave = async (ruleFormRef) => {
  if (!ruleFormRef) return;
  await ruleFormRef.validate((valid) => {
    if (valid) {
      data.fromObj.tbMiniFireStationChildList
        ? data.fromObj.tbMiniFireStationChildList
        : (data.fromObj.tbMiniFireStationChildList = []);
      if (data.fromObj) {
        if (props.title == "微型消防站新增") {
          postApi(
            data.fromObj,
            "/mosty-jcgl/miniFireStation/addMiniFireStationOne"
          )
            .then((res) => {
              ElMessage.success("新增成功");
              emits("close");
            })
        } else if (props.title == "微型消防站修改") {
          postApi(
            data.fromObj,
            "/mosty-jcgl/miniFireStation/updateMiniFireStationOne"
          )
            .then((res) => {
              ElMessage.success("修改成功");
              emits("close");
            })
        }
      }
    } else {
      ElMessage.error("请输入必填项!");
    }
  });
};
</script>

<style lang="scss" scoped>
.box_cont {
  height: calc(100vh - 10vh);
  overflow-y: scroll;
}

.searchBox {
  width: 1333px;
  margin: 20px auto;
}

.ld_box {
  display: flex;
  margin-right: 30px;
}

.el-form {
  width: 100%;
  align-items: center;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.el-form-item {
  display: flex;
  margin-right: 20px;
}

.addbtn {
  margin-bottom: 10px;
}

.bigBox {
  display: block !important;
  padding: 0 14px;
  box-sizing: border-box;
}

.tabbox {
  max-height: 300px;
  overflow-y: scroll;

  .input {
    width: 100%;
    height: 100%;
  }
}

::v-deep .el-table .cell {
  padding: 0 0 !important;
}

::v-deep .el-table--border .el-table__cell .cell {
  padding-left: 0px !important;

  >input {
    width: 100%;
    height: 33px;
  }
}

::v-deep .el-upload-list {
  width: 50%;
}
</style>