<template>
  <div>
    <div class="titleBox">
      <PageTitle title="标签布控" ></PageTitle>
    </div>
    <!-- 搜索 -->
    <div ref="searchBox">
      <Search :searchArr="searchConfiger" @submit="onSearch"></Search>
    </div>
    <!-- 表格 -->
    <div class="tabBox">
      <MyTable
        :tableData="pageData.tableData"
        :tableColumn="pageData.tableColumn"
        :tableHeight="pageData.tableHeight"
        :key="pageData.keyCount"
        :tableConfiger="pageData.tableConfiger"
        :controlsWidth="pageData.controlsWidth"
        @chooseData="chooseData"
      >
        <template #bkZt="{ row }">
          <DictTag :tag="false" :value="row.bkZt" :options="D_GS_BK_ZT" />
        </template>
        <template #bkDj="{ row }">
          <DictTag :tag="false" :value="row.bkDj" :options="D_GS_BK_DJ" />
        </template>
        <template #bkdxList="{ row }">
          <span v-if="row.bkdxList"><span class="nowrap" v-for="(it,idx) in row.bkdxList" :key="idx"> {{ it.ryXm }} <span v-if="idx < row.bkdxList.length-1">、</span></span></span>
          <span v-else>暂无</span>
        </template>
        <template #sjrs="{ row }">
          <span v-if="row.bkdxList"> {{ row.bkdxList.length }} </span>
           <span v-else>0</span>
        </template>
        <template #qyList="{ row }">
          <span v-for="(it,idx) in row.qyList" :key="idx"> {{ it.qymc }} 、</span>
        </template>
        <template #yjcs="{row}">
          <span @click="openWarning(row)" style="color: #00ffff;" class="mr5 pointer">{{ row.yjcs || 0}}</span>次
        </template>

        <!-- 操作 -->
        <template #controls="{ row }">
          <el-popover placement="left" :visible="row.visible" :width="400" trigger="manual">
            <template #reference>
              <el-link size="small" type="success" v-if="row.bkZt == '04'" @click="row.visible = !row.visible,chooseRow.id = row.id">审批</el-link>
            </template>
            <el-form :model="chooseRow" ref="elRowForm" v-if="row.visible" :inline="true" label-width="100px" :rules="rules">
              <el-form-item label="是否通过" prop="sftg" class="mt10 mb10" style="width: 100%;">
                <MOSTY.Select filterable v-model="chooseRow.sftg" :dictEnum="D_BZ_SF" width="100%" clearable  placeholder="请选择是否通过"/>
              </el-form-item>
              <el-form-item label="不通过原因" prop="bkspBtgyy" v-if="chooseRow.sftg == 0" style="width: 100%;">
                <MOSTY.Other style="width: 100%;" clearable v-model="chooseRow.bkspBtgyy" type="textarea" placeholder="请输入不通过原因"/>
              </el-form-item>
            </el-form>
            <div class="flex just-center mt10">
                <el-button @click.stop="cancelRow(row)">取消</el-button>
                <el-button type="primary"  @click.stop="handleSend(row)"  v-loading="btnloading">确定</el-button>
            </div>
          </el-popover>

          <el-link type="primary" size="small"  @click="handleAdd('detail',row)">详情</el-link>
        </template>
      </MyTable>
      <Pages
        @changeNo="changeNo"
        @changeSize="changeSize"
        :tableHeight="pageData.tableHeight + 42"
        :pageConfiger="{
          ...pageData.pageConfiger,
          total: pageData.total
        }"
      ></Pages>
    </div>
  </div>
</template>

<script setup>
import * as MOSTY from "@/components/MyComponents/index";
import PageTitle from "@/components/aboutTable/PageTitle.vue";
import MyTable from "@/components/aboutTable/MyTable.vue";
import Pages from "@/components/aboutTable/Pages.vue";
import Search from "@/components/aboutTable/Search.vue";
import { qcckGet, qcckPost } from "@/api/qcckApi.js";
import { reactive, ref, onMounted, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
const { D_BZ_SF,D_GS_BK_BKYS,D_BZ_XB,D_GS_BK_SJLX,D_GS_BK_DJ, D_GS_BK_DX, D_GS_BK_ZT,D_GS_BK_CZYQ,D_GS_BK_CZJSDWLX,D_GS_BK_TJFS } = proxy.$dict("D_BZ_SF","D_GS_BK_BKYS","D_BZ_XB","D_GS_BK_SJLX","D_GS_BK_DJ","D_GS_BK_DX","D_GS_BK_ZT","D_GS_BK_CZYQ","D_GS_BK_CZJSDWLX","D_GS_BK_TJFS"); //获取字典数据
const searchBox = ref();
const chooseRow = ref({})
const btns = ref();
const elRowForm = ref()
const rules = reactive({
  sftg: [{ required: true, message: "请选择是否通过", trigger: "change" }],
  bkspBtgyy: [{ required: true, message: "请输入不通过原因", trigger: "blur" }]
});
const btnloading = ref(false)
const addBkdxForm = ref(null); //布控对象组件
const searchConfiger = ref([
  {
    label: "任务名称",
    prop: "rwmc",
    placeholder: "请输入任务名称",
    showType: "input",
  },
]);
const queryFrom = ref({});
const pageData = reactive({
  tableData: [], //表格数据
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "null",
    loading: false
  },
  total: 0,
  pageConfiger: {
    pageSize: 20,
    pageCurrent: 1
  }, //分页
  controlsWidth: 120, //操作栏宽度

  tableColumn: [
    { label: "任务名称", prop: "rwmc" },
    { label: "布控人电话", prop: "bkysDh" },
    { label: "人员等级", prop: "bkysDj" },
    { label: "处置措施", prop: "czcs" },
    { label: "布控事由", prop: "bkspBtgyy" },
  ]
});
onMounted(() => {
  tabHeightFn();
  getList()
});

// 搜索
const onSearch = (val) => {
  queryFrom.value = { ...val };
  pageData.pageConfiger.pageCurrent = 1;
  getList();
};

const changeNo = (val) => {
  pageData.pageConfiger.pageCurrent = val;
  getList();
};
const changeSize = (val) => {
  pageData.pageConfiger.pageSize = val;
  getList();
};

// 获取列表
const getList = () => {
//   pageData.tableConfiger.loading = true;
//   let data = {
//     ...pageData.pageConfiger,
//     ...queryFrom.value,
//   };
//   qcckGet(data, "/mosty-gsxt/tbGsxtBk/selectPage").then((res) => {
//     pageData.tableData = res.records || [];
//     pageData.total = res.total;
//     pageData.tableConfiger.loading = false;
//   }).catch(() => {
//     pageData.tableConfiger.loading = false;
//   });
};

const handleAdd = (type,row) => {
  addBkdxForm.value.init(type,row);
};
const cancelRow = (row) => {
  row.visible = false;
  chooseRow.value = {};
  btnloading.value = false;
  elRowForm.value.resetFields()
};

// 审核
const handleSend = (val) =>{
  elRowForm.value.validate((valid) => {
    if(!valid) return;
    btnloading.value = true;
    qcckPost(chooseRow.value, "/mosty-gsxt/tbGsxtBk/audits").then(() => {
      proxy.$message({ type: "success", message: "审批成功" });
      val.visible = false;
      btnloading.value = false;
      chooseRow.value = {};
      getList();
    }).catch(()=>{
      btnloading.value = false;
    });
  })
}

// 表格高度计算
const tabHeightFn = () => {
  pageData.tableHeight = window.innerHeight - searchBox.value.offsetHeight - 300;
  window.onresize = function () {
    tabHeightFn();
  };
};
</script>

<style lang="scss" scoped>
.main-nav {
  padding: 16px;
  background-color: #fff;
}


.sub-nav {
  background-color: #fff;
  padding: 0 16px;
}

:deep(.el-tabs__header) {
  margin-bottom: 0;
}

:deep(.el-tabs__nav-wrap::after) {
  height: 1px;
  background: none;
}

:deep(.el-table--fit) {
  top: 52px !important;
}
.btns {
  height: 52px;
  padding: 10px;
  box-sizing: border-box;
}

</style>
