<template>
  <div class="customer-css">
    <el-form :inline="true" label-position="top" :model="searchData">
      <el-form-item label="始发港">
        <el-input v-model="searchData.departure" clearable style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="目的港">
        <el-input v-model="searchData.destination" clearable style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="searchTable()">查询</el-button>
        <el-button type="primary" @click="addModal()" v-if="shouldShowButton">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      :height="searchData.height"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        v-for="(item, index) in columnList"
        :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed == '' ? false : item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="180"
        fixed="right"
        label="Actions">
        <template slot-scope="scope">
          <a type="text" size="small" @click="editModel(scope.row)">Edit |</a>
          <a type="text" size="small" @click="deleteData(scope.row)"> Delete</a>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑弹窗 -->
    <el-dialog :title="dialogTitle" :close-on-click-modal="false" v-drag :visible.sync="dialogVisible" width="600px">
      <div class="dialog-content">
        <el-form label-position="top" :model="formData" :rules="formRules" ref="formRef">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="始发港" prop="departure">
                <el-input v-model="formData.departure" placeholder="请输入始发港"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="目的港" prop="destination">
                <el-input v-model="formData.destination" placeholder="请输入目的港"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="停留天数" prop="transitDays">
                <el-input v-model="formData.transitDays" type="number" placeholder="请输入停留天数"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: 20px">
        <el-button type="primary" @click="submitData()">保存</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 请根据实际API路径调整导入
import {
  searchTransitPage, // 分页查询运输周期列表（如果需要分页）
  searchTransitList,   // 查询运输周期列表
  createTransit,    // 新增运输周期
  updateTransit,    // 更新运输周期
  deleteTransit     // 删除运输周期
} from '@/api/port/portTransit.js'

export default {
  name: 'PortTransitList',
  data() {
    return {
      dataList: [],
      dataListLoading: false,
      searchData: {
        departure: '',
        destination: '',
        portId: '',
        portCode: '', 
        height: '200',
        page: 1,
        limit: 1000
      },
      dialogVisible: false,
      dialogTitle: '新增港口运输周期',
      formData: {
        id: '',
        departure: '',
        destination: '',
        transitDays: ''
      },
      formRules: {
        departure: [{ required: true, message: '请输入始发港', trigger: 'blur' }],
        destination: [{ required: true, message: '请输入目的港', trigger: 'blur' }],
        transitDays: [{ required: true, message: '请输入停留天数', trigger: 'blur' }]
      },
      columnList: [
        { columnProp: 'departure', headerAlign: 'center', align: 'left', columnLabel: '始发港', columnWidth: 150, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'destination', headerAlign: 'center', align: 'left', columnLabel: '目的港', columnWidth: 150, fixed: '', columnHidden: false, columnSortable: false },
        { columnProp: 'transitDays', headerAlign: 'center', align: 'center', columnLabel: '停留天数', columnWidth: 120, fixed: '', columnHidden: false, columnSortable: true }
      ]
    }
  },
  computed: {
    shouldShowButton() {
      return true
    }
  },
  methods: {
    // 初始化方法，供父组件调用
    init(inData) {
      if (inData) {
        this.searchData = { ...this.searchData, ...inData }
      }
      this.searchTable()
    },
    // 查询列表
    searchTable() {
      this.dataListLoading = true
      searchTransitPage(this.searchData).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.rows
        } else {
          this.dataList = []
        }
        this.dataListLoading = false
      }).catch(() => {
        this.dataListLoading = false
      })
    },
    // 新增
    addModal() {
      this.dialogTitle = '新增港口运输周期'
      this.formData = {
        id: '',
        portId: this.searchData.portId || '',
        departure: '',
        destination: '',
        transitDays: ''
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        if (this.$refs.formRef) {
          this.$refs.formRef.clearValidate()
        }
      })
    },
    // 编辑
    editModel(row) {
      this.dialogTitle = '编辑港口运输周期'
      this.formData = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.$nextTick(() => {
        if (this.$refs.formRef) {
          this.$refs.formRef.clearValidate()
        }
      })
    },
    // 提交保存
    submitData() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return
        
        const api = this.formData.id ? updateTransit : createTransit
        api(this.formData).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message.success(data.msg || '保存成功')
            this.dialogVisible = false
            this.searchTable()
          } else {
            this.$message.warning(data.msg || '保存失败')
          }
        }).catch(() => {
          this.$message.error('保存失败')
        })
      })
    },
    // 删除
    deleteData(row) {
      this.$confirm('确认删除该条港口运输周期记录吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTransit(row.id).then(({ data }) => {
          if (data.code === 0) {
            this.$message.success('删除成功')
            this.searchTable()
          } else {
            this.$message.warning(data.msg || '删除失败')
          }
        }).catch(() => {
          this.$message.error('删除失败')
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped lang="scss">
.dialog-content {
  width: 100%;
  max-height: 60vh;
  overflow-y: auto;
  padding-right: 10px;
}

.customer-css {
  padding: 10px;
}
</style>