<template>
  <div class='m-container'>
    <h2>
      <div class="squrt"></div>收费分润管理
    </h2>
    <div class="m-core">
      <div @click="chargeOperation('create')"
           class='newBand'>添加</div>
      <!-- <el-form :inline="true"
               class="searchForm"
               @submit.native.prevent
               v-model.trim="searchForm">
        <el-form-item>
          <el-select v-model.trim="form.organIdLists">
            <el-option v-for="item in branchList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="search"
                     type="danger">搜索</el-button>
        </el-form-item>
      </el-form> -->

      <!-- 列表 -->
      <div class="tableWrap">
        <el-table :data='tableList'
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <el-table-column align='center'
                           prop="organName"
                           label="分部">
          </el-table-column>
          <el-table-column align='center'
                           label="方式">
            <template slot-scope="scope">
              {{ scope.row.type == 1 ? '比例分润' : '收费类型' }}
            </template>
          </el-table-column>
          <el-table-column align='center'
                           label="详情">
            <template slot-scope="scope">
              <span v-if="scope.row.type == 1">
                比例：公{{ scope.row.comScale }} &nbsp;&nbsp;&nbsp; 私{{ scope.row.perScale }}
              </span>
              <span v-if="scope.row.type == 2">
                课程：{{ scope.row.courseFee == 'COM' ? '公' : '私' }}；
                乐器：{{ scope.row.instrumentFee == 'COM' ? '公' : '私' }}；
                辅件：{{ scope.row.accessoriesFee == 'COM' ? '公' : '私' }}；
                其它：{{ scope.row.otherFee == 'COM' ? '公' : '私' }}；
              </span>
            </template>
          </el-table-column>
          <el-table-column align='center'
                           width="250px"
                           label="操作">
            <template slot-scope="scope">
              <el-button @click="chargeOperation('update', scope.row)"
                         type="text">修改</el-button>
              <el-button @click="chargeDel(scope.row)"
                         type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination :total="pageInfo.total"
                    :page.sync="pageInfo.page"
                    :limit.sync="pageInfo.limit"
                    :page-sizes="pageInfo.page_size"
                    @pagination="getList" />
      </div>
    </div>

    <el-dialog :title="formTitle[formActionTitle]"
               :visible.sync="chargeStatus"
               @close="onFormClose('ruleForm')"
               width="500px">
      <el-form :model="form"
               :rules="rules"
               ref="ruleForm">
        <el-form-item label="收费方式"
                      required
                      :label-width="formLabelWidth">
          <el-radio v-model.trim.number="form.type"
                    @change="onRadioChange('ruleForm')"
                    :label="1">比例分润</el-radio>
          <el-radio v-model.trim.number="form.type"
                    @change="onRadioChange('ruleForm')"
                    :label="2">收费种类</el-radio>
        </el-form-item>

        <div v-if="form.type == 1">
          <el-form-item label="公"
                        prop="comScale"
                        :label-width="formLabelWidth">
            <el-input type="number"
                      @mousewheel.native.prevent
                      min="0"
                      max="10"
                      v-model.trim="form.comScale"></el-input>
          </el-form-item>
          <el-form-item label="私"
                        prop="perScale"
                        :label-width="formLabelWidth">
            <el-input type="number"
                      @mousewheel.native.prevent
                      min="0"
                      max="10"
                      v-model.trim="form.perScale"></el-input>
          </el-form-item>
        </div>

        <div v-if="form.type == 2">
          <el-form-item label="课程"
                        :label-width="formLabelWidth">
            <el-select placeholder="请选择方式"
                       v-model.trim="form.courseFee">
              <el-option label="公"
                         value="COM"></el-option>
              <el-option label="私"
                         value="PER"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="乐器"
                        :label-width="formLabelWidth">
            <el-select placeholder="请选择方式"
                       v-model.trim="form.instrumentFee">
              <el-option label="公"
                         value="COM"></el-option>
              <el-option label="私"
                         value="PER"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="辅件"
                        :label-width="formLabelWidth">
            <el-select placeholder="请选择方式"
                       v-model.trim="form.accessoriesFee">
              <el-option label="公"
                         value="COM"></el-option>
              <el-option label="私"
                         value="PER"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="其它"
                        :label-width="formLabelWidth">
            <el-select placeholder="请选择方式"
                       v-model.trim="form.otherFee">
              <el-option label="公"
                         value="COM"></el-option>
              <el-option label="私"
                         value="PER"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <el-form-item label="所属分部"
                      prop="organId"
                      :label-width="formLabelWidth">
          <el-select v-model.trim="form.organId">
            <el-option v-for="item in branchList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="chargeStatus = false">取 消</el-button>
        <el-button @click="onChargeSubmit('ruleForm')"
                   type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import pagination from '@/components/Pagination/index'
import { paymentConfigQueryPage, paymentConfigGet, paymentConfigAdd, paymentConfigUpdate, paymentConfigDel } from '@/api/systemManage'
import { branchQueryPage } from '@/api/specialSetting'
import store from '@/store'
// let validNumber = (rule, value, callback) => {
//   if (!value) {
//     callback(new Error('请输入数值'))
//   } else if (value > 10) {
//     callback(new Error('输入的值不能大于10'))
//   } else if(value < 0) {
//     callback(new Error('请入的值不能小于0'))
//   } else {
//     callback()
//   }
// }
export default {
  name: 'chargeProfitManager',
  components: { pagination },
  data () {
    return {
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      chargeStatus: false,
      roleList: [], // 角色列表
      branchList: [], // 分部列表
      formActionTitle: 'create',
      formLabelWidth: '100px',
      formTitle: {
        create: '添加收费分润',
        update: '修改收费分润'
      },
      form: {
        type: 1,
        comScale: null,
        perScale: null,
        courseFee: 'COM',
        instrumentFee: 'COM',
        accessoriesFee: 'COM',
        otherFee: 'COM',
        organId: null
      },
      rules: {
        comScale: [{ required: true, message: '请输入数值', trigger: 'blur' }],
        perScale: [{ required: true, message: '请输入数值', trigger: 'blur' }],
        organId: [{ required: true, message: '请选择所属分部', trigger: 'change' }]
      },
      searchForm: {
        search: null
      }
    }
  },
  mounted () {
    this.getList()
    this.getRoleList()
  },
  watch: {
    'form.comScale': {
      handler (newvalue, oldvalue) {
        if (newvalue) {
          this.form.perScale = 10 - newvalue
          if (newvalue > 10) {
            this.form.comScale = 10
          } else if (newvalue < 0) {
            this.form.comScale = 0
          }
        }
      }
    },
    'form.perScale': {
      handler (newvalue, oldvalue) {
        if (newvalue) {
          if (newvalue > 10) {
            this.form.perScale = 10
            this.form.comScale = 0
          } else if (newvalue < 0) {
            this.form.perScale = 0
            this.form.comScale = 10
          } else {
            this.form.comScale = 10 - newvalue
          }
        }
      }
    }
  },
  methods: {
    onRadioChange (formName) {
      this.$refs[formName].clearValidate()
    },
    onChargeSubmit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formActionTitle == 'create') {
            if (this.form.id) { // 判断有没有Id，如果有则删除
              delete this.form.id
            }
            paymentConfigAdd(this.form).then(res => {
              this.messageTips('添加', res)
            })
          } else if (this.formActionTitle == 'update') {
            paymentConfigUpdate(this.form).then(res => {
              this.messageTips('修改', res)
            })
          }
        } else {
          return
        }
      })
    },
    messageTips (title, res) {
      if (res.code == 200) {
        this.$message.success(title + '成功')
        this.chargeStatus = false
        this.getList()
      } else {
        this.$message.error(res.msg)
      }
    },
    search () {
      this.pageInfo.page = 1
      this.getList()
    },
    getList () {
      let params = this.searchForm
      params.rows = this.pageInfo.limit
      params.page = this.pageInfo.page
      paymentConfigQueryPage(params).then(res => {
        if (res.code == 200 && res.data) {
          this.tableList = res.data.rows
          this.pageInfo.total = res.data.total
        }
      })
    },
    getRoleList () { // 获取角色
      branchQueryPage({ // 获取分部
        delFlag: 0,
        rows: 9999
      }).then(res => {
        if (res.code == 200 && res.data && res.data.rows) {
          res.data.rows.forEach(item => {
            this.branchList.push({
              label: item.name,
              value: item.id
            })
          })
        }
      })
    },
    chargeOperation (type, data) {
      this.formActionTitle = type
      this.chargeStatus = true
      // 修改的时候
      if (type == 'update') {
        this.form = {
          id: data.id,
          type: Number(data.type),
          comScale: data.comScale,
          perScale: data.perScale,
          courseFee: data.courseFee,
          instrumentFee: data.instrumentFee,
          accessoriesFee: data.accessoriesFee,
          otherFee: data.otherFee,
          organId: data.organId
        }
      }
    },
    chargeDel (row) { // 删除数据
      // paymentConfigDel
      this.$confirm('你确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        paymentConfigDel({ id: row.id }).then(res => {
          this.messageTips('删除', res)
        })
      }).catch()
    },
    onFormClose (formName) { // 关闭弹窗重置验证
      this.form = {
        type: 1,
        courseFee: 'COM',
        instrumentFee: 'COM',
        accessoriesFee: 'COM',
        otherFee: 'COM'
      }
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/.el-button--primary {
  background: #14928a;
  border-color: #14928a;
  color: #fff;
  &:hover,
  &:active,
  &:focus {
    background: #14928a;
    border-color: #14928a;
    color: #fff;
  }
}
/deep/.el-dialog__body {
  padding: 0 20px;
}
/deep/.el-select,
/deep/.el-date-editor.el-input {
  width: 100% !important;
}
</style>