<template>
  <div class='m-container'>
    <!-- <h2>收费类型设置</h2> -->
    <div class="m-core">
      <save-form :inline="true" @submit="search" @reset="onReSet" :saveKey="'chargesList'" :model="searchForm">
        <el-form-item>
          <el-select
            placeholder="请选择分部"
            v-model="searchForm.organId"
            clearable
          >
            <el-option
              v-for="(item, index) in branchList"
              :label="item.label"
              :value="item.value"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            placeholder="收费类型"
            v-model="searchForm.chargeTypeId"
            clearable
            filterable
          >
            <el-option
              v-for="(item, index) in typesList"
              :label="item.label"
              :value="item.value"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="danger" native-type="submit">搜索</el-button>
          <el-button type="primary" native-type="reset">重置</el-button>
        </el-form-item>
      </save-form>
      <div @click="onChargeOperation('create')" v-permission="'chargeTypeOrganizationFee/add'"
           class='newBand'>添加</div>
      <div class="tableWrap">
        <el-table :data="dataList"
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <el-table-column prop="organId"
                           label="所属分部">
            <template slot-scope="scope">
              {{ branchById[scope.row.organId] }}
            </template>
          </el-table-column>
          <el-table-column prop="chargeTypeId"
                    label="乐团模式">
            <template slot-scope="scope">
              {{ typesById[scope.row.chargeTypeId] }}
            </template>
          </el-table-column>
          <el-table-column prop="name"
                           label="收费方式名称">
          </el-table-column>
          <el-table-column prop="totalCurrentPrice"
                           label="现价(元)">
            <template slot-scope="scope">
              {{ numeral(scope.row.totalCurrentPrice).format('0,0') }}
            </template>
          </el-table-column>
          <el-table-column prop="totalOriginalPrice"
                           label="原价(元)">
            <template slot-scope="scope">
              {{ numeral(scope.row.totalOriginalPrice).format('0,0') }}
            </template>
          </el-table-column>
          <el-table-column prop="updateTime"
                           label="修改时间">
          </el-table-column>
          <el-table-column align='center'
                           label="操作">
            <template slot-scope="scope">
              <el-button @click="onChargeOperation('update', scope.row)"
                         v-permission="'chargeTypeOrganizationFee/update'"
                         type="text">修改</el-button>
              <el-button @click="onChargeDelete(scope.row)"
                         v-permission="'chargeTypeOrganizationFee/delete'"
                         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>

      <el-dialog :title="formTitle[formActionTitle]"
                 :visible.sync="chargeStatus"
                 @close="closeModal"
                 destroy-on-close
                 width="850px">
        <chargesForm
          v-if="chargeStatus"
          :typesList="typesList"
          :branchList.sync="branchList"
          :rowDetail="rowDetail"
          @close="closeModal"
          @submited="getList"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import pagination from '@/components/Pagination/index'
import { chargeTypeList, chargeTypeOrganizationFeeAdd, musicGroupOrganizationCourseSettingsDel, chargeTypeOrganizationFeeUpdate, chargeTypeOrganizationFee, branchQueryPage, musicGroupOrganizationCourseSettingsQueryPage } from '@/api/specialSetting'
import chargesForm from './modals/chargesForm'
import numeral from 'numeral'
export default {
  components: {
    pagination,
    chargesForm
  },
  name: 'chargesList',
  data () {
    return {
      id: null,
      rowDetail: null,
      dataList: [],
      typesList: [], // 分部列表
      branchList: [], // 分部列表
      typesById: {},
      branchById: {},
      formActionTitle: 'create',
      formTitle: {
        create: '添加乐团收费标准',
        update: '修改乐团收费标准'
      },
      searchForm: {
        organId: null,
        chargeTypeId: null,
      },
      chargeStatus: false,
      formLabelWidth: '100px',
      form: {
        chargeTypeId: null,
        courseFee: null
      },
      rules: {
        courseFee: [{ required: true, message: '请输入课程费用', trigger: 'blur' }],
        chargeTypeId: [{ required: true, message: '请选择收费类型', trigger: 'change' }],
        organId: [{ required: true, message: '请选择所属分部', trigger: 'change' }]
      },
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      }
    }
  },
  async mounted () {
    this.getList()

    // 收费类型
    chargeTypeList({
      rows: 9999,
      page: 1
    }).then(res => {
      if (res.code == 200) {
        const typesById = {}
        res.data.rows.forEach(item => {
          typesById[item.id] = item.name
          this.typesList.push({
            label: item.name,
            value: item.id
          })
        })
        this.typesById = typesById
      }
    })

    // 分部列表
    branchQueryPage({
      rows: 9999,
      page: 1
    }).then(res => {
      if (res.code == 200) {
        const branchById = {}
        this.branchList = res.data.rows.map(item => {
          branchById[item.id] = item.name
          return ({
            label: item.name,
            value: item.id
          })
        })
        this.branchById = branchById
      }
    })
  },
  methods: {
    numeral,
    search() {
      this.pageInfo.page = 1;
      this.getList();
    },
    onReSet() {
      this.searchForm = {
        organId: null,
        chargeTypeId: null,
      };
      this.search();
    },
    onTypesSubmit (formName) { // 添加数据
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formActionTitle == 'create') {
            if (this.form.id) { // 判断有没有Id，如果有则删除
              delete this.form.id
            }
            chargeTypeOrganizationFeeAdd(this.form).then(res => {
              this.messageTips('添加', res)
            })
          } else if (this.formActionTitle == 'update') {
            chargeTypeOrganizationFeeUpdate(this.form).then(res => {
              this.messageTips('修改', res)
            })
          }
        } else {
          return false;
        }
      })
    },
    messageTips (title, res) {
      if (res.code == 200) {
        this.$message.success(title + '成功')
        this.chargeStatus = false
        this.getList()
      } else {
        this.$message.error(res.msg)
      }
    },
    onChargeOperation (type, row) {
      this.formActionTitle = type
      if (type == 'update') {
        this.rowDetail = { ...row }
      } else {
        this.rowDetail = null
      }
      this.chargeStatus = true
    },
    onChargeDelete (row) { // 删除
      this.$confirm('您确定删除该收费标准?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        musicGroupOrganizationCourseSettingsDel({ id: row.id }).then(res => {
          this.messageTips('删除', res)
        })
      }).catch(() => { })

    },
    getList () {
      musicGroupOrganizationCourseSettingsQueryPage({
        rows: this.pageInfo.limit,
        page: this.pageInfo.page,
        ...this.searchForm
      }).then(res => {
        if (res.code == 200) {
          this.dataList = res.data.rows
          this.pageInfo.total = res.data.total
        }
      })
    },
    onFormClose (formName) { // 关闭弹窗重置验证
      this.form = {
        courseFee: null,
        chargeTypeId: null
      }
      this.$refs[formName].resetFields()
    },
    closeModal() {
      this.chargeStatus = false
    }
  }
}
</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-date-editor.el-input {
  width: 100% !important;
}
/deep/.el-select {
  width: 98% !important;
}

/deep/.el-table {
  display: inline-block;
}
</style>
