<template>
  <div class='m-container'>
    <h2>
      <div class="squrt"></div>合作单位
    </h2>
    <div class="m-core">
      <div @click="openTeaching('create')"
           v-permission="'cooperationOrgan/add'"
           class='newBand'>新建</div>
      <!-- 搜索类型 -->
      <!-- <el-form :inline="true"
               class="searchForm"
               v-model.trim="searchForm">
        <el-form-item>
          <el-select v-model.trim="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'
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <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)"
                         v-permission="'cooperationOrgan/update'"
                         type="text">修改</el-button>
              <el-button @click="onDelete(scope.row)"
                         v-permission="'cooperationOrgan/del'"
                         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.trim="form.name"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所属分部"
                      prop="organId"
                      :label-width="formLabelWidth">
          <el-select v-model.trim="form.organId"
                     filterable
                     clearable>
            <el-option v-for="item in userBranchList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人"
                      prop="linkman"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.linkman"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="职位"
                      prop="job"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.job"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号"
                      prop="mobileNo"
                      :label-width="formLabelWidth">
          <el-input v-model.trim="form.mobileNo"
                    autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="权属类型" prop="ownershipType" :label-width="formLabelWidth">
          <el-select v-model.trim="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 { getEmployeeOrgan } from '@/api/buildTeam'
import store from '@/store'
import { isvalidPhone } from '@/utils/validate'
let validPhone = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入电话号码'))
  } else if (!isvalidPhone(value)) {
    callback(new Error('请输入正确的11位手机号码'))
  } else {
    callback()
  } 
}
export default {
  name: 'branchManager',
  components: { pagination },
  data () {
    return {
      searchForm: {
        status: ''
      },
      searchLsit: [],
      tableList: [],
      userBranchList: [], // 用户当前分部列表
      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: null
      },
      rules: {
        name: [{ required: true, message: '请输入教学点名称', trigger: 'blur' }],
        linkman: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        job: [{ required: true, message: '请输入职位', trigger: 'blur' }],
        mobileNo: [{ required: true, validator: validPhone, trigger: 'blur' },,{pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号',trigger: 'blur' }],
        organId: [{ required: true, message: '请选择所属分部', trigger: 'change' }]
        // ownershipType: [{ required: true, message: '请选择权属类型', trigger: 'change' }]
      },

    }
  },
  mounted () {
    this.getList() // 获取列表数据

    // 获取当前用户分部
    getEmployeeOrgan().then(res => {
      if (res.code == 200) {
        res.data.forEach(item => {
          this.userBranchList.push({
            label: item.name,
            value: item.id
          })
        })
      }
    })
  },
  methods: {
    onDelete (rows) {
      this.$confirm('您确定删除合作单位?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cooperationOrganDel(rows.id).then(res => {
          if (res.code == 200) {
            this.$message.success('删除成功')
            this.teachingStatus = false
            this.getList()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => { });

    },
    onTeachingSubmit (formName) { // 添加数据
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formActionTitle == 'create') {
            if (this.form.id) { // 判断有没有Id，如果有则删除
              delete this.form.id
            }
            cooperationOrganAdd(this.form).then(res => {
              if (res.code == 200) {
                this.$message.success('修改成功')
                this.teachingStatus = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else if (this.formActionTitle == 'update') {
            cooperationOrganUpdate(this.form).then(res => {
              if (res.code == 200) {
                this.$message.success('修改成功')
                this.teachingStatus = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        } else {
          return false;
        }
      })
    },
    getList () {
      queryPage({
        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.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 ? rows.mobileNo : null,
          ownershipType: rows.ownershipType,
          organId: rows.organId
        }
      }
    },
    onFormClose (formName) { // 关闭弹窗重置验证
      // this.$refs[formName].clearValidate()
      this.form = {
        id: null,
        name: null, // 教学点名称
        linkman: null, // 来源
        job: null,  // 费用
        mobileNo: null,
        // ownershipType: null,
        organId: null
      }
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-button--primary {
  background: #14928a;
  border-color: #14928a;
  color: #fff;
  &:hover,
  &:active,
  &:focus {
    background: #14928a;
    border-color: #14928a;
    color: #fff;
  }
}
/deep/.el-select {
  width: 100% !important;
}
.el-vue-search-box-container {
  position: absolute !important;
  left: 10px;
  margin-top: 10px;
  z-index: 99999 !important;
}
</style>