<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import ContractForm from './ContractForm.vue'
import { useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import TableBase from '@/components/TableBase/index.vue'
import { Edit, Delete, View } from '@element-plus/icons-vue'
import type { ColumnProps } from '@/components/TableBase/interface/index'
import { Storehouse_Contract_Del, Storehouse_Contract_User_List } from '@/api/storehouse/index'
import { useTablePublic, options } from '@/hooks/useTablePublic'

const router = useRouter()
const TableRef = ref<InstanceType<typeof TableBase> | null>(null)
const ContractFormRef = ref<InstanceType<typeof ContractForm> | null>(null)
const { globalStore, searchOnTableList, updateOnTableList } = useTablePublic()

const columns: ColumnProps[] = [
  { type: 'index', label: '序号', width: 80 },
  { prop: 'T_number', label: '合同编号' },
  { prop: 'T_customer', label: '客户名称' },
  { prop: 'T_State', label: '状态', name: 'T_State' },
  { prop: 'T_date', label: '更新时间' },
  { prop: 'operation', label: '操作', width: 260, fixed: 'right' }
]
const openContractFormDrawer = (type: string, row?: any) => ContractFormRef.value?.openDrawer(type, row)
const processContract = (id: string) => router.push({ name: 'ContractDetail', params: { id } })
const UserDelete = (row: any) => {
  ElMessageBox.confirm('您确定要删除该销售合同吗?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res: any = await Storehouse_Contract_Del({ User_tokey: globalStore.GET_User_tokey, T_number: row.T_number })
      if (res.Code === 200) {
        ElMessage.success('删除成功！')
        nextTick(() => {
          updateOnTableList(TableRef.value)
        })
      }
    })
    .catch(() => {
      ElMessage.warning('取消成功！')
    })
}
const initParam = reactive({
  User_tokey: globalStore.GET_User_tokey,
  T_name: '',
  T_state: ''
})
</script>

<template>
  <div class="contract-sale">
    <TableBase ref="TableRef" :columns="columns" :requestApi="Storehouse_Contract_User_List" :initParam="initParam">
      <template #table-header>
        <div class="input-suffix">
          <el-row :gutter="20" style="margin-bottom: 0">
            <el-col :xl="6" :lg="8" :md="10">
              <span class="inline-flex items-center">合同编号：</span>
              <el-input
                v-model="initParam.T_name"
                type="text"
                class="w-50 m-2"
                placeholder="按合同编号搜索"
                @change="searchOnTableList(TableRef)"
              />
            </el-col>
            <el-col :xl="10" :md="12">
              <span class="inline-flex items-center">状态：</span>
              <el-select v-model="initParam.T_state" class="w-50" clearable placeholder="请选择状态~">
                <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
              <el-button type="primary" @click="searchOnTableList(TableRef)">搜索</el-button>
            </el-col>
            <el-col :xl="6" :md="2" class="btn"
              ><el-button type="primary" @click="openContractFormDrawer('new')">添加</el-button></el-col
            >
          </el-row>
        </div>
      </template>
      <template #T_State="{ row }">
        <el-tag v-if="row.T_State === 1" type="success" effect="dark"> 已通过 </el-tag>
        <el-tag v-else-if="row.T_State === 2" type="warning" effect="dark"> 未通过 </el-tag>
        <el-tag v-else type="danger" effect="dark"> 待审核 </el-tag>
      </template>
      <template #T_out="{ row }">
        <el-tag v-if="row.T_out === 2 || row.T_out === 3" type="success" effect="dark">
          {{ row.T_out === 2 ? '已出库' : '已全部出库' }}
        </el-tag>
        <el-tag v-else-if="row.T_State === 1" type="warning" effect="dark"> 未出库 </el-tag>
        <el-tag v-else type="danger" effect="dark"> --- </el-tag>
      </template>
      <template #right="{ row }">
        <el-button
          :disabled="row.T_State === 1"
          link
          type="primary"
          size="small"
          :icon="Edit"
          @click="openContractFormDrawer('edit', row)"
          >编辑</el-button
        >
        <el-button link type="success" size="small" :icon="View" @click="processContract(row.T_number)">详情</el-button>
        <el-button :disabled="row.T_State === 1" link type="danger" size="small" :icon="Delete" @click="UserDelete(row)"
          >删除</el-button
        >
      </template>
    </TableBase>
    <ContractForm ref="ContractFormRef" @onTableList="updateOnTableList(TableRef)" />
  </div>
</template>

<style scoped lang="scss">
@import '@/styles/var.scss';

.contract-sale {
  @include f-direction;

  :deep(.el-drawer__header) {
    margin-bottom: 0;
  }
  .input-suffix {
    width: 100%;
    .inline-flex {
      white-space: nowrap;
    }
    .w-50 {
      width: 12.5rem;
    }
  }
}
</style>
