<!-- eslint-disable vue/valid-v-model -->
<template>
  <div style="margin-top: 10px;">
    <!-- 搜索工作栏 -->
    <el-form v-show="showSearch" ref="customeForm" :model="customeParams" size="small" :inline="true" label-width="100px">
      <el-form-item :label="$t('supplier.grouping')" prop="companyZh">
        <el-select v-model="customeParams.pid">
          <el-option
            v-for="item in groupList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('supplier.name')" prop="name">
        <el-input v-model="customeParams.name" :placeholder="$t('supplier.nameNotice')" clearable />
      </el-form-item>
      <el-form-item :label="$t('supplier.company')" prop="company">
        <el-input v-model="customeParams.company" :placeholder="$t('supplier.companyNotice')" clearable />
      </el-form-item>
      <el-form-item :label="$t('supplier.phone')" prop="phone">
        <el-input v-model="customeParams.phone" :placeholder="$t('supplier.phoneNotice')" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('common.search') }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('common.reload') }}</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"
        >{{ $t('common.add') }}</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 列表 -->
    <div style="margin-top: 16px">
      <el-table
        :data="SupplierList"
        header-cell-class-name="table-header"
      >
        <el-table-column
          type="index"
          width="70"
          :label="$t('supplier.index')"
          align="center"
          header-align="center"
        />
        <el-table-column
          :label="$t('supplier.grouping')"
          width="80"
          align="center"
          header-align="center"
        ><template slot-scope="scope">
          <span>{{ groupList.find(item=>item.id==scope.row.pid)?groupList.find(item=>item.id==scope.row.pid).title:'' }}</span>
        </template></el-table-column>
        <el-table-column
          prop="name"
          :label="$t('supplier.name')"
          align="center"
          header-align="center"
        />
        <el-table-column
          prop="company"
          :label="$t('supplier.company')"
          align="center"
          header-align="center"
        />
        <el-table-column
          prop="phone"
          :label="$t('supplier.phone')"
          align="center"
          header-align="center"
        />
        <el-table-column
          prop="createTime"
          :label="$t('supplier.createTime')"
          align="center"
          header-align="center"
        ><template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template></el-table-column>
        <el-table-column :label="$t('common.action')" align="center" header-align="center" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >{{ $t('common.update') }}</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >{{ $t('common.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="customeParams.page"
      :limit.sync="customeParams.rows"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="126px">
        <el-form-item :label="$t('supplier.name')" prop="name">
          <el-input v-model="form.name" :placeholder="$t('supplier.nameNotice')" />
        </el-form-item>
        <el-form-item :label="$t('supplier.company')" prop="company">
          <el-input v-model="form.company" :placeholder="$t('supplier.companyNotice')" />
        </el-form-item>
        <el-form-item :label="$t('supplier.maitou')" prop="maitou">
          <div v-for="( item, index) in maitouData" :key="index">
            <el-row :gutter="20" style="margin-top: 10px;margin-left: 10px;">
              <el-col :span="16"><el-input v-model="maitouData[index]" :placeholder="$t('supplier.maitouNotice')" /></el-col>
              <el-col :span="4"><el-button v-if="maitouData.length-1==index" type="success" icon="el-icon-plus" size="mini" circle @click="addMaitou" /></el-col>
              <el-col :span="4"><el-button v-if="maitouData.length-1!=index" type="danger" icon="el-icon-minus" size="mini" circle @click="delMaitou(index)" /></el-col>
            </el-row>
          </div>
        </el-form-item>
        <el-form-item :label="$t('supplier.phone')" prop="phone">
          <div v-for="( item, index) in phoneData" :key="index">
            <el-row :gutter="20" style="margin-top: 10px;margin-left: 10px;">
              <el-col :span="16"><el-input v-model="phoneData[index]" :placeholder="$t('supplier.phoneNotice')" /></el-col>
              <el-col :span="4"><el-button v-if="phoneData.length-1==index" type="success" icon="el-icon-plus" size="mini" circle @click="addPhone" /></el-col>
              <el-col :span="4"><el-button v-if="phoneData.length-1!=index" type="danger" icon="el-icon-minus" size="mini" circle @click="delPhone(index)" /></el-col>
            </el-row>
          </div>
        </el-form-item>
        <el-form-item :label="$t('supplier.email')" prop="email">
          <div v-for="( item, index) in emailData" :key="index">
            <el-row :gutter="20" style="margin-top: 10px;margin-left: 10px;">
              <el-col :span="16"><el-input v-model="emailData[index]" placeholder="supplier.emailNotice" /></el-col>
              <el-col :span="4"><el-button v-if="emailData.length-1==index" type="success" icon="el-icon-plus" size="mini" circle @click="addEmail" /></el-col>
              <el-col :span="4"><el-button v-if="emailData.length-1!=index" type="danger" icon="el-icon-minus" size="mini" circle @click="delEmail(index)" /></el-col>
            </el-row>
          </div>
        </el-form-item>
        <el-form-item :label="$t('supplier.address')" prop="address">
          <el-input v-model="form.address" :placeholder="$t('supplier.addressNotice')" />
        </el-form-item>
        <el-form-item :label="$t('supplier.groupBy')" prop="pid">
          <el-select v-model="form.pid">
            <el-option
              v-for="item in groupList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('supplier.submit') }}</el-button>
        <el-button @click="cancel">{{ $t('supplier.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSupplier, getSupplierList, createSupplier, updateSupplier, deleteSupplier, getGroupList } from '@/api/supplier'
import { getCountryList } from '@/api/user'
import { parseTime } from '@/utils'
import RightToolbar from '@/components/RightToolbar'
import Pagination from '@/components/Pagination'
export default {
  components: {
    RightToolbar,
    Pagination
  },
  data() {
    return {
      parseTime,
      SupplierList: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      customeParams: {
        page: 1,
        rows: 10,
        phone: null,
        name: null,
        company: null,
        pid: null
      },
      countryList: [],
      title: '',
      // 是否显示弹出层
      open: false,
      groupList: [],
      form: {},
      rules: {
        name: [{ required: true, message: this.$t('supplier.nameRules'), trigger: 'blur' }],
        company: [{ required: true, message: this.$t('supplier.companyRules'), trigger: 'blur' }]
      },
      maitouData: [''],
      phoneData: [''],
      emailData: ['']
    }
  },
  created() {
    this.getList()
    this.getGroup()
    getCountryList().then(r => {
      this.countryList = r.data
    })
  },
  methods: {
    getList() {
      getSupplierList(this.customeParams).then((r) => {
        this.SupplierList = r.data.list
        this.total = r.data.total
      })
    },
    getGroup() {
      getGroupList({ page: 1, rows: 100 }).then(r => {
        this.groupList = r.data.list
        this.groupList.push({ id: 0, title: this.$t('supplier.none') })
      })
    },
    addMaitou() {
      this.maitouData.push('')
    },
    delMaitou(index) {
      this.maitouData.splice(index, 1)
    },
    addPhone() {
      this.phoneData.push('')
    },
    delPhone(index) {
      this.phoneData.splice(index, 1)
    },
    addEmail() {
      this.emailData.push('')
    },
    delEmail(index) {
      this.emailData.splice(index, 1)
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = this.$t('supplier.addSupplier')
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
      }
      this.maitouData = ['']
      this.phoneData = ['']
      this.emailData = ['']
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.customeParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.customeParams = {}
      this.handleQuery()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        var maitou = this.maitouData.join()
        var phone = this.phoneData.join()
        var email = this.emailData.join()
        if (maitou === '') {
          this.$message.error(this.$t('supplier.maitouNotice'))
          return
        }
        if (phone === '') {
          this.$message.error(this.$t('supplier.phoneNotice'))
          return
        }
        this.form.maitou = maitou
        this.form.phone = phone
        this.form.email = email
        // 修改的提交
        if (this.form.id != null) {
          updateSupplier(this.form).then(response => {
            this.$message({
              message: this.$t('common.success'),
              type: 'success'
            })
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        createSupplier(this.form).then(response => {
          this.$message({
            message: this.$t('common.success'),
            type: 'success'
          })
          this.open = false
          this.getList()
        })
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getSupplier({ id: id }).then(response => {
        this.form = response.data
        this.maitouData = this.form.maitou.split(',')
        this.phoneData = this.form.phone.split(',')
        this.emailData = this.form.email.split(',')
        this.open = true
        this.title = this.$t('supplier.updateSupplier')
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$confirm(this.$t('supplier.deleteSupplier')).then(function() {
        return deleteSupplier(id)
      }).then(() => {
        this.getList()
        this.$message({
          message: this.$t('common.success'),
          type: 'success'
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
::v-deep .el-table__header .el-table__cell {
  background-color: #27417c;
  font-size: 13px;
  height: 38px;
  text-align: center;
  font-size: 13px;
  font-weight: 500;
  color: #ffffff;
}
</style>
