<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="emailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column :label="$t('role.email')" prop="email"/>
      <el-table-column :label="$t('联系人')" prop="contact"/>
      <el-table-column :label="$t('是否启用')" prop="enableFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.option')" class-name="small-padding fixed-width" 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('role.email')" prop="email">
          <el-input v-model="form.email"/>
        </el-form-item>
        <el-form-item :label="$t('联系人')" prop="contact">
          <el-input v-model="form.contact"/>
        </el-form-item>
        <el-form-item :label="$t('是否启用')" prop="enableFlag">
          <el-select v-model="form.enableFlag">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </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 {addEmail, delEmail, getEmail, listEmail, updateEmail} from '@/api/mes/md/clientUrgeEmail'

export default {
  name: 'Email',
  dicts: ['sys_yes_no'],
  props: {
    clientCode: {type: String | undefined, default: undefined},
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 催货款通知邮箱表格数据
      emailList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientCode: null,
        email: null, contact: null, enableFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        email: [
          {required: true, trigger: 'blur'}
        ],
        contact: [
          {required: true, trigger: 'blur'}
        ],
        enableFlag: [
          {required: true, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    /** 查询催货款通知邮箱列表 */
    getList() {
      this.loading = true
      this.queryParams.clientCode = this.clientCode
      listEmail(this.queryParams).then(response => {
        this.emailList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        clientCode: this.clientCode,
        email: null,
        contact: null,
        enableFlag: 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
      getEmail(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) {
            updateEmail(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('修改成功'))
              this.open = false
              this.getList()
            })
          } else {
            this.form.clientCode = this.clientCode
            addEmail(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('common.confirm_del_data')).then(function () {
        return delEmail(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/email/export', {
        ...this.queryParams
      }, `email_${new Date().getTime()}.xlsx`)
    },
    init(form) {
      if (!form.clientCode) return
      this.queryParams.clientCode = form.clientCode
      this.getList()
    }
  }
}
</script>

<style scoped>

</style>
