<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="120px" prop="orgId">
        <el-select v-model="queryParams.storeId" placeholder="请选择" clearable filterable>
          <el-option v-for="store in storeList" :key="store.id + store.sourceType" :label="store.name" :value="store.id"> </el-option>
        </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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <Page uri="/mapi/settlement/store/rate/list" :request-params="queryParams" ref="pagination">
      <el-table-column label="编号" align="center" prop="storeId" width="100px" />
      <el-table-column label="门店名称" align="center" prop="storeName" />
      <el-table-column label="工厂结算费率" align="center" prop="factoryRate">
        <template slot-scope="scope">
          <span v-if="scope.row.factoryRate != null && scope.row.factoryRate >= 0">{{(scope.row.factoryRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="材料费率" align="center" prop="materialRate">
        <template slot-scope="scope">
          <span v-if="scope.row.materialRate != null && scope.row.materialRate >= 0">{{(scope.row.materialRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="会员卡押金费率" align="center" prop="cardDepositRate">
        <template slot-scope="scope">
          <span v-if="scope.row.cardDepositRate != null && scope.row.cardDepositRate >= 0">{{(scope.row.cardDepositRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="福利押金费率" align="center" prop="welfareDepositRate">
        <template slot-scope="scope">
          <span v-if="scope.row.welfareDepositRate != null && scope.row.welfareDepositRate >= 0">{{(scope.row.welfareDepositRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="卡密押金费率" align="center" prop="passwordDepositRate">
        <template slot-scope="scope">
          <span v-if="scope.row.passwordDepositRate != null && scope.row.passwordDepositRate >= 0">{{(scope.row.passwordDepositRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="异店消费费率" align="center" prop="otherConsumeRate">
        <template slot-scope="scope">
          <span v-if="scope.row.otherConsumeRate != null && scope.row.otherConsumeRate >= 0">{{(scope.row.otherConsumeRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="支付宝支付费率" align="center" prop="aliPayRate">
        <template slot-scope="scope">
          <span v-if="scope.row.aliPayRate != null && scope.row.aliPayRate >= 0">{{(scope.row.aliPayRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="微信支付费率" align="center" prop="wxPayRate">
        <template slot-scope="scope">
          <span v-if="scope.row.wxPayRate != null && scope.row.wxPayRate >= 0">{{(scope.row.wxPayRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="系统管理费率" align="center" prop="managementRate">
        <template slot-scope="scope">
          <span v-if="scope.row.managementRate != null && scope.row.managementRate >= 0">{{(scope.row.managementRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="洗衣费率" align="center" prop="washRate">
        <template slot-scope="scope">
          <span v-if="scope.row.washRate != null && scope.row.washRate >= 0">{{(scope.row.washRate).toFixed(2)}}</span >
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startDate">
        <template slot-scope="scope">
          {{ scope.row.startDate || '--' }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDate">
        <template slot-scope="scope">
          {{ scope.row.endDate || '--' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['settlement:store:rate:edit']">修改</el-button>
        </template>
      </el-table-column>
    </Page>

    <!-- 添加或修改结算费率对话框 -->
    <el-dialog :title="title" :visible.sync="open" size="30%" append-to-body>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="130px">
          <el-col :span="12">
            <el-form-item label="工厂结算费率" prop="factoryRate">
              <el-input-number v-model="form.factoryRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
            <el-form-item label="材料费率" prop="materialRate">
              <el-input-number v-model="form.materialRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
            <el-form-item label="会员卡押金费率" prop="cardDepositRate">
              <el-input-number v-model="form.cardDepositRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
            <el-form-item label="福利押金费率" prop="welfareDepositRate">
              <el-input-number v-model="form.welfareDepositRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
            <el-form-item label="卡密押金费率" prop="passwordDepositRate">
              <el-input-number v-model="form.passwordDepositRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>

            <el-form-item label="时间范围" prop="dateRange">
              <el-date-picker clearable v-model="form.dateRange" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
                              start-placeholder="开始日期" end-placeholder="结束日期" @change="selectDateRange"> </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="异店消费费率" prop="otherConsumeRate">
              <el-input-number v-model="form.otherConsumeRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
            <el-form-item label="支付宝支付费率" prop="aliPayRate">
              <el-input-number v-model="form.aliPayRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
            <el-form-item label="微信支付费率" prop="wxPayRate">
              <el-input-number v-model="form.wxPayRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
            <el-form-item label="系统管理费率" prop="managementRate">
              <el-input-number v-model="form.managementRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
            <el-form-item label="洗衣费率" prop="washRate">
              <el-input-number v-model="form.washRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getStoreRateInfo, saveStoreRate, updateStoreRate } from '@/api/settlement/storeRate'
import { allOrg } from '@/api/system/store'

export default {
  name: 'StoreRate',
  data() {
    return {
      orgList: [],
      storeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算费率表格数据
      rateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        storeId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dateRange: [{ required: true, message: '时间范围不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    allOrg().then((res) => {
      this.orgList = res.data
      this.orgList.forEach((org) => {
        if (org.sourceType !== '01') {
          this.storeList.push(org)
        }
      })
    })
    this.getList()
  },
  methods: {
    /** 查询结算费率列表 */
    getList() {
      this.$nextTick(() => {
        this.$refs.pagination.handleSearch(true)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryRate: 0,
        materialRate: 0,
        cardDepositRate: 0,
        welfareDepositRate: 0,
        passwordDepositRate: 0,
        otherConsumeRate: 0,
        aliPayRate: 0,
        wxPayRate: 0,
        managementRate: 0,
        washRate: 0,
        startDate: null,
        endDate: null,
        dateRange: []
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams = {}
      this.handleQuery()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      if (id) {
        getStoreRateInfo(id).then((response) => {
          this.form = response.data
          this.form.dateRange = [response.data.startDate, response.data.endDate]
          this.open = true
        })
      } else {
        this.open = true
      }
      this.form.storeId = row.storeId
      this.title = '修改门店结算费率'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateStoreRate(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            saveStoreRate(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 时间选择
    selectDateRange() {
      if (this.form.dateRange != null) {
        this.form.startDate = this.form.dateRange[0]
        this.form.endDate = this.form.dateRange[1]
      }
    },
  }
}
</script>
