<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车型" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入车型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理商" prop="agentId">
        <el-input
          v-model="queryParams.agentId"
          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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:model:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:model:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:model:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:model:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="代理商" align="center" prop="agnetName" />
      <el-table-column label="车型" align="center" prop="model" />
      <el-table-column label="满电电压" align="center" prop="fullVoltage" />
      <el-table-column label="亏电电压" align="center" prop="lowVoltage" />
      <el-table-column label="满电续航" align="center" prop="fullEndurance" />
      <el-table-column label="押金" align="center" prop="deposit" />
      <el-table-column label="图片" align="center" prop="picture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:model:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:model:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改车辆型号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="代理商" prop="agentId " label-width="100px">
          <!-- <el-input v-model="form.agentId" placeholder="请输入代理商" /> -->
          <el-select v-model="form.agentId" filterable placeholder="请选择">
              <el-option v-for="item in agentList" :key="item.agentId" :label="item.name+'  ' +item.contact" :value="item.agentId">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="套餐" prop="agentId " label-width="100px">
          <!-- <el-input v-model="form.agentId" placeholder="请输入代理商" /> -->
          <el-select v-model="form.feeRulesIds" filterable placeholder="请选择" :multiple="true">
              <el-option v-for="item in ruleList" :key="item.ruleId" :label="item.price+'元/' + timeReturn( item.rentalUnit)" :value="item.ruleId">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="车辆配件" prop="agentId " label-width="100px">
          <!-- <el-input v-model="form.agentId" placeholder="请输入代理商" /> -->
          <el-select v-model="form.accessoryIds" filterable placeholder="请选择" :multiple="true">
              <el-option v-for="item in AccessoryList" :key="item.accessoryId" :label="item.name" :value="item.accessoryId">
              </el-option>
            </el-select>
        </el-form-item>
       
        <el-form-item label="车型名称" prop="model" label-width="100px">
          <el-input v-model="form.model" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="满电电压(v)" prop="fullVoltage" label-width="100px">
          <el-input v-model="form.fullVoltage" placeholder="请输入满电电压(v)" />
        </el-form-item>
        <el-form-item label="亏电电压(v)" prop="lowVoltage" label-width="100px">
          <el-input v-model="form.lowVoltage" placeholder="请输入亏电电压(v)" />
        </el-form-item>
        <el-form-item label="满电续航" prop="fullEndurance" label-width="100px">
          <el-input v-model="form.fullEndurance" placeholder="请输入满电续航" />
        </el-form-item>
        <el-form-item label="备注" prop="remark" label-width="100px">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="简介" prop="intro" label-width="100px">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="押金" prop="deposit" label-width="100px">
          <el-input v-model="form.deposit" placeholder="请输入押金" />
        </el-form-item>
        <el-table-column label="商户图片" align="center" prop="picture" width="100" label-width="100px">
          <form-col label="店铺图片" prop="picture" :span="span * 2">
            <image-upload v-model="form.picture" :limit="1" />
          </form-col>
      </el-table-column>
        <!-- <el-form-item label="图片" prop="picture">
          <el-input v-model="form.picture" placeholder="请输入图片" />
        </el-form-item> -->
        <el-form-item label="描述" prop="description" label-width="100px">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </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>
  </div>
</template>

<script>
import { listModel, getModel, delModel, addModel, updateModel } from "@/api/rl/model";
import { listAgent, getAgent, delAgent, addAgent, updateAgent } from "@/api/rl/agent";
import { listRule, getRule, delRule, addRule, updateRule } from "@/api/rl/rule";
import {
    listAccessory,
  
  } from "@/api/rl/accessory";
export default {
  name: "Model",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆型号表格数据
      modelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        model: null,
        fullVoltage: null,
        lowVoltage: null,
        fullEndurance: null,
        agentId: null,
        intro: null,
        deposit: null,
        picture: null,
        description: null
      },
      // 表单参数
      form: {},
      agentList:[],
      ruleList:[],
      AccessoryList:[],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getAgentList()
    this.getlistRule()
    this.getAccessoryList()
  },
  methods: {
    // 
    timeReturn(time){
      if (time == 'hour') {
					return '小时'
				} else if (time == 'day') {
					return '天'
				} else if (time == 'week') {
					return '周'
				} else if (time == 'month') {
					return '月'
				} else if (time == 'quarter') {
					return '季度'
				} else if (time == 'year') {
					return '年'
				}
    },
    getAgentList() {
      let data = {
        pageNum: 1,
        pageSize: 30,
       
      }
      listAgent(this.addDateRange(data)).then(response => {
        console.log(response,'responseresponseresponse');
        this.agentList=response.rows
        // this.options = response.rows.map(item => ({
				// 			value: item.userId,
				// 			label: item.realName+'  ' +item.phonenumber
				// 		}));
          console.log( this.options,' this.options this.options');
      });
     
    },
    getAccessoryList() {
      let data = {
        pageNum: 1,
        pageSize: 30,
       
      }
      listAccessory(this.addDateRange(data)).then(response => {
        console.log(response,'responseresponseresponse');
        this.AccessoryList=response.rows
        // this.options = response.rows.map(item => ({
				// 			value: item.userId,
				// 			label: item.realName+'  ' +item.phonenumber
				// 		}));
          console.log( this.options,' this.options this.options');
      });
     
    },
    getlistRule() {
      let data = {
        pageNum: 1,
        pageSize: 30,
       
      }
      listRule(this.addDateRange(data)).then(response => {
        console.log(response,'responseresponseresponse');
        this.ruleList=response.rows
     
          console.log( this.options,' this.options this.options');
      });
     
    },
    
    /** 查询车辆型号列表 */
    getList() {
      this.loading = true;
      listModel(this.queryParams).then(response => {
        this.modelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        modelId: null,
        model: null,
        fullVoltage: null,
        lowVoltage: null,
        fullEndurance: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        agentId: null,
        intro: null,
        deposit: null,
        picture: null,
        description: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.modelId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆型号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const modelId = row.modelId || this.ids
      getModel(modelId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆型号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.modelId != null) {
            updateModel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const modelIds = row.modelId || this.ids;
      this.$modal.confirm('是否确认删除车辆型号编号为"' + modelIds + '"的数据项？').then(function() {
        return delModel(modelIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/model/export', {
        ...this.queryParams
      }, `model_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
