<template>
  <el-row>
    <el-row>
      <el-form ref="searchForm" :model="searchForm" label-width="80px">
        <search-white-board :show-more="true" @search="list">
          <div slot="base">
            <el-row style="line-height: 40px; text-align: right;">
              <!-- <el-col :span="2"><label class="search-label">机构</label></el-col>
              <el-col :span="5">
                <select-organ v-model="searchForm.organIds" />
              </el-col> -->
              <el-col :span="2" :offset="1"><label class="search-label">学校名称</label></el-col>
              <el-col :span="5">
                <el-input v-model.trim="searchForm.name" placeholder="请输入学校名称" clearable />
              </el-col>
              <el-col :span="2" :offset="1"><label class="search-label">年级</label></el-col>
              <el-col :span="5">
                <el-select v-model="searchForm.gradeYear" placeholder="请选择年级" clearable filterable style="width: 100%;" @change="list">
                  <el-option v-for="item in constants.GRADE_YEAR_LIST" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-col>
            </el-row>
          </div>

          <div slot="more">
            <el-row style="line-height: 40px; text-align: right;">
              <el-col :span="2"><label class="search-label">是否删除</label></el-col>
              <el-col :span="5" style="text-align: left;">
                <el-switch v-model="searchForm.isDel" active-color="#13ce66" inactive-color="#ff4949" @change="list" />
              </el-col>
            </el-row>
          </div>
        </search-white-board>
      </el-form>
    </el-row>

    <el-row>
      <white-board>
        <el-row>
          <el-col :span="2" :offset="22" style="text-align: right;">
            <el-button icon="el-icon-plus" type="success" plain @click="handleAdd">新增</el-button>
          </el-col>
        </el-row>

        <el-row style="padding: 10px 0px;">
          <el-table :data="tableData" style="width: 100%;" highlight-current-row stripe>
            <el-table-column prop="organName" label="机构名称" show-overflow-tooltip />
            <el-table-column prop="province" label="省" show-overflow-tooltip />
            <el-table-column prop="city" label="市" show-overflow-tooltip />
            <el-table-column prop="area" label="区" show-overflow-tooltip />
            <el-table-column prop="name" label="学校名称" show-overflow-tooltip />
            <el-table-column label="年级">
              <template slot-scope="scope">
                <label v-if="scope.row.gradeYear === '1'">小学</label>
                <label v-if="scope.row.gradeYear === '2'">中学</label>
                <label v-if="scope.row.gradeYear === '3'">高中</label>
              </template>
            </el-table-column>
            <el-table-column prop="remarks" label="备注" show-overflow-tooltip />
            <el-table-column label="更新时间" width="135">
              <template slot-scope="scope">
                <el-row>{{ scope.row.updateByName }}</el-row>
                <el-row>{{ scope.row.updateAt }}</el-row>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="230" fixed="right">
              <template slot-scope="scope">
                <el-button type="primary" plain @click="handleLevelUp(scope.row)">升年级</el-button>
                <el-button type="primary" plain @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button v-if="scope.row.isDel" type="success" plain @click="handleIsDel(scope.$index, scope.row)">启用</el-button>
                <el-button v-if="!scope.row.isDel" type="danger" plain @click="handleIsDel(scope.$index, scope.row)">停用</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>

        <el-row>
          <el-pagination :current-page="searchForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="50" layout="total, sizes, prev, pager, next, jumper" :total="total" small @size-change="changeSizeChange" @current-change="changeCurrentChange" />
        </el-row>
      </white-board>
    </el-row>

    <el-dialog :visible.sync="dialogVisible">
      <el-divider />
      <label slot="title" style="float: left;">{{ dialogTitle }}</label>

      <el-row>
        <el-col :span="18" :offset="3">
          <el-form ref="form" :model="form" :rules="formRules" label-width="80px">
            <el-form-item label="机构" prop="organId">
              <select-organ v-model="form.organId" :multiple="false" />
            </el-form-item>
            <el-form-item label="省市区">
              <el-row style="text-align: left">
                <v-distpicker :province="form.province" :city="form.city" :area="form.area" @province="onChangeProvince" @city="onChangeCity" @area="onChangeArea" />
              </el-row>
            </el-form-item>
            <el-form-item label="学校名称" prop="name">
              <el-input v-model.trim="form.name" placeholder="请输入学校名称" clearable />
            </el-form-item>
            <el-form-item label="年级" prop="gradeYear">
              <el-select v-model="form.gradeYear" placeholder="请选择年级" clearable filterable style="width: 100%;">
                <el-option v-for="item in constants.GRADE_YEAR_LIST" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model.trim="form.remarks" type="textarea" placeholder="请输入备注" rows="3" maxlength="255" show-word-limit clearable />
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <el-divider />
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
  </el-row>
</template>

<script>
import VDistpicker from 'v-distpicker'
import { SelectOrgan } from '@/components/organ'
import schoolApi from '@/api/basics/schoolApi'

export default {
  name: 'School',
  components: {
    VDistpicker,
    'select-organ': SelectOrgan
  },
  data() {
    return {
      searchForm: {
        name: '',
        organIds: [],
        gradeYear: '',
        province: '',
        city: '',
        area: '',
        isDel: false,
        page: 1,
        size: 50
      },
      total: 0,
      tableData: [],
      dialogTitle: '',
      dialogVisible: false,
      form: {
        id: '',
        organId: '',
        name: '',
        gradeYear: '',
        remarks: '',
        province: '广东省',
        city: '广州市',
        area: ''
      },

      formRules: {
        name: [{ required: true, message: '请输入学校名称', trigger: 'blur' }],
        gradeYear: [{ required: true, message: '请选择年级', trigger: 'blur' }],
        organId: [{ required: true, message: '请选择机构', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.list()
  },
  methods: {
    cancelDialog() {
      this.dialogVisible = false
      this.$refs['form'].resetFields()
    },
    onChangeProvince(a) {
      this.form.province = a.value
    },
    onChangeCity(a) {
      this.form.city = a.value
    },
    onChangeArea(a) {
      this.form.area = a.value
    },
    handleAdd() {
      /* 重置表单 */
      this.form = {
        id: '',
        name: '',
        organId: '',
        gradeYear: '',
        remarks: '',
        province: '广东省',
        city: '广州市',
        area: ''
      }

      this.dialogTitle = '新增'
      this.dialogVisible = true
    },
    handleEdit(index, row) {
      /* 重置表单 */
      this.form['id'] = row.id
      this.form['city'] = row.city
      this.form['area'] = row.area
      this.form['name'] = row.name
      this.form['gradeYear'] = row.gradeYear
      this.form['remarks'] = row.remarks
      this.form['province'] = row.province
      this.form['organId'] = row.organId
      this.dialogTitle = '编辑'
      this.dialogVisible = true
    },
    handleIsDel(index, row) {
      schoolApi.handleIsDel(row.id).then(data => {
        this.$message({ message: '操作成功', type: 'success' })
        this.list()
      })
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }

        if (this.form.province === '省') {
          this.$message.error('请选择省')
          return
        } else if (this.form.city === '市') {
          this.$message.error('请选择市')
          return
        } else if (this.form.area === '区' || this.form.area === '') {
          this.$message.error('请选择区')
          return
        }

        schoolApi.saveOrUpdate(this.form).then(data => {
          this.$message({ message: '操作成功', type: 'success' })

          this.list()

          this.dialogVisible = false
          this.$refs['form'].resetFields()
        })
      })
    },
    list() {
      this.tableData = []
      schoolApi.page(this.searchForm).then(data => {
        this.total = data.total
        this.tableData = data.data
      })
    },
    changeSizeChange(val) {
      this.searchForm.size = val
      this.list()
    },
    changeCurrentChange(val) {
      this.searchForm.page = val
      this.list()
    },
    handleLevelUp(row) {
      const _that = this
      this.$confirm('该操作不能回滚，请谨慎操作', '提示', {
        confirmButtonText: '升年级',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        schoolApi.upLevel(row.id).then(data => {
          _that.$message({ message: '操作成功', type: 'success' })

          _that.list()
        })
      })
    }
  }
}
</script>

<style scoped>
   /deep/ .distpicker-address-wrapper select{
     padding: 0px;
     height: 28px;
     color: #606266;
     font-size: 0.5rem;line-height: 0.3
  }
</style>

