<!--
 * @Author: ysh
 * @Date: 2024-06-18 17:38:50
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-10-14 14:46:08
-->
<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单位名称" prop="partyAName">
        <!-- <el-select v-model="queryParams.partyAName" clearable filterable remote reserve-keyword placeholder="请输入单位名称"
          :remote-method="remoteMethod" :loading="queryLoading" style="width: 400px;" @change="handleQuery()">
          <el-option v-for="partyA in queryPartyAList" :key="partyA.partyAId" :label="partyA.partyAName"
            :value="partyA.partyAName">
          </el-option>
        </el-select> -->
        <el-autocomplete class="inline-input" clearable style="width: 400px;" v-model="queryParams.partyAName"
          :fetch-suggestions="querySearch" value-key="partyAName" placeholder="请输入内容"
          @select="handleQuery()"></el-autocomplete>
      </el-form-item>
      <el-form-item style="margin-bottom:0;display:none;"> <el-input></el-input> </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="partyA" v-loading="loading" :data="partyAList" :row-key="getRowKeys"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="partyAId" />
      <el-table-column label="单位名称" align="center" prop="partyAName" width="300px" />
      <el-table-column label="联系人" align="center" prop="aPerson" />
      <el-table-column label="联系电话" align="center" prop="aPhone" />
      <el-table-column label="备注说明" align="center" prop="remark" />
    </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 choosePartyA" style="margin: 5px;" :key="item.partyAId">{{ item.partyAName }}</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>
    <!-- 添加或修改cmc业主单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位名称" prop="partyAName">
          <el-input v-model="form.partyAName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="aPerson">
          <el-input v-model="form.aPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="aPhone">
          <el-input v-model="form.aPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input v-model="form.remark" 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 { listPartyA, getPartyA, delPartyA, addPartyA, updatePartyA } from "@/api/oa/partyA/partyA";

export default {
  name: "PartyA",
  props: {
    projectSource: {
      type: String,
      default: '2'
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      queryLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc业主单位表格数据
      partyAList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        partyAName: null,
        aPerson: null,
        aPhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        partyAName: [
          { required: true, message: '请输入单位名称', trigger: 'blur' },
        ]
      },
      choosePartyA: [],
      queryPartyAList: []
    };
  },
  created() {
    this.firstGetList();
    this.getList();
  },
  watch: {
    projectSource() {
      this.firstGetList();
      this.getList();
    }
  },
  methods: {
    firstGetList() {
      if (this.projectSource == '0') {
        this.$set(this.queryParams, 'partyAName', '中国电建集团成都勘测设计研究院有限公司')
      } else if (this.projectSource == '1') {
        this.$set(this.queryParams, 'partyAName', null)
      }
    },
    /** 查询cmc业主单位列表 */
    getList() {
      this.loading = true;
      listPartyA(this.queryParams).then(response => {
        this.partyAList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    remoteMethod(val) {
      listPartyA({
        pageNum: 1,
        pageSize: 30,
        partyAName: val
      }).then(res => {
        this.queryPartyAList = res.rows;
        this.queryLoading = false;
      })
    },
    querySearch(queryString, cb) {
      let partyAList = this.partyAList;
      let results = queryString ? partyAList.filter(this.createFilter(queryString)) : partyAList;
      cb(results)
    },
    createFilter(queryString) {
      return (partyA) => {
        return (partyA.partyAName.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partyAId: null,
        partyAName: null,
        aPerson: null,
        aPhone: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加甲方单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const partyAId = row.partyAId || this.ids
      getPartyA(partyAId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改甲方单位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partyAId != null) {
            updatePartyA(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPartyA(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    getRowKeys(row) {
      return row.partyAId;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.choosePartyA = selection
    },
    // 清空选择
    clearChoose() {
      this.$refs.partyA.clearSelection();
    },
    confirmChoose() {
      this.$emit('confirm', this.choosePartyA)
    },
  }
};
</script>
<style lang="scss" scoped></style>