<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账单标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入账单标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="月份">
        <el-date-picker v-model="monthTime" value-format="yyyy-MM" type="month" placeholder="选择月">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.settlement_status_type" :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" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @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="['settlement:bill:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-check" size="mini" :disabled="multiple" @click="handleBatchConfirm"
          v-hasPermi="['settlement:bill:confirm']">批量确认</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

        <Page uri="/mapi/settlement/bill/listSettlementBill" :request-params="queryParams" ref="pagination">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账单标题" align="center" prop="title" />
      <el-table-column label="开始时间" align="center" prop="beginTime" width="100px" />
      <el-table-column label="结束时间" align="center" prop="endTime" width="100px" />
      <el-table-column label="门店数量" align="center" prop="storeNum" width="80px" />
      <el-table-column label="原材料备用金（元）" align="center" prop="materialImprestAmount" />
      <el-table-column label="系统维护费（元）" align="center" prop="systemMaintenanceAmount" />
      <el-table-column label="洗衣费（元）" align="center" prop="washClothAmount" />
      <el-table-column label="结算（元）" align="center" prop="settlementAmount" />
      <el-table-column label="状态" align="center" prop="status" width="100px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.settlement_status_type" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="250">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="storeDetail(scope.row)">门店结算详情</el-button>
          <el-button type="text" icon="el-icon-edit" @click="factoryDetail(scope.row)" v-hasPermi="['settlement:bill:query']">工厂结算详情</el-button>
          <el-button type="text" icon="el-icon-refresh" @click="handleReverse(scope.row)" v-if="scope.row.status === '1'"
            v-hasPermi="['settlement:bill:reverse']">红冲</el-button>
        </template>
      </el-table-column>
    </Page>


    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账单标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入账单标题" />
        </el-form-item>
        <el-form-item label="月份" prop="month">
          <el-date-picker v-model="form.month" type="month" placeholder="选择月份" value-format="yyyy-MM" />
        </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 { listSettlementBill, getSettlementBill, delSettlementBill, addSettlementBill, confirmBatchSettlementBill, reverseSettlementBill } from '@/api/settlement/bill'

export default {
  name: 'SettlementBill',
  dicts: ['settlement_status_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算单表格数据
      billList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        status: null,
        monthTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '账单标题不能为空', trigger: 'blur' }
        ],
        month: [
          { required: true, message: '月份不能为空', trigger: 'blur' }
        ]
      },
      // 月份
      monthTime: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询结算单列表 */
    getList() {
      if (this.monthTime) {
        this.queryParams.monthTime = this.monthTime
      }
      this.$nextTick(() => {
        this.$refs.pagination.handleSearch(true)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        month: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.monthTime = null
      this.resetForm('queryForm')
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        title: null,
        status: null,
        monthTime: null
      }
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加结算单'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addSettlementBill(this.form).then(response => {
            this.$modal.msgSuccess('新增成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 批量确认按钮操作 */
    handleBatchConfirm() {
      if (this.ids.length === 0) {
        this.$message.warning('请至少选择一条记录')
        return
      }
      this.$modal.confirm('确认要批量确认所选择的结算单吗?').then(() => {
        return confirmBatchSettlementBill(this.ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('批量确认成功')
      }).catch(() => { })
    },
    /** 红冲按钮操作 */
    handleReverse(row) {
      this.$modal.confirm('是否确认红冲该结算单?').then(() => {
        return reverseSettlementBill({ id: row.id })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('红冲成功')
      }).catch(() => { })
    },
    /** 跳转到门店结算详情 */
    storeDetail(row) {
      this.$tab.openPage(row.title + '的门店结算详情', '/settlement/store/' + row.id)
    },
    /** 跳转到工厂结算详情 */
    factoryDetail(row) {
      this.$tab.openPage(row.title + '的工厂结算详情', '/settlement/factory/' + row.id)
    }
  }
}
</script>
