<template>
  <div class='m-container'>
    <h2>合作单位</h2>
    <div class="m-core">
      <div @click="openTeaching('create')" class='newBand'>新建</div>
      <!-- 搜索类型 -->
      <!-- <el-form :inline="true"
               class="searchForm"
               v-model="searchForm">
        <el-form-item>
          <el-select v-model="searchForm.status"
                     placeholder="工作类型">
            <el-option label="哈哈哈"
                       value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input suffix-icon="el-icon-search"
                    placeholder="请输入搜索内容"></el-input>
        </el-form-item>
      </el-form>
      <div class="searchWrap">
        <p>查询条件:</p>
        <div class="searchItem"
             @click="closeSearch(item)"
             v-for="(item,index) in searchLsit">
          {{ item.key }}
          <i class="el-icon-close"></i>
        </div>
      </div> -->
      <!-- 列表 -->
      <div class="tableWrap">
        <el-table :data='tableList'>
          <el-table-column align='center' prop='id'
                           label="单位编号">
          </el-table-column>
          <el-table-column align='center' prop='name'
                           label="单位名称">
          </el-table-column>
          <el-table-column align='center' prop='linkman'
                           label="联系人">
          </el-table-column>
          <el-table-column align='center' prop='job'
                           label="职位">
          </el-table-column>
          <el-table-column align='center' prop='mobileNo'
                           label="手机号">
          </el-table-column>
          <el-table-column align='center' prop='ownershipType'
                           label="权属类型">
            <template slot-scope="scope">
              {{ scope.row.ownershipType | branchType }}
            </template>
          </el-table-column>
          <el-table-column align='center'
                           label="操作" width='100'>
            <template slot-scope="scope">
              <el-button @click="openTeaching('update', scope.row)" type="text">修改</el-button>
              <el-button @click="onDelete(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="teachingStatus" @close="onFormClose('ruleForm')" width="500px">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="单位名称" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="linkman" :label-width="formLabelWidth">
          <el-input v-model="form.linkman" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="job" :label-width="formLabelWidth">
          <el-input v-model="form.job" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo" :label-width="formLabelWidth">
          <el-input v-model.number="form.mobileNo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权属类型" prop="ownershipType" :label-width="formLabelWidth">
          <el-select v-model="form.ownershipType">
              <el-option label="自有" value="OWN"></el-option>
              <el-option label="合作" value="COOPERATION"></el-option>
              <el-option label="租赁" value="LEASE"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="teachingStatus = false">取 消</el-button>
        <el-button type="primary" @click="onTeachingSubmit('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import pagination from '@/components/Pagination/index'
import { cooperationOrganAdd, cooperationOrganUpdate, cooperationOrganDel, queryPage } from '@/api/systemManage'
import store from '@/store'
export default {
  components: { pagination },
  name: 'adminManager',
  data () {
    return {
      searchForm: {
        status: ''
      },
      searchLsit: [],
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      teachingStatus: false, // 添加教学点 
      formActionTitle: 'create',
      formTitle: {
        create: '添加合作单位',
        update: '修改合作单位'
      },
      formLabelWidth: '100px',
      form: {
        id: null,
        name: null, // 教学点名称
        linkman: null, // 来源
        job: null,  // 费用
        mobileNo: null, 
        ownershipType: null,
        organId: store.getters.organ
      },
      rules: {
        name: [{ required: true, message: '请输入教学点名称', trigger: 'blur' }],
        linkman: [{required: true, message: '请输入联系人', trigger: 'blur'}],
        job: [{ required: true, message: '请输入职位', trigger: 'blur'}],
        mobileNo: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          { type: 'number', message: '手机号必须为数字'},
        ],
        ownershipType: [{ required: true, message: '请选择权属类型', trigger: 'change' }]
      },
      
    }
  },
  mounted() {
    this.getList() // 获取列表数据
  },
  methods: {
    onDelete(rows) {
      cooperationOrganDel(rows.id).then(res => {
        if(res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.teachingStatus = false
          this.getList()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    onTeachingSubmit(formName) { // 添加数据
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.formActionTitle == 'create') {
            cooperationOrganAdd(this.form).then(res => {
              if(res.code == 200) {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.teachingStatus = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else if(this.formActionTitle == 'update') {
            cooperationOrganUpdate(this.form).then(res => {
              // console.log(res)
              if(res.code == 200) {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.teachingStatus = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        } else {
          return false;
        }
      })
    },
    getList() {
      queryPage({
        organId: this.form.organId,
        rows: this.pageInfo.limit,
        page: this.pageInfo.page
      }).then(res => {
        if(res.code == 200 && res.data) {
          this.tableList = res.data.rows
          this.pageInfo.total = res.data.total
        }
      })
    },
    openTeaching(type, rows) {
      // 重置数据
      this.form = {
        id: null,
        name: null, // 教学点名称
        linkman: null, // 来源
        job: null,  // 费用
        mobileNo: null, // 合作单位
        ownershipType: null,
        organId: store.getters.organ
      }
      this.teachingStatus = true
      this.formActionTitle = type
      // 修改的时候赋值
      if(type == 'update') {
        this.form = {
          id: rows.id,
          name: rows.name, // 教学点名称
          linkman: rows.linkman, // 来源
          job: rows.job,  // 费用
          mobileNo: rows.mobileNo ? Number(rows.mobileNo) : null, // 合作单位
          ownershipType: row.ownershipType,
          organId: store.getters.organ
        }
      }
    },
    onFormClose(formName) { // 关闭弹窗重置验证
      this.$refs[formName].clearValidate()
    }
  }
}
</script>
<style lang="scss">
.el-input-group__append, .el-button--primary {
  background: #14928a;
  border-color: #14928a;
  color: #fff;
  &:hover, &:active, &:focus {
    background: #14928a;
    border-color: #14928a;
    color: #FFF;
  }
}
.el-vue-search-box-container {
  position: absolute !important;
  left: 10px;
  margin-top: 10px;
  z-index: 99999 !important;
}
</style>