<template>
  <el-dialog
    v-dialogDrag
    title="货位"
    :visible.sync="viewOpen"
    width="1200px"
    append-to-body
    @open="dialogOpen"
  >

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="98px"
    >
      <SearchBar :show-span="12" :collapse-span="6">
        <template #header>
          <el-col :span="6">
            <el-form-item label="车辆信息编号" prop="code">
              <el-input
                v-model="queryParams.code"
                placeholder="请输入车辆信息编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="供应商编号" prop="supplierCode">
              <el-input
                v-model="queryParams.supplierCode"
                placeholder="请输入关联供应商编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="车牌号" prop="plate">
              <el-input
                v-model="queryParams.plate"
                placeholder="请输入车牌号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #body>
          <el-col :span="6">
            <el-form-item label="联系人" prop="person">
              <el-input
                v-model="queryParams.person"
                placeholder="请输入联系人"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="车型" prop="models">
              <el-input
                v-model="queryParams.models"
                placeholder="请输入车型"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系电话" prop="phone">
              <el-input
                v-model="queryParams.phone"
                placeholder="请输入联系电话"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #buttons>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </template>
      </SearchBar>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:supplier-car:create']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:supplier-car:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      size="mini"
    >
      <el-table-column
        label="车辆信息编号"
        align="center"
        prop="code"
        width="120"
      />
      <el-table-column
        label="供应商编号"
        align="center"
        prop="supplierCode"
        width="140"
      />
      <el-table-column
        label="车牌号"
        align="center"
        prop="plate"
        width="120"
      />
      <el-table-column label="联系人" align="center" prop="person" />
      <el-table-column label="车型" align="center" prop="models" />
      <el-table-column
        label="联系电话"
        align="center"
        prop="phone"
        width="120"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:supplier-car:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['wms:supplier-car:delete']"
            size="mini"
            type="text"
            class="text-danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      v-dialogDrag
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="98px"
      >
        <el-form-item label="车辆信息编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入车辆信息编号" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-input v-model="queryParams.supplierName" disabled placeholder="请输入关联供应商主键id" />
        </el-form-item>
        <el-form-item label="供应商编号" prop="supplierCode">
          <el-input v-model="queryParams.supplierCode" disabled placeholder="请输入关联供应商编号" />
        </el-form-item>
        <el-form-item label="车牌号" prop="plate">
          <el-input v-model="form.plate" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="联系人" prop="person">
          <el-input v-model="form.person" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="车型" prop="models">
          <el-input v-model="form.models" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            :autosize="{ minRows: 4, maxRows: 6}"
            type="textarea"
            maxlength="200"
            show-word-limit
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { createSupplierCar, updateSupplierCar, deleteSupplierCar, getSupplierCar, getSupplierCarPage, exportSupplierCarExcel } from '@/api/wms/base/supplierCar'

export default {
  name: 'SupplierCar',
  components: {
  },
  data() {
    return {

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        code: null,
        supplierId: null,
        supplierCode: null,
        supplierName: null,
        plate: null,
        person: null,
        models: null,
        phone: null,
        remark: null,
        createTime: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getSupplierCarPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        code: undefined,
        supplierId: undefined,
        supplierCode: undefined,
        plate: undefined,
        person: undefined,
        models: undefined,
        phone: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加车辆信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getSupplierCar(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改车辆信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        // 修改的提交
        this.form.supplierCode = this.code
        if (this.form.id != null) {
          updateSupplierCar(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
          return
        }
        this.form.supplierId = this.id
        // 添加的提交
        this.form.supplierCode = this.code
        createSupplierCar(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除车辆信息编号为"' + id + '"的数据项?').then(function() {
        return deleteSupplierCar(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有车辆信息数据项?').then(() => {
        this.exportLoading = true
        return exportSupplierCarExcel(params)
      }).then(response => {
        this.$download.excel(response, '车辆信息.xls')
        this.exportLoading = false
      }).catch(() => {})
    },
    dialogOpen() {
      this.getList()
    }
  }
}
</script>
