<template>
  <div class='m-container'>
    <h2>
      <div class="squrt"></div>合作单位
    </h2>
    <div class="m-core">
      <!-- 搜索类型 -->
      <save-form :inline="true"
               class="searchForm"
               @submit="search"
               @reset="onReSet"
               :model="searchForm">
        <el-form-item>
          <el-input v-model="searchForm.search" clearable
                    placeholder="请输入合作单位名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model.trim="searchForm.organId"
                     placeholder="请选择分部"
                     filterable
                     clearable>
            <el-option v-for="item in selects.branchs"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model.trim="searchForm.isEnable"
                     placeholder="请选择状态"
                     filterable
                     clearable>
            <el-option label="开启"
                       :value="true"></el-option>
            <el-option label="关闭"
                       :value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="danger">搜索</el-button>
          <el-button native-type="reset" type="primary">重置</el-button>
          <!-- export/isSettlementCourseSalarys  -->
          <el-button @click="onExport"
                     v-permission="'export/cooperationOrgan'"
                     type="primary">导出</el-button>
        </el-form-item>
      </save-form>
      <div @click="openTeaching('create')"
           v-permission="'cooperationOrgan/add'"
           class='newBand'>新建</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='name'
                           label="所属分部">
            <template slot-scope="scope">
              <div>
                {{ scope.row.organization.name }}
              </div>
            </template>
          </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'
                           label="是否全职资源">
            <template slot-scope="scope">
              {{ scope.row.fullJobResource ? '是' : '否' }}
            </template>
          </el-table-column>
          <el-table-column align='center'
                           label="状态">
            <template slot-scope="scope">
              <div>
                {{scope.row.isEnable?'开启':'关闭'}}
              </div>
            </template>
          </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 sync :total.sync="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 selects.branchs"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态"
                      prop="isEnable"
                      :label-width="formLabelWidth">
          <el-select v-model.trim="form.isEnable"
                     filterable
                     clearable>
            <el-option label="开启"
                       :value="true"></el-option>
            <el-option label="关闭"
                       :value="false"></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="fullJobResource"
                      :label-width="formLabelWidth">
          <el-select v-model.trim="form.fullJobResource">
            <el-option label="是"
                       :value="1"></el-option>
            <el-option label="否"
                       :value="0"></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 cleanDeep from 'clean-deep'
import store from '@/store'
import { isvalidPhone } from '@/utils/validate'
import { getToken } from "@/utils/auth";
import { Export } from '@/utils/downLoadFile'
import qs from "qs";
import axios from "axios";
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: {
        organId: null,
        isEnable: null,
        search: null
      },
      searchLsit: [],
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      teachingStatus: false, // 添加教学点
      formActionTitle: 'create',
      formTitle: {
        create: '添加合作单位',
        update: '修改合作单位'
      },
      formLabelWidth: '120px',
      form: {
        id: null,
        name: null, // 教学点名称
        linkman: null, // 来源
        job: null,  // 费用
        mobileNo: null,
        fullJobResource: 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' }],
        fullJobResource: [{ required: true, message: '请选择是否全职资源', trigger: 'change' }],
        isEnable: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },

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

    // 获取当前用户分部
    this.$store.dispatch('setBranchs')
  },
  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(() => { });

    },
    search () {
      this.pageInfo.page = 1;
      this.getList()
    },
    onReSet () {
      this.searchForm = {
        organId: null,
        isEnable: null
      }
      this.search()
    },
    onExport () {
      Export(this, {
        url: '/api-web/export/cooperationOrgan',
        fileName: '合作单位.xlsx',
        method: 'post',
        params: qs.stringify(cleanDeep(this.searchForm))
      }, '您确定导出合作单位?')
    },
    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,
        ...this.searchForm
      }).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,
          fullJobResource: rows.fullJobResource,
          isEnable: rows.isEnable
        }
      }
    },
    onFormClose (formName) { // 关闭弹窗重置验证
      // this.$refs[formName].clearValidate()
      this.form = {
        id: null,
        name: null, // 教学点名称
        linkman: null, // 来源
        job: null,  // 费用
        mobileNo: null,
        fullJobResource: null,
        organId: null,
        isEnable: 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>