<template>
  <div class="app-container">
    <div class="header">
      <el-button icon="el-icon-d-arrow-left" plain type="primary" class="back" @click="goBack">返回</el-button>
      <h1>{{ '【' + carInfo.licensePlate + '】' }}车辆明细</h1>
    </div>
    <el-divider></el-divider>
    <el-descriptions class="descriptions" :column="column" border style="text-align: center;">
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="car" class="el-input__icon input-icon" />
          车牌号
        </template>
        {{ carInfo.licensePlate }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="Branding" class="el-input__icon input-icon" />
          品牌
        </template>
        {{ carInfo.brand }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="PhAppStoreLogoFill" class="el-input__icon input-icon" />
          系列
        </template>
        {{ carInfo.series }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="cost" class="el-input__icon input-icon" />
          总价
        </template>
        {{ carInfo.cost }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="date" class="el-input__icon input-icon" />
          上牌时间
        </template>
        {{ parseTime(carInfo.acquisitionTime, '{y}-{m}-{d}') }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user-solid"></i>
          驾驶员
        </template>
        {{ carInfo.driver }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="date" class="el-input__icon input-icon" />
          预计使用年限
        </template>
        {{ carInfo.expectLife }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="car" class="el-input__icon input-icon" />
          预计行驶里程（万千米）
        </template>
        {{ carInfo.expectKm }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="JamYelpSquare" class="el-input__icon input-icon" />
          是否为租车
        </template>
        {{ carInfo.isRent == '0' ? '否' : '是' }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="TeenyiconsYenSolid" class="el-input__icon input-icon" />
          单日成本
        </template>
        {{ carInfo.dayCost }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <svg-icon slot="prefix" icon-class="remark" class="el-input__icon input-icon" />
          备注
        </template>
        {{ carInfo.remark }}
      </el-descriptions-item>
    </el-descriptions>
    <!-- 费用明细 -->
    <el-divider></el-divider>

    <el-card>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
            @click="handleUpdate">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
            @click="handleDelete">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="expenseList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" width="55" align="center" label="序号" />
        <el-table-column label="费用类型" align="center" prop="expenseType">
          <template slot-scope="scope">
            <el-tag :type="typeStyle(scope.row.expenseType)">{{ expenseTypeText(scope.row.expenseType) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="费用金额" align="center" prop="expense">
          <template slot-scope="scope">
            {{ scope.row.expense + '元' }}
          </template>
        </el-table-column>
        <el-table-column label="发生日期" align="center" prop="occurDate" width="180">
          <template slot-scope="scope">
            {{ scope.row.occurDate }}
          </template>
        </el-table-column>
        <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="['oa:carExpense:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['oa:carExpense: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-card>
    <!-- 添加明细 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="expenseForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="费用类型" prop="expenseType">
          <el-select v-model="form.expenseType" placeholder="请选择" clearable>
            <el-option label="保险费" value="0"></el-option>
            <el-option label="维修/保养费" value="1"></el-option>
            <el-option label="轮胎费" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用金额" prop="expense">
          <el-input v-model="form.expense" placeholder="请输入费用金额" style="width:150px" /> <el-tag>元</el-tag>
        </el-form-item>
        <el-form-item label="发生日期" prop="occurDate">
          <el-date-picker clearable v-model="form.occurDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择发生日期">
          </el-date-picker>
        </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 { getCar } from '@/api/oa/car/car.js'
import { listCarExpense, getCarExpense, delCarExpense, addCarExpense, updateCarExpense } from "@/api/oa/car/carExpense";
export default {
  data() {
    let validateExpense = (rule, value, callback) => {
      setTimeout(() => {
        if (!value) {
          return callback(new Error('请输入金额'));
        }
        if (!this.isNumber(Number(value))) {
          callback(new Error('请输入数字值'));
        } else {
          callback();
        }
      }, 100)
    }
    return {
      column: 4,
      carInfo: {},
      expenseList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      multiple: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      title: '',
      open: false,
      form: {},
      rules: {
        expenseType: [
          { required: true, message: '请选择费用类型', trigger: 'change' }
        ],
        expense: [
          { validator: validateExpense, required: true, trigger: 'blur' },
        ],
        occurDate: [
          { required: true, message: '请选择发生日期', trigger: 'change' },
        ],
      },
      loading: false
    }
  },
  created() {
    if (this.$route.query) {
      getCar(this.$route.query.carId).then(res => {
        console.log(res);
        this.carInfo = res.data;
      })
      this.getList();
    }
    if (this.$store.state.app.device == 'desktop') {

    } else {
      this.column = 1
    }
  },
  methods: {
    getList() {
      this.queryParams.carId = this.$route.query.carId
      listCarExpense(this.queryParams).then(res => {
        console.log(res);
        this.expenseList = res.rows;
        this.total = res.total;
      })
    },
    goBack() {
      let obj = { path: "/car/expense" }
      this.$tab.closeOpenPage(obj);
      this.$router.push({ path: '/car' });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carExpenseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆明细";
    },
    handleUpdate(row) {
      this.reset();
      const carExpenseId = row.carExpenseId || this.ids
      getCarExpense(carExpenseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆明细";
      });
    },
    handleDelete(row) {
      debugger
      const carExpenseIds = row.carExpenseId || this.ids;
      this.$modal.confirm('是否确认删除车辆费用编号为"' + carExpenseIds + '"的数据项？').then(function () {
        return delCarExpense(carExpenseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    handleExport() {
      this.download('oa/carExpense/export', {
        ...this.queryParams
      }, `carExpense_${new Date().getTime()}.xlsx`)
    },
    // 表单重置
    reset() {
      this.form = {
        carExpenseId: null,
        carId: null,
        expenseType: null,
        expense: null,
        occurDate: null
      };
      this.resetForm("expenseForm");
    },
    submitForm() {
      this.$refs["expenseForm"].validate(valid => {
        if (valid) {
          this.form.carId = this.$route.query.carId
          if (this.form.carExpenseId != null) {
            updateCarExpense(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCarExpense(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    isNumber(value) {
      return typeof value === 'number' && /^\d+(\.\d+)?$/.test(value);
    },
    expenseTypeText(row) {
      if (row == '0') {
        return '保险费'
      }
      if (row == '1') {
        return '维修/保养费'
      }
      if (row == '2') {
        return '轮胎费'
      }
    },
    typeStyle(row) {
      if (row == '0') {
        return 'primary'
      }
      if (row == '1') {
        return 'warning'
      }
      if (row == '2') {
        return 'success'
      }
    }
  },
}
</script>

<style lang="scss" scoped>
.header {
  position: relative;
  text-align: center;

  .back {
    position: absolute;
    left: 12px;
    top: -10px;
  }
}

.descriptions {
  padding: 20px 50px;
}


::v-deep .el-descriptions-item__label.is-bordered-label {
  color: #121212;
  background: rgba($color: #46a6ff, $alpha: 0.1);
  width: 200px;
}

::v-deep .el-descriptions .is-bordered .el-descriptions-item__cell {
  border: 1px solid #2d3f62;
}
</style>