<template>
  <div class="base-info">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >{{$t('新增')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >{{$t('修改')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >{{$t('删除')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contactList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column :label="$t('品牌名称')" prop="brandName" width="150"/>
      <el-table-column :label="$t('role.department')" prop="department" width="100"/>
      <el-table-column :label="$t('职位')" prop="position" width="100"/>
      <el-table-column :label="$t('联系人')" prop="contact" width="100"/>
      <el-table-column :label="$t('电话号码')" prop="tel" width="150"/>
      <el-table-column label="email" prop="email" width="200"/>
      <el-table-column :label="$t('经营类型')" prop="businessType" width="100"/>
      <el-table-column :label="$t('工厂代号')" prop="factoryCode" width="100"/>
      <el-table-column :label="$t('跟单员')" prop="merchandiser" width="100"/>
      <el-table-column :label="$t('common.option')" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >{{$t('修改')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户联系人对话框 -->
    <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="$t('品牌名称')" prop="brandName" label-width="120px">
          <el-select
            v-model="form.brandName"
            filterable
            remote
           
            :remote-method="remoteMethod"
            :loading="loadingOptions">
            <el-option
              v-for="item in options"
              :key="item.brandId"
              :label="item.brandName"
              :value="item.brandName">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('role.department')" prop="department" label-width="120px">
          <el-input v-model="form.department"/>
        </el-form-item>

        <el-form-item :label="$t('职位')" prop="position" label-width="120px">
          <el-input v-model="form.position"/>
        </el-form-item>
        <el-form-item :label="$t('联系人')" prop="contact" label-width="120px">
          <el-input v-model="form.contact"/>
        </el-form-item>
        <el-form-item :label="$t('电话号码')" prop="tel" label-width="120px">
          <el-input v-model="form.tel"/>
        </el-form-item>
        <el-form-item label="email" prop="email" label-width="120px">
          <el-input v-model="form.email"/>
        </el-form-item>
        <el-form-item :label="$t('经营类型')" prop="businessType" label-width="120px">
          <el-input v-model="form.businessType"/>
        </el-form-item>
        <el-form-item :label="$t('工厂代号')" prop="spefactoryCodec" label-width="120px">
          <el-input v-model="form.factoryCode"/>
        </el-form-item>
        <el-form-item :label="$t('跟单员')" prop="merchandiser" label-width="120px">
          <el-input v-model="form.merchandiser"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {listContact, addContact, updateContact, getContact, delContact} from '@/api/mes/md/clientContact'
import {listBrandByCondition} from '@/api/mes/md/brand'


export default {
  props: {
    clientCode: {type: String | undefined, default: undefined},
  },
  data() {
    return {
      //远程搜索获取到的列表
      brandList: [],
      //远程搜索加载选项
      loadingOptions: false,
      //远程搜索选项
      options: [],
      //远程搜索参数
      remoteQuery: {
        brandName: null,
        enableFlag: "Y"
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户联系人表格数据
      contactList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // position: [
        //   {required: true, trigger: 'blur'}
        // ],
        // contact: [
        //   {required: true, trigger: 'blur'}
        // ],
        // tel: [
        //   {required: true, trigger: 'blur'}
        // ],
        businessType: [
          {required: true, trigger: 'blur'}
        ],
        // merchandiser: [
        //   {required: true, trigger: 'blur'}
        // ]

      }
    }
  },
  methods: {
    /** 查询客户联系人列表 */
    getList() {
      this.loading = true
      this.queryParams.clientCode = this.clientCode
      listContact(this.queryParams).then(response => {
        console.log(response, 111)
        this.contactList = response.rows
        this.total = response.total
      }).finally(() => this.loading = false)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        clientCode: this.clientCode,

        brandName: null,
        position: null,
        contact: null,
        tel: null,
        email: null,
        businessType: null,
        factoryCode: null,
        merchandiser: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = this.$t('添加客户联系人')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getContact(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = this.$t('修改客户联系人')
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
              updateContact(this.form).then(response => {
                this.$modal.msgSuccess(this.$t('修改成功'))
                this.open = false
                this.getList()
              })
          } else {
            this.form.clientCode = this.clientCode
            addContact(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('新增成功'))
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm(this.$t('是否确认删除？')).then(function () {
        return delContact(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/contact/export', {
        ...this.queryParams
      }, `contact_${new Date().getTime()}.xlsx`)
    },
    init(form) {
      if (!form.clientCode) return
      this.queryParams.clientCode = form.clientCode
      this.getList()
    },
    /** 远程搜索 */
    remoteMethod(query) {
      // 如果用户输入内容了，就发请求拿数据，远程搜索模糊查询
      if (query !== "") {
        this.remoteQuery.brandName = query;
        this.loadingOptions = true; // 开始拿数据喽
        // 这里模拟发请求，res就当成发请求返回来的数据吧。
        listBrandByCondition(this.remoteQuery).then(response => {
          if (response.code == 200) {
            this.options = response.data
          }
          this.loadingOptions = false // 拿到数据喽
        })
      } else {
        this.options = [];
      }
    }
  }
}
</script>

<style scoped>

</style>
