<template>
  <div>
    <div class="titleBox">
      <div class="title">巡访管理</div>
      <div class="btnBox">
        <el-button type="primary" @click="addEdit('add','')">
          <el-icon style="vertical-align: middle"><CirclePlus /> </el-icon>
          <span style="vertical-align: middle">新增</span>
        </el-button>
        <el-button :disabled="ids.length == 0" @click="handleDelete('')" typeof="danger">
          <el-icon style="vertical-align: middle"> <Delete /> </el-icon>
          <span style="vertical-align: middle">批量删除</span>
        </el-button>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="商户名">
          <el-input v-model="listQuery.shm" placeholder="请输入商户名"></el-input>
        </el-form-item>
        <el-form-item label="巡访人姓名">
          <el-input v-model="listQuery.xm" placeholder="请输入巡访人姓名"></el-input>
        </el-form-item>
        <el-form-item label="巡访部门">
          <MOSTY.Department width="100%" clearable v-model="listQuery.ssbmdm"/>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleFilter"> 查询 </el-button>
          <el-button @click="reset"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        :data="tableData"
        border
        row-key="id"
        @selection-change="handleSelectionChange"
        :tree-props="{ children: 'itemList', hasChildren: true }"
        style="width: 100%"
        :key="keyCount"
        :height="tableHeight"
        v-loading="loadingTable"
        element-loading-background="rgba(0,0,0,0.3)"
        element-loading-text="数据加载中。。"
      >
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column type="index" align="center" width="60px" label="序号"/>
        <el-table-column prop="shm" align="center" label="商户名" />
        <el-table-column prop="xm" align="center" label="巡访人姓名" />
        <el-table-column prop="sfzh" align="center" label="巡访人身份证" />
        <el-table-column prop="ssbm" align="center" label="所属部门" />
        <el-table-column prop="type" align="center" label="巡访类型">
          <template #default="{ row }">
             <dict-tag
              :options="D_BZ_XFLX"
              :value="row.type"
              :tag="false"
            ></dict-tag>
          </template>
          </el-table-column>
        <el-table-column prop="lxxl" align="center" label="商户细类">
          <template #default="{ row }">
             <dict-tag
              :options="D_BZ_SHLX"
              :value="row.lxxl"
              :tag="false"
            ></dict-tag>
          </template>
          </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template #default="{ row }">
            <el-button @click="addEdit('edit',row.id)" size="small">修改</el-button>
            <el-button @click="handleDelete(row.id)" type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.pageCurrent"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
    <div v-if="dialogFormVisible" class="dialog">
      <div class="head_box">
        <span class="title">{{ title }}</span>
        <div>
          <el-button type="primary" size="small" @click="submit" :loading="btnLoading" >保存</el-button>
          <el-button size="small" @click="closeDialog">关闭</el-button>
        </div>
      </div>
      <el-form ref="elform" :model="form" :rules="rules" :inline="true" label-position="top">
        <el-form-item style="width: 48%" prop="shm" label="商户名">
          <el-input v-model="form.shm" placeholder="请输入商户名"></el-input>
        </el-form-item>
        <el-form-item style="width: 48%" prop="ssbmdm" label="巡访部门">
          <MOSTY.Department width="100%" clearable v-model="form.ssbmdm"/>
        </el-form-item>
        <el-form-item style="width: 48%" prop="xm" label="巡访人姓名">
          <el-input v-model="form.xm" placeholder="请输入巡访人姓名"></el-input>
        </el-form-item>
        <el-form-item style="width: 48%" prop="sfzh" label="巡访人身份证">
          <el-input v-model="form.sfzh" placeholder="请输入巡访人身份证"></el-input>
        </el-form-item>
        <el-form-item style="width: 48%" prop="type" label="类型">
          <el-select v-model="form.type" placeholder="请选择类型" clearable>
            <el-option v-for="item in D_BZ_XFLX" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="width: 48%" prop="lxxl" label="商户细类">
          <el-select v-model="form.lxxl" placeholder="请选择商户细类" clearable>
            <el-option v-for="item in D_BZ_SHLX" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="详细地址" style="width: 94%">
          <el-input v-model="form.xxdz" placeholder="请输入关键字" show-word-limit type="textarea" />
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { qcckPost, qcckGet,qcckPut,qcckDelete } from "@/api/qcckApi.js";
import * as MOSTY from "@/components/MyComponents/index";
import * as rule from "@/utils/rules.js";
import { ref, reactive, onMounted, onUnmounted, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
const { D_BZ_SHLX, D_BZ_XFLX } = proxy.$dict("D_BZ_SHLX", "D_BZ_XFLX");
const tableData = ref([]); //表格数据
const searchBox = ref(null); // 搜索盒子
const tableHeight = ref(); // 表格高度
const btnLoading = ref(false); //按钮截流
const keyCount = ref(0); //tabel组件刷新值
const title = ref("新增必巡线");
const loadingTable = ref(false);
const listQuery = ref({ pageCurrent: 1, pageSize: 20 }); //搜索数据
const elform = ref(null);
const ids = ref([]); //批量删除的ID
const total = ref(0);
const dialogFormVisible = ref(false); //弹窗
const form = ref({}); //表单数据
//表单验证
const rules = reactive({
  shm: [{ required: true, message: "请输请输入商户名", trigger: "blur" }],
  ssbmdm: [{ required: true, message: "请选择巡访部门", trigger: "change" }],
  xm: [{ required: true, message: "请输入巡访人姓名", trigger: "blur" }],
  type: [{ required: true, message: "请选择商户类型", trigger: "change" }],
  lxxl: [{ required: true, message: "请选择商户细类", trigger: "change" }],
  ...rule.identityCardRule( {require: true,trigger: ['blur','change'], message: "请输入身份证号",validator: true},"sfzh"), //身份证校验
});
onMounted(() => {
  getListData();
  proxy.mittBus.on("mittFn", (data) => { keyCount.value = data; });
  tabHeightFn();
  window.onresize = function () { tabHeightFn(); };
});
//搜索
function handleFilter() {
  listQuery.value.pageCurrent = 1;
  getListData();
}
//重置
function reset() {
  listQuery.value = { pageCurrent: 1, pageSize: 20 };
  getListData();
}

//获取列表数据
function getListData() {
    loadingTable.value = true;
    qcckGet(listQuery.value,'/mosty-jcgl/tbJcglXf').then((res) => {
      tableData.value = res.records;
      total.value = res.total;
      loadingTable.value = false;
    }).catch(()=>{
      loadingTable.value = false;
    });
}
//新增
function addEdit(type, id) {
  dialogFormVisible.value = true;
  if (type == "add") {
    title.value = "新增巡访管理";
  } else {
    title.value = "修改巡访管理";
    qcckGet({},'/mosty-jcgl//tbJcglXf/'+id).then((res) => {
      form.value = JSON.parse(JSON.stringify(res))
    })
  }
}

//批量数据选择的数据
const handleSelectionChange = (val) => {
  let list = val || [];
  ids.value = list.map((item) => {
    return item.id;
  });
};

//批量删除-单个删除
function handleDelete(id) {
  proxy.$confirm("确定要删除", "警告", { type: "warning" }).then(() => {
    let delIds = id ? [id] : ids.value;
    qcckDelete(delIds,'/mosty-jcgl/tbJcglXf/bacth').then(res=>{
      proxy.$message({ message: "删除成功", type: "success" });
      handleFilter();
    })
  });
}

//关闭弹窗
function closeDialog() {
  dialogFormVisible.value = false;
  btnLoading.value = false;
}
//提交
function submit() {
  elform.value.validate((valid) => {
    if (valid) {
      btnLoading.value = true;
      if (title.value == "修改巡访管理") {
        qcckPut(form.value,'/mosty-jcgl//tbJcglXf').then(() => {
          proxy.$message({ type: "success",  message: "修改成功" });
          closeDialog()
          getListData();
        }).catch(()=>{ btnLoading.value = false; });
      } else {
        qcckPost(form.value,'/mosty-jcgl/tbJcglXf').then(() => {
          proxy.$message({ type: "success", message: "新增成功" });
          closeDialog()
          getListData();
        }).catch(()=>{ btnLoading.value = false; });;
      }
    }
  });
}
// pageSize 改变触发
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  getListData();
};

// 页码改变触发
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageCurrent = currentPage;
  getListData();
};
// 表格高度计算
const tabHeightFn = () => {
  tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};
onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
</script>

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