<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="结算单号" prop="settlementNumber">
        <el-input
          v-model="queryParams.settlementNumber"
          placeholder="请输入结算单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="clientPhone">
        <el-input
          v-model="queryParams.clientPhone"
          placeholder="请输入客户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择结算状态" style="width: 100%;">
          <el-option v-for="item in dict.type.settlement_state" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="settlementMaintainList" @selection-change="handleSelectionChange">
      <el-table-column label="结算单号" align="center" prop="settlementNumber" width="150px"/>
      <el-table-column label="客户名称" align="center" prop="clientName" width="150px" show-overflow-tooltip/>
<!--      <el-table-column label="客户手机号" align="center" prop="clientPhone" show-overflow-tooltip />-->
<!--      <el-table-column label="客户类型" align="center" prop="clientType" />-->
      <el-table-column label="支付方式" align="center" prop="paymentMethod" width="80px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.settlement_payment_method" :value="scope.row.paymentMethod" />
        </template>
      </el-table-column>
      <el-table-column label="证件类型" align="center" prop="clientCertificateType" width="120px" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhengjianleixing" :value="scope.row.clientCertificateType" />
        </template>
      </el-table-column>
      <el-table-column label="证件号" align="center" prop="clientCertificateNumber" width="100px" show-overflow-tooltip/>
      <el-table-column label="工时费优惠" align="center" prop="manHourDiscountMoney" />
      <el-table-column label="原料费优惠" align="center" prop="materialDiscountMoney" />
      <el-table-column label="结算时间" align="center" prop="settlementTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.settlementTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.settlement_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="结算备注" align="center" prop="settlementRemark" show-overflow-tooltip/>
      <!--      <el-table-column label="付款账号" align="center" prop="paymentAccount" />-->
<!--      <el-table-column label="账号名称" align="center" prop="paymentAccountName" />-->
      <el-table-column label="付款凭证" align="center" prop="paymentVoucher">
        <template slot-scope="scope">
          <image-preview :src="scope.row.paymentVoucher" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="收款回执" align="center" prop="collectionVoucher">
        <template slot-scope="scope">
          <image-preview :src="scope.row.collectionVoucher" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="收款时间" align="center" prop="collectionTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.collectionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款账号" align="center" prop="collectionAccount" width="150" show-overflow-tooltip />
      <el-table-column label="流水号" align="center" prop="collectionSerialNumber" width="150" show-overflow-tooltip />
      <el-table-column label="收款备注" align="center" prop="collectionRemark" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" width="120px" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.state === 1"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:settlementMaintain:receipt']"
          >回执</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="80%" append-to-body>
      <el-scrollbar style="height: 75vh;">
        <el-form ref="form" :model="form" :rules="rules" label-width="110px">
          <span style="font-weight: bold; font-size: 18px">结算信息</span>
          <el-divider />
          <el-row>
            <el-col :span="6">
              <el-form-item label="结算单号" prop="settlementNumber">
                <el-input v-model="form.settlementNumber" disabled placeholder="请输入结算单号" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="结算时间" prop="settlementTime">
                <el-date-picker clearable
                                v-model="form.settlementTime"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                disabled
                                placeholder="请选择结算时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="工时费优惠" prop="manHourDiscountMoney">
                <el-input-number v-model="form.manHourDiscountMoney" disabled style="width: 100%" placeholder="请输入工时费优惠" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="原料费优惠" prop="materialDiscountMoney">
                <el-input-number v-model="form.materialDiscountMoney" disabled style="width: 100%" placeholder="请输入原料费优惠" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="客户名称" prop="clientName">
                <el-input v-model="form.clientName" disabled placeholder="请输入客户名称" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="客户手机号" prop="clientPhone">
                <el-input v-model="form.clientPhone" disabled placeholder="请输入客户手机号" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="证件类型" prop="clientCertificateType">
                <el-select v-model="form.clientCertificateType" disabled placeholder="请选择证件类型">
                  <el-option v-for="item in dict.type.zhengjianleixing" :key="item.value" :value="parseInt(item.value)" :label="item.label" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="证件号" prop="clientCertificateNumber">
                <el-input v-model="form.clientCertificateNumber" disabled placeholder="请输入证件号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="付款凭证" prop="paymentVoucher">
                <image-preview :src="form.paymentVoucher" width="50px" height="50px"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="支付方式" prop="paymentMethod">
                <el-select v-model="form.paymentMethod" disabled placeholder="请选择支付方式">
                  <el-option v-for="item in dict.type.settlement_payment_method" :key="item.value" :value="parseInt(item.value)" :label="item.label" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="付款账号" prop="paymentAccount">
                <el-input v-model="form.paymentAccount" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="账号名称" prop="paymentAccountName">
                <el-input v-model="form.paymentAccountName" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="结算单备注" prop="settlementRemark">
                <el-input v-model="form.settlementRemark" disabled type="textarea" :rows="2" />
              </el-form-item>
            </el-col>
          </el-row>
          <span style="font-weight: bold; font-size: 18px">报修信息</span>
          <el-divider />
          <el-table v-loading="loading" :data="form.repairFormList">
            <el-table-column label="序号" align="center" type="index" />
            <el-table-column label="报修单号" align="center" prop="repairNumber" />
            <el-table-column label="客户类型" align="center" prop="repairNumber">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.client_type" :value="scope.row.client.type"/>
              </template>
            </el-table-column>
            <el-table-column label="客户名称" align="center" prop="client.name" />
            <el-table-column label="联系方式" align="center" prop="client.phone" min-width="130px" show-overflow-tooltip />
            <el-table-column label="车辆品牌" align="center" prop="car.carBrand" />
            <el-table-column label="车牌号" align="center" prop="car.plateNumber" />
            <el-table-column label="总工时" align="center" prop="totalActualTime" />
            <el-table-column label="工时费用（元）" align="center" prop="totalActualTimeMoney" min-width="120px" />
            <el-table-column label="材料费用（元）" align="center" prop="totalMaterialMoney" min-width="120px" />
            <el-table-column label="拖车费用（元）" align="center" prop="trailerMoney" min-width="120px" />
            <el-table-column label="结算总价（元）" align="center" prop="totalMoney" min-width="120px" />
            <el-table-column label="交工时间" align="center" prop="deliveryTime" min-width="150px" />
            <el-table-column label="结算时间" align="center" prop="settlementTime" min-width="150px" />
            <el-table-column label="登记时间" align="center" prop="createTime" min-width="150px" />
            <el-table-column label="登记人员" align="center" prop="createBy" min-width="100px" />
            <el-table-column label="是否交车" align="center" prop="isDelivery" fixed="right">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.repair_form_is_delivery" :value="scope.row.isDelivery" />
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="state" fixed="right">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.repair_form_state" :value="scope.row.state" />
              </template>
            </el-table-column>
          </el-table>
          <span style="font-weight: bold; font-size: 18px">收款回执</span>
          <el-divider />
          <el-row>
            <el-col :span="6">
              <el-form-item label="应收金额" prop="receivableMoney">
                <el-input-number v-model="form.receivableMoney" disabled :min="0" style="width: 100%" placeholder="请输入应收金额" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="结算金额" prop="practicalMoney">
                <el-input-number v-model="form.practicalMoney" disabled :min="0" style="width: 100%" placeholder="请输入结算金额" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建人" prop="createBy">
                <el-input v-model="form.createBy" disabled placeholder="请输入创建人" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建时间" prop="createTime">
                <el-input v-model="form.createTime" disabled placeholder="请输入创建时间" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="收款时间" prop="collectionTime">
                <el-date-picker clearable
                                v-model="form.collectionTime"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="请选择收款时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="收款账号" prop="collectionAccount">
                <el-input v-model="form.collectionAccount" placeholder="请输入收款账号" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="收款流水号" prop="collectionSerialNumber">
                <el-input v-model="form.collectionSerialNumber" placeholder="请输入收款流水号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="收款回执" prop="collectionVoucher">
                <imageUpload v-model="form.collectionVoucher" :limit="1" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="收款备注" prop="collectionRemark">
                <el-input v-model="form.collectionRemark" type="textarea" :rows="2" placeholder="请输入收款备注" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-scrollbar>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="80%" append-to-body>
      <el-scrollbar style="height: 75vh;">
        <span style="font-weight: bold; font-size: 18px">结算信息</span>
        <el-divider />
        <el-descriptions border :column="4">
          <el-descriptions-item label="结算单号">{{form.settlementNumber}}</el-descriptions-item>
          <el-descriptions-item label="结算时间">{{form.settlementTime}}</el-descriptions-item>
          <el-descriptions-item label="工时费优惠">{{form.manHourDiscountMoney}}</el-descriptions-item>
          <el-descriptions-item label="原料费优惠">{{form.materialDiscountMoney}}</el-descriptions-item>
          <el-descriptions-item label="客户名称">{{form.clientName}}</el-descriptions-item>
          <el-descriptions-item label="客户手机号">{{form.clientPhone}}</el-descriptions-item>
          <el-descriptions-item label="证件类型">
            <dict-tag :options="dict.type.zhengjianleixing" :value="form.clientCertificateType" />
          </el-descriptions-item>
          <el-descriptions-item label="证件号">{{form.clientCertificateNumber}}</el-descriptions-item>
          <el-descriptions-item label="付款凭证">
            <image-preview :src="form.paymentVoucher" :width="50" :height="50" />
          </el-descriptions-item>
          <el-descriptions-item label="支付方式">
            <dict-tag :options="dict.type.settlement_payment_method" :value="form.paymentMethod" />
          </el-descriptions-item>
          <el-descriptions-item label="付款账号">{{form.paymentAccount}}</el-descriptions-item>
          <el-descriptions-item label="账号名称">{{form.paymentAccountName}}</el-descriptions-item>
          <el-descriptions-item label="结算单备注">{{form.settlementRemark}}</el-descriptions-item>
        </el-descriptions>
        <span style="font-weight: bold; font-size: 18px">收款回执</span>
        <el-divider />
        <el-descriptions border :column="4">
          <el-descriptions-item label="应收金额">{{form.receivableMoney}}</el-descriptions-item>
          <el-descriptions-item label="结算金额">{{form.practicalMoney}}</el-descriptions-item>
          <el-descriptions-item label="创建人">{{form.createBy}}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{form.createTime}}</el-descriptions-item>
          <el-descriptions-item label="收款时间">{{form.collectionTime}}</el-descriptions-item>
          <el-descriptions-item label="收款账号">{{form.collectionAccount}}</el-descriptions-item>
          <el-descriptions-item label="收款流水号">{{form.collectionSerialNumber}}</el-descriptions-item>
          <el-descriptions-item label="收款回执">
            <image-preview :src="form.collectionVoucher" :width="50" :height="50" />
          </el-descriptions-item>
          <el-descriptions-item label="收款备注">{{form.collectionRemark}}</el-descriptions-item>
        </el-descriptions>
        <span style="font-weight: bold; font-size: 18px">报修信息</span>
        <el-divider />
        <el-table v-loading="loading" :data="form.repairFormList">
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="报修单号" align="center" prop="repairNumber" />
          <el-table-column label="客户类型" align="center" prop="repairNumber">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.client_type" :value="scope.row.client.type"/>
            </template>
          </el-table-column>
          <el-table-column label="客户名称" align="center" prop="client.name" />
          <el-table-column label="联系方式" align="center" prop="client.phone" min-width="130px" show-overflow-tooltip />
          <el-table-column label="车辆品牌" align="center" prop="car.carBrand" />
          <el-table-column label="车牌号" align="center" prop="car.plateNumber" />
          <el-table-column label="总工时" align="center" prop="totalActualTime" />
          <el-table-column label="工时费用（元）" align="center" prop="totalActualTimeMoney" min-width="120px" />
          <el-table-column label="材料费用（元）" align="center" prop="totalMaterialMoney" min-width="120px" />
          <el-table-column label="拖车费用（元）" align="center" prop="trailerMoney" min-width="120px" />
          <el-table-column label="结算总价（元）" align="center" prop="totalMoney" min-width="120px" />
          <el-table-column label="交工时间" align="center" prop="deliveryTime" min-width="150px" />
          <el-table-column label="结算时间" align="center" prop="settlementTime" min-width="150px" />
          <el-table-column label="登记时间" align="center" prop="createTime" min-width="150px" />
          <el-table-column label="登记人员" align="center" prop="createBy" min-width="100px" />
          <el-table-column label="是否交车" align="center" prop="isDelivery" fixed="right">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.repair_form_is_delivery" :value="scope.row.isDelivery" />
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="state" fixed="right">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.repair_form_state" :value="scope.row.state" />
            </template>
          </el-table-column>
        </el-table>
      </el-scrollbar>
    </el-dialog>
  </div>
</template>

<script>
import {
  delSettlementMaintain,
  getSettlementMaintain,
  listSettlementMaintain,
  receiptSettlementMaintain
} from '@/api/business/settlementMaintain'

export default {
  name: "SettlementMaintain",
  dicts:['settlement_state', 'zhengjianleixing', 'client_type', 'settlement_payment_method', 'repair_form_is_delivery', 'repair_form_state'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算维修单表格数据
      settlementMaintainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        settlementNumber: undefined,
        clientName: undefined,
        clientPhone: undefined,
        state: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        collectionTime: [
          { required: true, message: "收款时间不能为空", trigger: "blur" }
        ],
        collectionAccount: [
          { required: true, message: "收款账号不能为空", trigger: "blur" }
        ],
        collectionSerialNumber: [
          { required: true, message: "收款流水号不能为空", trigger: "blur" }
        ],
        collectionVoucher: [
          { required: true, message: "收款回执不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询结算维修单列表 */
    getList() {
      this.loading = true;
      this.queryParams.clientType = this.clientType
      listSettlementMaintain(this.queryParams).then(response => {
        this.settlementMaintainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        settlementNumber: undefined,
        clientId: undefined,
        clientName: undefined,
        clientPhone: undefined,
        clientType: undefined,
        clientCertificateType: undefined,
        clientCertificateNumber: undefined,
        manHourDiscountMoney: undefined,
        materialDiscountMoney: undefined,
        settlementTime: undefined,
        paymentMethod: undefined,
        paymentAccount: undefined,
        paymentAccountName: undefined,
        paymentVoucher: undefined,
        settlementRemark: undefined,
        collectionTime: undefined,
        collectionVoucher: undefined,
        collectionAccount: undefined,
        collectionSerialNumber: undefined,
        collectionRemark: undefined,
        state: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        createDeptId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getSettlementMaintain(row.id).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "结算维修单详情";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加结算维修单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSettlementMaintain(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "结算维修单回执";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.repairFormIds = this.form.repairFormList.map(item => item.id)
          receiptSettlementMaintain(this.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除结算维修单编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSettlementMaintain(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/settlementMaintain/export', {
        ...this.queryParams
      }, `settlementMaintain_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
