<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" size="mini">
        <el-form-item>
          <el-input v-model="queryParams.contact" placeholder="姓名/主办业务" clearable size="mini" style="width: 240px" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:maillist:add']">新增</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table v-loading="loading.tableLoading" :data="tableDataList" stripe size="mini">
      <el-table-column type="index" width="50" align="center" />
      <el-table-column label="姓名" align="center" prop="contact" show-overflow-tooltip />
      <el-table-column label="手机" align="center" prop="phone" show-overflow-tooltip />
      <el-table-column label="固话" align="center" prop="tel" show-overflow-tooltip />
      <el-table-column label="主办业务" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="操作" fixed="right" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:maillist:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDelete(scope.row)" v-hasPermi="['system:maillist:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getPageList" />

    <el-dialog title="通讯录" :visible.sync="modalVisible" width="500px" append-to-body :close-on-click-modal="false">
      <el-form :model="modalForm" ref="modalForm" :rules="modalRules" size="mini" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="contact">
              <el-input v-model="modalForm.contact" placeholder="请输入姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="modalForm.phone" placeholder="请输入联系电话"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="联系固话" prop="tel">
              <el-input v-model="modalForm.tel" placeholder="请输入联系固话"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="主办业务" prop="remark">
            <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="modalForm.remark" placeholder="请输入主办业务" />
          </el-form-item>
        </el-row>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="modalVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" v-loading="loading.modalSaveLoading" @click="handleSave">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
  
  <script>
import { getMaillistTableList, updateMaillist, insertMaillist, deleteMaillist } from "@/api/system/maillist";
import { validateMoney } from '@/utils/validate'
export default {
  name: 'Maillist',
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeName: undefined,
        status: undefined,
        deptId: this.$store.getters.schoolId,
        licenseType: undefined
      },
      loading: {
        tableLoading: false,
        modalSaveLoading: false
      },
      tableDataList: [],
      total: 0,
      statusOptions: [],
      licenseTypeOption: [],
      modalVisible: false,
      modalForm: {},
      modalRules: {
        contact: { required: true, message: '姓名不为空', trigger: 'blur' },
        originalPrice: { required: true, validator: validateMoney, trigger: 'blur' },
        currentPrice: { required: true, validator: validateMoney, trigger: 'blur' }
      },
    };
  },
  created() {
    this.getPageList();
  },
  methods: {
    // 搜索
    handleQuery() {
      this.queryParams.page = 1;
      this.getPageList();
    },
    getPageList() {
      this.loading.tableLoading = true;
      getMaillistTableList(this.queryParams).then(response => {
        this.tableDataList = response.rows;
        this.total = response.total;
        this.loading.tableLoading = false;
      });
    },
    // 重置搜索
    resetQuery() {
      this.queryParams.typeName = "";
      this.queryParams.status = undefined;
      this.handleQuery();
    },
    // 新增
    handleAdd() {
      this.modalVisible = true;
      this.modalForm = {
        typeName: '',
        status: '0',
        originalPrice: undefined,
        licenseType: 'C1',
        description: '',
        currentPrice: undefined
      }
    },
    // 编辑
    handleUpdate(listItem) {
      this.modalForm = Object.assign({}, listItem);
      this.modalVisible = true;
    },
    //  模态框保存
    handleSave() {
      this.$refs.modalForm.validate((valid) => {
        if (valid) {
          this.loading.modalSaveLoading = true;
          if (this.modalForm.mailId) {
            updateMaillist(this.modalForm).then(resp => {
              if (resp.code == 200) {
                this.$message.success('修改成功');
                this.modalVisible = false;
                this.loading.modalSaveLoading = false;
                this.getPageList();
              } else {
                this.$message.error('修改失败:' + resp.message);
                this.loading.modalSaveLoading = false;
              }
            })
          } else {
            this.$set(this.modalForm, 'deptId', this.$store.getters.schoolId);
            insertMaillist(this.modalForm).then(resp => {
              if (resp.code == 200) {
                this.$message.success('新增成功');
                this.modalVisible = false;
                this.loading.modalSaveLoading = false;
                this.getPageList();
              } else {
                this.$message.error('新增失败:' + resp.message);
                this.loading.modalSaveLoading = false;
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(listItem) {
      this.modalForm = Object.assign({}, listItem);
      const roleIds = this.modalForm.mailId || this.ids;
      this.$confirm('是否确认删除通讯录"' + this.modalForm.contact + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return deleteMaillist(roleIds);
      }).then(() => {
        this.getPageList();
        this.msgSuccess("删除成功");
      }).catch(function () { });
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
  }
};
  </script>