<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="communityId">
        <el-select v-model="queryParams.communityId" placeholder="请选择小区" clearable>
          <el-option v-for="item in communityList" :key="item.communityId" :value="item.communityId" :label="item.communityName" />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="proprietorName">
        <el-input
          v-model="queryParams.proprietorName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="proprietorPhone">
        <el-input
          v-model="queryParams.proprietorPhone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户号" prop="room">
        <el-input
          v-model="queryParams.room"
          placeholder="请输入户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登记车牌" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入登记车牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.wx_live_bill_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.wx_live_bill_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:liveBill: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="['business:liveBill:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleConfirm"
          v-hasPermi="['business:liveBill:confirm']"
        >确认缴纳</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:liveBill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="liveBillList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="账单主键" align="center" prop="billId" v-if="false"/>-->
<!--      <el-table-column label="用户名称" align="center" prop="userName" />-->
      <el-table-column label="小区" align="center" prop="communityName" />
      <el-table-column label="姓名" align="center" prop="proprietorName" />
      <el-table-column label="电话" align="center" prop="proprietorPhone" />
      <el-table-column label="户号" align="center" prop="room" />
      <el-table-column label="面积㎡" align="center" prop="sqm" />
      <el-table-column label="车位号" align="center" prop="carportNumber" />
      <el-table-column label="登记车牌" align="center" prop="plateNumber" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目描述" align="center" prop="projectInfo" />
      <el-table-column label="缴费月份" align="center" prop="month">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.month, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="金额" align="center" prop="money" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wx_live_bill_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wx_live_bill_state" :value="scope.row.state"/>
        </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"
            v-if="scope.row.state === 1"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:liveBill:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="scope.row.state === 1"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:liveBill: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="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="小区业主" prop="proprietorId">
              <el-select v-model="form.proprietorId" @change="proprietorIdChange" filterable style="width: 100%" placeholder="请选择小区业主" clearable>
                <el-option v-for="item in proprietorLedgerList" :key="item.proprietorId" :value="item.proprietorId" :label="`${item.communityName} | ${item.room} | ${item.name} | ${item.phone}`" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="缴费月份" prop="month">
              <el-date-picker v-model="form.month" type="month" value-format="yyyy-MM" style="width: 100%" placeholder="选择月" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="费用类型" prop="type">
              <el-select v-model="form.type" style="width: 100%" placeholder="请选择类型">
                <el-option
                  v-for="dict in dict.type.wx_live_bill_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="小区" prop="communityName">
              <el-input v-model="form.communityName" disabled placeholder="请输入小区" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="户号" prop="room">
              <el-input v-model="form.room" disabled placeholder="请输入户号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="proprietorName">
              <el-input v-model="form.proprietorName" disabled placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="proprietorPhone">
              <el-input v-model="form.proprietorPhone" disabled placeholder="请输入电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="面积㎡" prop="sqm">
              <el-input v-model="form.sqm" disabled placeholder="请输入面积㎡" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单价㎡" prop="price">
              <el-input-number v-model="form.price" style="width: 100%" disabled placeholder="请输入单价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="金额" prop="money">
              <el-input-number v-model="form.money" :min="0" style="width: 100%" placeholder="请输入金额" />
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="登记车牌" prop="plateNumber">-->
<!--              <el-input v-model="form.plateNumber" placeholder="请输入登记车牌" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
      </el-form>
      <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>
  </div>
</template>

<script>
import {
  addLiveBill,
  confirmLiveBill,
  delLiveBill,
  getLiveBill,
  listLiveBill,
  updateLiveBill
} from "@/api/business/liveBill";
import {listCommunityAll} from "@/api/business/community";
import {listProprietorLedgerAll} from "@/api/business/proprietorLedger";

export default {
  name: "LiveBill",
  dicts: ['wx_live_bill_type', 'wx_live_bill_state'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生活缴费账单表格数据
      liveBillList: [],
      communityList: [],
      proprietorLedgerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        userName: undefined,
        communityId: undefined,
        communityName: undefined,
        proprietorName: undefined,
        proprietorPhone: undefined,
        room: undefined,
        plateNumber: undefined,
        month: undefined,
        price: undefined,
        money: undefined,
        type: undefined,
        state: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        proprietorId: [
          { required: true, message: "业主不能为空", trigger: "blur" }
        ],
        plateNumber: [
          { required: true, message: "登记车牌不能为空", trigger: "blur" }
        ],
        month: [
          { required: true, message: "缴费月份不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "金额不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCommunityListAll();
  },
  methods: {
    /** 查询生活缴费账单列表 */
    getList() {
      this.loading = true;
      listLiveBill(this.queryParams).then(response => {
        this.liveBillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询全部小区列表 */
    getCommunityListAll() {
      listCommunityAll().then(response => {
        this.communityList = response.data;
      });
    },
    /** 查询小区业主台账信息列表 */
    getProprietorLedgerList() {
      this.loading = true
      listProprietorLedgerAll().then(response => {
        this.proprietorLedgerList = response.data;
        this.loading = false;
      });
    },
    /** 业主改变 */
    proprietorIdChange(id) {
      let proprietorLedger = this.proprietorLedgerList.find(item => item.proprietorId === id)
      if (proprietorLedger) {
        this.form.communityId= proprietorLedger.communityId
        this.form.communityName = proprietorLedger.communityName
        this.form.proprietorName = proprietorLedger.name
        this.form.proprietorPhone = proprietorLedger.phone
        this.form.room = proprietorLedger.room
        this.form.sqm = proprietorLedger.sqm
        this.form.price = proprietorLedger.sqmPrice
        this.form.money = proprietorLedger.monthMoney
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        billId: undefined,
        userId: undefined,
        userName: undefined,
        communityId: undefined,
        communityName: undefined,
        proprietorName: undefined,
        proprietorPhone: undefined,
        room: undefined,
        sqm: undefined,
        plateNumber: undefined,
        month: undefined,
        price: undefined,
        money: undefined,
        type: undefined,
        state: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: 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.billId)
      this.single = selection.length!==1
      let isTrue = false
      selection.map(item =>{
        if (item.state === 2) {
          isTrue = true
        }
      })
      this.multiple = !selection.length || isTrue
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生活缴费账单";
      this.getProprietorLedgerList()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.getProprietorLedgerList()
      const billId = row.billId || this.ids
      getLiveBill(billId).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;
          if (this.form.billId != null) {
            updateLiveBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addLiveBill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.billId || this.ids;
      this.$modal.confirm('是否确认删除生活缴费账单编号为"' + billIds + '"的数据项？').then(() => {
        this.loading = true;
        return delLiveBill(billIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 确认缴纳按钮操作 */
    handleConfirm(row) {
      const billIds = row.billId || this.ids;
      this.$modal.confirm('是否确认勾选待缴账单已缴纳？').then(() => {
        this.loading = true;
        return confirmLiveBill(billIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/liveBill/export', {
        ...this.queryParams
      }, `liveBill_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
