<template>
  <div class="vue-element-page-wrap" ref="tableContainer">
    <el-row class="page-search" justify="space-between" align="bottom">
      <el-form ref="queryFormRef" :model="queryParams.params" :inline="true" label-width="80px" label-position="left">
        <el-row :gutter="20">
          <el-col :span="6" class="col_height">
            <el-form-item label="到货单类型" prop="deliveryType">
              <el-select filterable v-model="queryParams.params.deliveryType" placeholder="到货单类型">
                <el-option v-for="item in deliveryType" :key="item.key" :label="item.label" :value="item.key" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="col_height">
            <el-form-item label="到货单单号" prop="deliveryName">
              <el-Input v-model="queryParams.params.deliveryName" placeholder="输入到货单单号" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6" class="col_height">
            <el-form-item label="到货单状态" prop="deliveryState">
              <el-select filterable v-model="queryParams.params.deliveryState" placeholder="到货单状态">
                <el-option v-for="item in deliveryState" :key="item.key" :label="item.label" :value="item.key" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="page-search-btns">
        <el-button type="primary" @click="queryInfo">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </div>
    </el-row>
    <!-- table 区域-->
    <div class="vue-element-page-table">
      <div class="page-table-operate">
        <div class="page-table-title">
          <div class="page-table-title-left">
            <span>到货单管理</span>
          </div>
        </div>
        <div class="page-table-operateBtns">
          <el-popover :persistent="false" placement="right" popper-class="config-table-wrap" trigger="click">
            <template #reference>
              <el-button class="operateBtns-setting">
                <svg-icon icon-class="table_title" width="16px" height="16px" />
              </el-button>
            </template>
            <ConfigTable :list="tableConfig.column" @updateList="updateList" />
          </el-popover>
        </div>
      </div>
      <el-table :height="tableHeight" border v-loading="loading"
                :data="state.dataList" highlight-current-row row-key="id" style="width: 100%"
                @selection-change="handleSelection">
        <el-table-column type="selection" width="55" />
        <template v-for="(col, index) in tableConfig.column" :key="index">
          <el-table-column v-if="!col.hide" :label="col.header" :prop="col.field" show-overflow-tooltip
                           :sort-orders="['descending', 'ascending']" :sortable="true">
            <template #default="scope">
              <span v-if="col.field === 'deliveryType'">
                <span v-for="(item, index) in deliveryType" :key="index">
                  <span v-if="item.key === scope.row[col.field]" >{{ item.label }}</span>
                </span>
              </span>
              <span v-else-if="col.field === 'deliveryState'">
                <span v-for="(item, index) in deliveryState" :key="index">
                  <span v-if="item.key === scope.row[col.field]" >{{ item.label }}</span>
                </span>
              </span>
              <span v-else>{{ scope.row[col.field] }}</span>
            </template>
          </el-table-column>
        </template>

        <el-table-column width="80" label="操作" align="center" class-name="table-operation" fixed="right">
          <template #default="scope">
            <el-button type="danger" link @click.stop="handleDel(scope.row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" :total="total" v-model:pageNumTo="queryParams.pageNum"
                  v-model:pageSizeTo="queryParams.pageSize" @pagination="handleQuery" />
    </div>
  </div>
</template>
<script lang="ts">
export default {
  name: 'deliveryNote'
}
</script>
<script lang="ts" setup>
import { reactive, ref, onMounted, toRefs, getCurrentInstance } from 'vue'
import { ElForm, ElMessageBox, ElTable } from 'element-plus'
import { localStorage } from '@/utils/storage'
import { bTableHeight } from '@/composables/calcTableHeight'
import { getDeliveryList } from '@/api/delivery'
import { delDelivery } from '@/api/delivery'
// 公共变量
const { tableContainer, tableHeight, updateTableHeight, handleResize } = bTableHeight(310)
const { proxy }: any = getCurrentInstance()
const tableConfig = ref({
  loading: false,
  column: [] as any,
  data: []
})
const queryFormRef = ref(ElForm)
// 变量
const state = reactive({
  dataList: [] as any, // 表格数据
  total: 0, // 总数
  selection: [] as any, // 多选
  loadings: false, // 更新按钮
  loading: true, // 表格
  // 到货单类型
  deliveryType: [{label: '批次到货单', key: '01'}, {label: '辅材到货单', key: '02'}],
  deliveryState: [{label: '新建', key: '01'}, {label: '进行中', key: '02'}, {label: '结束', key: '03'}],
  // 查询参数
  queryParams: {
    params: {
      siteName: localStorage.get('siteName') || 'SDK',
      // 到货单名称
      deliveryName: '',
      // 到货单类
      deliveryType: '',
      // 到货单状态
      deliveryState: ''
    },
    pageNum: 1,
    pageSize: 10
  }
})
const { queryParams, total, loading, deliveryType, deliveryState } = toRefs(state)

// 加载
onMounted(() => {
  tableConfig.value.column = [
    { header: '工厂', field: 'siteName', hide: false },
    { header: '到货单号', field: 'deliveryName', hide: false },
    { header: '到货单类型', field: 'deliveryType', hide: false },
    { header: '到货单状态', field: 'deliveryState', hide: false }
  ]
  handleQuery()
  updateTableHeight()
  window.addEventListener('resize', handleResize)
})
// 搜索
function queryInfo() {
  state.queryParams.pageNum = 1
  handleQuery()
}
// 表格事件
const updateList = (col: any) => {
  tableConfig.value.column = col
}
// 多选
const handleSelection = (val: any) => {
  state.selection = val
}
// 删除确认
function handleDel(row: any) {
  ElMessageBox.confirm('确认删除?', '提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      distinguishCancelAndClose: true,
      type: 'warning',
  }).then(() => {
    delDelivery(row).then((res: any) => {
      if (res.errorCode == 200) {
        proxy.$ElMessage.success(res.message)
        queryInfo()
      } else {
        proxy.$ElMessage.error(res.message)
      }
    })
  }).catch(() => {});
}
// 查询
async function handleQuery() {
  state.loading = true
  await getDeliveryList(state.queryParams).then((res: any) => {
    state.dataList = res.resultObj.list
    state.total = res.resultObj.total
    state.loading = false
  })
}
// 重置
function resetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}
</script>
<style>
.col_height {
  padding-top: 3.5px;
  padding-bottom: 3.5px;
}
</style>