<template>
  <div class="app-container">
    <el-form inline :model="queryParams">
      <el-form-item label="批量对象表名:">
        <el-input v-model="queryParams.bathObjTabName"></el-input>
      </el-form-item>
      <el-form-item label="批里对象字段名:">
        <el-input v-model="queryParams.bathObjFldName"></el-input>
      </el-form-item>
      <el-form-item label="词性名称:">
        <el-input v-model="queryParams.posName"></el-input>
      </el-form-item>
      <el-form-item label="状态:">
        <el-select v-model="queryParams.status" style="width: 200px" clearable>
          <el-option label="正常" key="tsmcb-query-status-1" value="1"></el-option>
          <el-option label="弃用" key="tsmcb-query-status-1" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Minus"
            @click="multDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="Upload"
            @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>
    <el-table :data="dataList" @selection-change="handleSelection">
      <el-table-column type="selection" width="50" align="center" />
<!--      <el-table-column label="序号" align="center" prop="onum"></el-table-column>-->
      <el-table-column label="批量对象表名" align="center" prop="bathObjTabName"></el-table-column>
      <el-table-column label="批里对象字段名" align="center" prop="bathObjFldName"></el-table-column>
      <el-table-column label="频次" align="center" prop="freq"></el-table-column>
      <el-table-column label="词性" align="center" prop="pos"></el-table-column>
      <el-table-column label="词性名称" align="center" prop="posName"></el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-switch v-model="scope.row.status"
                     active-value="1"
                     inactive-value="0" @change="changeStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="负责人员" align="center" prop="updateBy"></el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime">
        <template #default="scope">
          <span>{{scope.row.updateTime.replace("T"," ")}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" prop="operation">
        <template #default="scope">
          <el-link type="primary" @click="handleEdit(scope.row)" :underline="false">修改</el-link>
          <el-link type="danger"  style="margin-left: 10px" @click="handleDelete([scope.row.onum])" :underline="false">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <el-dialog
        v-model="showDialog"
        :title="currentForm.operate === 'ADD'?'新增词典配置表':'编辑词典配置表'"
        width="500"
    >
      <el-form v-if="showDialog"
               :model="currentForm"
               ref="formRef"
               :rules="rules"
               label-width="125px">
        <el-form-item label="批量对象表名:" prop="bathObjTabName">
          <el-input
              v-model="currentForm.bathObjTabName" clearable
          />
        </el-form-item>
        <el-form-item label="批量对象字段名:" prop="bathObjFldName">
          <el-input
              v-model="currentForm.bathObjFldName" clearable
          />
        </el-form-item>
        <el-form-item label="频次:" prop="freq">
          <el-input
              v-model="currentForm.freq" clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="词性:" prop="pos">
          <el-input
              v-model="currentForm.pos" clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="词性名称:" prop="posName">
          <el-input
              v-model="currentForm.posName" clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="状态:">
          <el-radio-group v-model="currentForm.status">
            <el-radio value="1" size="large">正常</el-radio>
            <el-radio value="0" size="large">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="handleSave">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, nextTick, computed, watch, reactive, onMounted } from 'vue'
import { getCdplbList, saveCdplb, deleteCdplb } from "@/api/dataint/cdplb"
const { proxy } = getCurrentInstance();

const queryParams = ref({
  bathObjTabName:'',
  bathObjFldName:'',
  posName:'',
  status:'',
  pageNum:1,
  pageSize:10
})
const showDialog = ref(false)
const rules = ref({
  bathObjTabName: [{required: true, message: '批量对象表名不能为空！', trigger: 'change',},],
  bathObjFldName: [{required: true, message: '批里对象字段名不能为空！！', trigger: 'change',},],
  pos: [{required: true, message: '词性不能为空！', trigger: 'change',},],
  posName: [{required: true, message: '词性名称不能为空！', trigger: 'change',},],
  freq: [{required: true, message: '频次不能为空！', trigger: 'change',},],
})
const currentForm = ref({
  operate: 'ADD',
  bathObjTabName:'',
  bathObjFldName:'',
  pos:'',
  posName:'',
  freq:'',
  status:'1'
})
const multSelection = ref([])
const dataList = ref([])
const total = ref(0)
function handleQuery(){
  queryParams.value.pageNum = 1;
  getList()
}
function getList(){
  getCdplbList(queryParams.value).then(res=>{
    dataList.value = res.data.rows
    total.value = res.data.total
  })
}
function resetQuery(){
  queryParams.value = {
    pos:'',
    type:'',
    stdRplStr:'',
    status:'',
    pageNum:1,
    pageSize:10
  }
  handleQuery()
}
function handleAdd(){
  showDialog.value = true
  currentForm.value ={
    operate: 'ADD',
    bathObjTabName:'',
    bathObjFldName:'',
    pos:'',
    posName:'',
    freq:'',
    status:'1'
  }
}
function handleSave(){
  proxy.$refs.formRef.validate(valid => {
    if (valid) {
      saveCdplb(currentForm.value).then(res=>{
        proxy.$modal.msgSuccess("操作成功");
        showDialog.value = false
        getList()
      })
    }
  });
}
function handleEdit(row){
  currentForm.value = JSON.parse(JSON.stringify(row))
  currentForm.value.operate = 'UPDATE'
  showDialog.value = true
}
function changeStatus(row){
  let param = {
    onum: row.onum,
    status: row.status
  }
  saveCdplb(param).then(res=>{
    proxy.$modal.msgSuccess("操作成功");
    getList()
  })
}
function multDelete(){
  let array = []
  if (multSelection.value.length>0){
    for (let i = 0; i < multSelection.value.length; i++) {
      array.push(multSelection.value[i].onum)
    }
    handleDelete(array)
  }else{
    proxy.$modal.msgWarning("请至少选择一条数据")
  }

}
function handleDelete(array){
  deleteCdplb(array).then(res=>{
    proxy.$modal.msgSuccess("操作成功");
    getList()
  })
}
function handleImport(){

}
function handleExport(){

}
function handleSelection(val){
  multSelection.value= val
}
onMounted(() => {
  handleQuery()
})
</script>
<style scoped lang="scss">

</style>