<template>
  <!-- 危化品弹窗 -->
  <el-dialog center ref="dialog" :title="dataTitle" :show-close="false" modal :destory-on-close="true"
    class="dialogClass" :append-to-body="true" width="871px" custom-class="custom-dialog" v-model="props.visible">

    <section class="button">
      <el-button @click="exportExcels" :loading="excelLoading">导出</el-button>
      <el-button v-if="popForm" @click="backbtn">返回</el-button>
      <el-button @click="closeDialog">关闭</el-button>
    </section>

    <div class="itemTrading">
      <el-table v-if="!popForm" v-loading="tableloading" id="out-table" show-summary stripe
        :summary-method="getSummaries" ref="detailTable" height="450px" border :data="whpData">
        <el-table-column property="orgName" label="企业名称" width="250"></el-table-column>
        <el-table-column property="species" sortable label="种类"></el-table-column>
        <!-- <el-table-column label="操作" v-if="!popForm">
          <template #default="scope">
            <span class="operesee" @click="handleClick(scope.$index, scope.row)"><el-icon>
                <View />
              </el-icon>详情</span>
          </template>
</el-table-column> -->
      </el-table>

      <!-- 详情 -->
      <el-table stripe v-if="popForm" id="out-table" ref="detailTable" height="450px" border :data="detailData">
        <el-table-column property="goodsName" label="物品名称" width="250"></el-table-column>
        <el-table-column property="goodsModel" sortable label="物品型号"></el-table-column>
        <el-table-column property="stockNumber" sortable label="物品数量"></el-table-column>
        <el-table-column property="goodsCompany" sortable label="物品单位"></el-table-column>
        <el-table-column property="goodsSpec" sortable label="物品规格"></el-table-column>
      </el-table>
    </div>

  </el-dialog>
</template>

<script setup>
import { ref, defineProps, onMounted, reactive, watch, defineEmits } from "vue";
import { getSummaries } from "@/utils/tools";
import { getApi } from "@/api/tobAcco_api.js";
import { exportXlsx } from "@/excel/Export2Excel.js";
const tableloading = ref(false)
const excelLoading = ref(false)
const popForm = ref(false);
const detailTable = ref();
const emit = defineEmits(['changeVi'])
const listQuery = ref({
  code: ''
});
const props = defineProps({
  visible: false,
  dataTitle: {
    type: String,
    requried: true
  }
});
// 危化品
const whpData = ref([
  { orgName: '甘肃省烟草专卖局（公司）', species: 1 },
  { orgName: '兰州市烟草专卖局（公司）', species: 0 },
  { orgName: '天水市烟草专卖局（公司）', species: 0 },
  { orgName: '陇南市烟草专卖局（公司）', species: 0 },
  { orgName: '酒泉市烟草专卖局（公司）', species: 0 },
  { orgName: '嘉峪关市烟草专卖局（公司）', species: 0 },
  { orgName: '庆阳市烟草专卖局（公司）', species: 0 },
  { orgName: '白银市烟草专卖局（公司）', species: 0 },
  { orgName: '定西市烟草专卖局（公司）', species: 0 },
  { orgName: '金昌市烟草专卖局（公司）', species: 3 },
  { orgName: '平凉市烟草专卖局（公司）', species: 0 },
  { orgName: '武威市烟草专卖局（公司）', species: 0 },
  { orgName: '甘南州烟草专卖局（公司）', species: 2 },
  { orgName: '临夏州烟草专卖局（公司）', species: 0 },
]);
const detailData = ref([])


onMounted(() => {
  // getWhpData();
  popForm.value = false;

});

//关闭弹窗事件
function closeDialog() {
  emit("changeVi", false)
  popForm.value = false;
}
// 获取危化品列表
const getWhpData = () => {
  whpData.value = []
  getApi({}, '/mosty-jcgl/wxwp/listDisplay').then((res) => {
    whpData.value = res ? res : []

  })
}
// 获取危化品详情
const getDetails = () => {
  detailData.value = []
  getApi(listQuery.value, '/mosty-jcgl/wxwp/details').then((res) => {
    detailData.value = res.list ? res.list : []
  })
}
//详情按钮
const handleClick = (index, row) => {
  var val = row.cityCode;
  listQuery.value.code = val
  popForm.value = true; //显示返回按钮
  getDetails();
};

//点击返回
const backbtn = () => {
  popForm.value = false;
  getWhpData()
};
const exportExcels = () => {
  excelLoading.value = true
  let title = {
    orgName: '企业名称',
    species: "种类",
  };
  const param = listQuery.value
  let data = structuredClone(whpData.value);
  let obj = {
    orgName: '合计',
    species: 0,
  }
  if (data) {
    for (let i = 0; i < data.length; i++) {
      obj.species += data[i].species;
    }
    data.push(obj)
    let tablename = "危化品台账.xlsx";
    exportXlsx(title, data, '危化品台账', tablename);
    excelLoading.value = false
  }
  // getApi(param, "/mosty-jcgl/basicData/tzsbList").then((res) => {
  //   if (res) {

  //     excelLoading.value = false
  //   }
  // }).catch(() => {
  //   excelLoading.value = false
  // });
};
</script>

<style lang="scss" scoped>
@import "~@/assets/css/element-plus.scss";
</style>
