<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.factoryId" placeholder="请选择" clearable filterable>
          <el-option v-for="factory in factoryList" :key="factory.id + factory.sourceType" :label="factory.name" :value="factory.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/factory/rate/list" :request-params="queryParams" ref="pagination">
      <el-table-column label="编号" align="center" prop="factoryId" width="100px" />
      <el-table-column label="工厂名称" align="center" prop="factoryName" />
      <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="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="depositRate">
        <template slot-scope="scope">
          <span v-if="scope.row.depositRate != null && scope.row.depositRate >= 0">{{(scope.row.depositRate).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:factory: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="materialRate">
              <el-input-number v-model="form.materialRate" :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="managementRate">
              <el-input-number v-model="form.managementRate" :precision="2" :step="0.01" :max="1" />
            </el-form-item>
            <el-form-item label="押金费率" prop="depositRate">
              <el-input-number v-model="form.depositRate" :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 { getFactoryRateInfo, saveFactoryRate, updateFactoryRate } from '@/api/settlement/factoryRate'
import { allOrg } from '@/api/system/store'

export default {
  name: 'FactoryRate',
  data() {
    return {
      orgList: [],
      factoryList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算费率表格数据
      rateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        factoryId: 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.factoryList.push(org)
        }
      })
    })
    this.getList()
  },
  methods: {
    /** 查询结算费率列表 */
    getList() {
      this.$nextTick(() => {
        this.$refs.pagination.handleSearch(true)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialRate: 0,
        managementRate: 0,
        depositRate: 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) {
        getFactoryRateInfo(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.factoryId = row.factoryId
      this.title = '修改工厂结算费率'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateFactoryRate(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            saveFactoryRate(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>
