<template>
  <div class="app-container">

    <PageTitle>
      <template #buttons>
        
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="medium"
            @click="handleAdd"
            v-hasPermi="['inventory:owners:add']"
          >新增</el-button>
        
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="medium"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['inventory:owners:edit']"
          >修改</el-button>
        
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="medium"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['inventory:owners:remove']"
          >删除</el-button>
        
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="medium"
            @click="handleExport"
            v-hasPermi="['inventory:owners:export']"
          >导出</el-button>
        
          <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="medium"
            @click="handleImport"
            v-hasPermi="['inventory:owners:add']"
          >导入</el-button>

       
      </template>
    </PageTitle>

    <div class="page-container">
      <page-wrapper-search 
        :model="queryParams" 
        ref="queryForm" 
        size="small" 
        @search="handleQuery"
        @reset="resetQuery"
      >
        <el-form-item label="货主编码" prop="ownerCode">
          <el-input
            v-model="queryParams.ownerCode"
            placeholder="请输入货主编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="货主名称" prop="ownerName">
          <el-input
            v-model="queryParams.ownerName"
            placeholder="请输入货主名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- 新增英文名称查询项 -->
        <el-form-item label="英文名称" prop="englishName">
          <el-input
            v-model="queryParams.englishName"
            placeholder="请输入英文名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="税号" prop="taxNumber">
          <el-input
            v-model="queryParams.taxNumber"
            placeholder="请输入税号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
    
        <el-form-item label="是否激活" prop="isActive">
          <el-select
            v-model="queryParams.isActive"
            placeholder="请选择是否激活"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </page-wrapper-search>
      <div class="table-container">
        <el-table v-loading="loading" :data="ownersList" height="100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" fixed />
          <el-table-column label="货主编码" align="center" prop="ownerCode" width="120" fixed/>
          <el-table-column label="货主名称" align="center" prop="ownerName"width="220" />
          <!-- 新增英文名称表格列 -->
          <el-table-column label="英文名称" align="center" prop="englishName"width="220" />
          <el-table-column label="货主类型" align="center" prop="ownerType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.owner_type" :value="scope.row.ownerType"/>
            </template>
          </el-table-column>
          <el-table-column label="联系人" align="center" prop="contactPerson" />
          <el-table-column label="联系电话" align="center" prop="contactPhone" width="120"/>
          <el-table-column label="邮箱" align="center" prop="email" width="200"/>
          <el-table-column label="地址" align="center" prop="address" width="220"/>
          <el-table-column label="税号" align="center" prop="taxNumber"width="180" />
          <el-table-column label="银行账户" align="center" prop="bankAccount" width="180"/>
          <el-table-column label="是否激活" align="center" prop="isActive">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.yes_no" :value="scope.row.isActive"/>
            </template>
          </el-table-column>
         
          <el-table-column label="创建日期" align="center" prop="createTime" width="160" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['inventory:owners:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['inventory:owners:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>


    
      
    <!-- 添加或修改货主信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="货主编码" prop="ownerCode">
              <el-input v-model="form.ownerCode" placeholder="请输入货主编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货主名称" prop="ownerName">
              <el-input v-model="form.ownerName" placeholder="请输入货主名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 新增英文名称表单项 -->
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="英文名称" prop="englishName">
              <el-input v-model="form.englishName" placeholder="请输入英文名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
        <el-col :span="12">
            <el-form-item label="货主类型" prop="ownerType">
              <el-select v-model="form.ownerType" placeholder="请选择货主类型" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.owner_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        <el-col :span="12">
            <el-form-item label="是否激活" prop="isActive">
              <el-radio-group v-model="form.isActive" size="small">
                <el-radio-button label="1">激活</el-radio-button>
                <el-radio-button label="0">未激活</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税号" prop="taxNumber">
              <el-input v-model="form.taxNumber" placeholder="请输入税号" />
            </el-form-item>
          </el-col>
        </el-row>
            <el-row :gutter="20">
          
          <el-col :span="12">
            <el-form-item label="银行账户" prop="bankAccount">
              <el-input v-model="form.bankAccount" placeholder="请输入银行账户" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

     <import-excel
        ref="import"
        title="导入"
        import-url="/inventory/owners/import"
        template-url="inventory/owners/importTemplate"
        template-name="owners_importTemplate"
        @success="getList"
      />

  </div>
</template>
<script>
  
import { listOwners, getOwners, delOwners, addOwners, updateOwners } from "@/api/inventory/owners"
import ImportExcel from "@/components/ImportExcel/index"

export default {
  name: "Owners",
  components: { ImportExcel },
  dicts: ['yes_no', 'owner_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货主信息表格数据
      ownersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ownerCode: null,
        ownerName: null,
        englishName: null, // 新增英文名称查询参数
        ownerType: null,
        contactPerson: null,
        contactPhone: null,
        email: null,
        address: null,
        taxNumber: null,
        bankAccount: null,
        isActive: null,
        sortNo: null,
        createUserCode: null,
        updateUserCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ownerCode: [
          { required: true, message: '货主编码不能为空', trigger: 'blur' }
        ],
        ownerName: [
          { required: true, message: '货主名称不能为空', trigger: 'blur' }
        ],
        englishName: [ // 新增英文名称校验规则（非必填，可根据需求调整）
          { required: false, message: '英文名称不能为空', trigger: 'blur' }
        ],
        contactPerson: [
          { required: true, message: '联系人不能为空', trigger: 'blur' }
        ],
        ownerType: [
          { required: true, message: '货主类型不能为空', trigger: 'blur' }
        ],
        contactPhone: [
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码格式', trigger: 'blur' }
        ],
        email: [
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        isActive: [
          { required: true, message: '是否激活不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询货主信息列表 */
    getList() {
      this.loading = true
      listOwners(this.queryParams).then(response => {
        this.ownersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ownerCode: null,
        ownerName: null,
        englishName: null, // 新增英文名称表单重置
        ownerType: null,
        contactPerson: null,
        contactPhone: null,
        email: null,
        address: null,
        taxNumber: null,
        bankAccount: null,
        isActive: null,
        sortNo: null,
        createTime: null,
        createUserCode: null,
        updateTime: null,
        updateUserCode: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
       this.queryParams={
        pageNum: 1,
        pageSize: 10,
        ownerCode: null,
        ownerName: null,
        englishName: null, // 新增英文名称查询参数重置
        ownerType: null,
        contactPerson: null,
        contactPhone: null,
        email: null,
        address: null,
        taxNumber: null,
        bankAccount: null,
        isActive: null,
        sortNo: null,
        createUserCode: null,
        updateUserCode: null
      },
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加货主信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getOwners(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改货主信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOwners(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOwners(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm(row.id?`是否确认删除货主信息编号为"${row.ownerCode}"的数据项？`: `是否确认删除选中的${ids.length}条货主信息数据项？`).then(function() {
        return delOwners(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('inventory/owners/export', {
        ...this.queryParams
      }, `owners_${new Date().getTime()}.xlsx`)
    }, 
    /** 导入组件 */
    handleImport() {
       this.$refs.import.show()
    }
  }
}
</script>