<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="单位名称" prop="partnerName">
        <el-input v-model="queryParams.partnerName" placeholder="请输入单位名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="资质等级" prop="qualification">
        <el-input v-model="queryParams.qualification" placeholder="请输入资质等级" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table ref="partner" v-loading="loading" :data="partnerList" :row-key="getRowKeys"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单位名称" align="center" prop="partnerName" width="200px" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="资质等级" align="center" prop="qualification" />
      <el-table-column label="内业规模" align="center" prop="inWorker" />
      <el-table-column label="外业规模" align="center" prop="outWorker" />
      <el-table-column label="擅长业务方向" align="center" prop="strength" />
      <el-table-column label="履约能力评价" align="center" prop="fulfill" />
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" :autoScroll="false"
      @pagination="getList" />
    <div>
      已选单位：<el-tag v-for="item in partners" style="margin: 5px;" :key="item.partnerId">{{ item.partnerName }}</el-tag>
    </div>
    <div style="text-align: center;margin-top: 20px;">
      <el-button type="primary" @click="confirmChoose">确认选择</el-button>
      <el-button @click="clearChoose">清空选择</el-button>
    </div>
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12" :gutter="20">
            <el-form-item label="单位名称" prop="partnerName">
              <el-input v-model="form.partnerName" placeholder="请输入单位名称" />
            </el-form-item>
            <el-form-item label="主要联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
            </el-form-item>
            <el-form-item label="联系电话" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资质等级" prop="qualification">
              <el-input v-model="form.qualification" placeholder="请输入资质等级" />
            </el-form-item>
            <el-form-item label="内业规模" prop="inWorker">
              <el-input v-model="form.inWorker" placeholder="请输入内业规模" />
            </el-form-item>
            <el-form-item label="外业规模" prop="outWorker">
              <el-input v-model="form.outWorker" placeholder="请输入外业规模" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="主要技术装备" prop="equipment" label-width="120px">
          <el-input v-model="form.equipment" placeholder="请输入主要技术装备" />
        </el-form-item>
        <el-form-item label="擅长业务方向" prop="strength" label-width="120px">
          <el-input v-model="form.strength" placeholder="请输入擅长业务方向" />
        </el-form-item>
        <el-form-item label="履约能力评价" prop="fulfill" label-width="120px">
          <el-input v-model="form.fulfill" placeholder="请输入履约能力评价" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remark" label-width="120px">
          <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 4, maxRows: 10 }"
            placeholder="请输入备注说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPartner, getPartner, delPartner, addPartner, updatePartner } from "@/api/oa/partner/partner";

export default {
  name: "Partner",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc合作单位表格数据
      partnerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        partnerName: null,
        contactPerson: null,
        telephone: null,
        qualification: null,
        inWorker: null,
        outWorker: null,
        equipment: null,
        strength: null,
        fulfill: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        partnerName: [
          { required: true, message: '请输入单位名称', trigger: 'blur' },
        ]
      },
      partners: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc合作单位列表 */
    getList() {
      this.loading = true;
      listPartner(this.queryParams).then(response => {
        this.partnerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partnerId: null,
        partnerName: null,
        contactPerson: null,
        telephone: null,
        qualification: null,
        inWorker: null,
        outWorker: null,
        equipment: null,
        strength: null,
        fulfill: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合作单位";
    },
    handleView(row) {

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partnerId != null) {
            updatePartner(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPartner(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    getRowKeys(row) {
      return row.partnerName;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.partners = selection
    },
    // 清空选择
    clearChoose() {
      this.$refs.partner.clearSelection();
    },
    confirmChoose() {
      this.$emit('confirm', this.partners)
    },
  }
};
</script>

<style lang="scss" scoped></style>