<template>
  <el-dialog
    title="车辆选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="true"
    width="80%"
    center
    append-to-body
  >
    <el-row :gutter="20">
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="车辆名称" prop="vehicleName">
            <el-input
              v-model="queryParams.vehicleName"
              placeholder="请输入车辆名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="车牌号" prop="licensePlate">
            <el-input
              v-model="queryParams.licensePlate"
              placeholder="请输入车牌号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <el-table
          v-loading="loading"
          :data="vehicleList"
          @current-change="handleCurrent"
          @row-dblclick="handleRowDbClick"
        >
          <el-table-column width="50" align="center">
            <template v-slot="scope">
              <el-radio
                v-model="selectedVehicleId"
                :label="scope.row.vehicleId"
                @change="handleRowChange(scope.row)"
                >{{ "" }}</el-radio
              >
            </template>
          </el-table-column>
          <el-table-column label="车辆编码" align="center" prop="vehicleCode"/>
          <el-table-column label="车辆名称" align="center" prop="vehicleName" />
          <el-table-column label="车牌号" align="center" prop="licensePlate" />
          <el-table-column label="车型" align="center" prop="vehicleModel">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.vehicle_model"
                :value="scope.row.vehicleModel"
              />
            </template>
          </el-table-column>
          <el-table-column label="座位数" align="center" prop="seating" />
          <el-table-column label="购买时间" align="center" prop="purchaseDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.purchaseDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="验车时间" align="center" prop="inspectionDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.inspectionDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { listVehicle } from "@/api/mes/md/vehicle";

export default {
  name: "MdItemSingle",
  dicts: ["sys_yes_no", "mes_item_product"],
  components: {  },
  data() {
    return {
      showFlag: false,
      // 选中数组
      selectedVehicleId: undefined,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      vehicleList: null,
      // 部门树选项
      itemTypeOptions: undefined,
      // 部门名称
      itemTypeName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: undefined,
        itemName: undefined,
        itemTypeId: 0,
      },
      // 列信息
      columns: [
        { key: 0, label: `物料/产品编码`, visible: true },
        { key: 1, label: `物料/产品名称`, visible: true },
        { key: 2, label: `规格型号`, visible: true },
        { key: 3, label: `单位`, visible: true },
        { key: 4, label: `物料/产品`, visible: true },
        { key: 5, label: `物料分类`, visible: true },
        { key: 6, label: `创建时间`, visible: true },
        { key: 7, label: `备注`, visible: true },
      ],
    };
  },
  watch: {
    // 根据名称筛选分类树
    itemTypeName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      listVehicle(this.queryParams).then((response) => {
        this.vehicleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedVehicleId == null || this.selectedVehicleId == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
