<!--
  @description: 选择供应商
  @Modifier: hdk
  @date: 2020-11-09 17:22:52
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.dialog"
    :before-close="onClose"
    title="选择供应商弹框"
    width="936px"
    append-to-body
    custom-class="el-dialog__body-p8 el-dialog_table"
  >
    <div class="clearfix">
      <div class="fl">
        <el-form
          ref="searchForm"
          :model="searchForm"
          label-width="140px"
          label-position="left"
          inline
        >
          <el-form-item label="供应商名称" prop="supplierName">
            <el-input v-model.trim="searchForm.supplierName" @keyup.enter.native="search" />
          </el-form-item>
          <el-form-item label="统一社会信用代码" prop="businesslicence">
            <el-input v-model.trim="searchForm.businesslicence" placeholder="" @keyup.enter.native="search" />
          </el-form-item>
        </el-form>
      </div>
      <div class="fr">
        <el-button type="primary" @click="search('searchForm')">
          查 询
        </el-button>
        <el-button plain type="info" @click="resetForm('searchForm')">
          重 置
        </el-button>
      </div>
    </div>
    <div>
      <div>
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="list"
          class="pointer"
          highlight-current-row
          @row-click="singleElection"
          @current-change="currentChange"
        >
          <!-- 选择-->
          <el-table-column label="选择" align="center" width="60">
            <template slot-scope="scope">
              <el-radio v-model="supplierId" :label="scope.$index">
                {{ '' }}
              </el-radio>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            type="index"
            label="序号"
            align="center"
            width="50"
          />
          <el-table-column
            header-align="center"
            prop="supplierName"
            min-width="230"
            align="left"
            label="供应商"
          />
          <el-table-column
            header-align="center"
            prop="businesslicence"
            label="统一社会信用代码"
            min-width="130"
            align="center"
          />
          <el-table-column
            header-align="center"
            prop="linkerName"
            align="left"
            min-width="110"
            label="联系人"
          />
          <el-table-column
            header-align="center"
            prop="linkerTel"
            align="center"
            label="联系方式"
          />
        </el-table>
      </div>
      <pagination :pagination-data="paginationData" @change="loadData" />
    </div>
    <!--底部操作按钮-->
    <span slot="footer" class="dialog-footer">
      <el-button :loading="confirmBtnLoading" type="primary" @click="ensure">确 认</el-button>
      <el-button :loading="confirmBtnLoading" @click="onClose">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { queryInvititationSupplierMessage } from '@/api/procurement/commonUser' // 获取供应信息
import { updateExecuteSupplier } from '@/api/procurement/purchasplan.js'

/* 公用方法*/
import { filterObj, restoreInitialValue } from '@/assets/js/utils' // 过滤空的key
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin' // 分页
/* 组件*/
import pagination from '@comp/common/pagination/pagination' // 分页
import { mapGetters } from 'vuex'
export default {
  name: 'SlelectSupplierDialog',
  components: {
    pagination // 分页
  },
  mixins: [ListMixin],
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: function() {
        return {}
      }
    },
    callBack: {
      type: Function,
      default: function() {
      }
    }
  },
  data() {
    return {
      loading: false,
      confirmBtnLoading: false, // btnLoading
      title: '项目类型',
      // 搜索信息
      searchForm: {
        invitationId: '',
        businesslicence: '', // 统一社会信用代码
        departId: '', // 机构id
        supplierName: '', // 供应商名称
        excludeSupplierIds: ''
      },
      list: [], // 列表数据
      supplierId: '', // 选择的id
      // 选择成交供应商参数
      concludeSupplier: {
        suppliersName: '', // 供应商名称
        suppliersValue: '' // 供应商id
      }
    }
  },
  computed: {
    ...mapGetters(['language'])
  },
  watch: {
    // 监听-弹框展示状态
    'dialogData.dialog'(val) {
      if (val) {
        // 加载数据
        this.loadData()
      }
    }
  },
  methods: {
    // 加载数据
    loadData() {
      this.loading = true
      this.supplierId = ''
      // 查询条件
      const params = this.getParams()
      queryInvititationSupplierMessage(params).then(response => {
        this.loading = false
        this.list = response.data.records
        this.paginationData.total = response.data.total
      }).catch(() => {
        this.loading = false
      })
    },
    // 查询条件
    getParams() {
      this.searchForm.excludeSupplierIds = this.dialogData.excludeSupplierIds
      this.searchForm.departId = this.$store.getters.info.companyId // 当前登录角色的机构id
      const param = Object.assign({}, this.searchForm)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      // 过滤空的key
      return filterObj(param)
    },
    // 选择信息-确定
    ensure() {
      // 判断是否选择供应商
      if (this.concludeSupplier.suppliersValue) {
        this.confirmBtnLoading = true // btnLoading
        this.saveUpdateExecuteSupplier()
      } else {
        this.$message.warning('请选择供应商')
      }
    },
    // 点击行设置单选按钮选中
    singleElection(row) {
      this.supplierId = this.list.indexOf(row)
    },
    // 当前选择的信息
    currentChange(val) {
      if (val) {
        this.concludeSupplier.suppliersValue = val.supplierId // 供应商id
        this.concludeSupplier.suppliersName = val.supplierName // 供应商名称
      }
    },
    /*
    * 保存选择的供应商
    * @params {executeId: 执行机构id,suppliersValue: 供应商id, suppliersName: 供应商名称}
    */
    saveUpdateExecuteSupplier() {
      this.confirmBtnLoading = true // btnLoading
      const data = {
        executeId: this.dialogData.executeId, // 执行机构id
        suppliersValue: this.concludeSupplier.suppliersValue, // 供应商id
        suppliersName: this.concludeSupplier.suppliersName // 供应商名称
      }
      updateExecuteSupplier(data).then(res => {
        this.confirmBtnLoading = false // btnLoading
        this.$emit('checkSupplier', this.concludeSupplier)
        this.clearmaterialId()
        this.dialogData.dialog = false // 关闭弹框
      }).catch(() => {
        this.confirmBtnLoading = false // btnLoading
      })
    },
    // 清空历史数据
    clearmaterialId() {
      restoreInitialValue(this.searchForm) // 清除数据
    },
    // 搜索
    search() {
      this.loading = true
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData()
    },
    // 重置
    resetForm(formName) {
      this.list = []
      this.$refs[formName].resetFields()
      // 获取列表信息
      this.loadData()
    },
    // 关闭
    onClose() {
      this.clearmaterialId()
      this.dialogData.dialog = false // 关闭弹框
    }
  }
}
</script>

<style scoped lang="less">

</style>
