<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-col>
          <el-form-item label="企业税号" prop="sellertax">
            <el-input v-model="queryParams.sellertax" placeholder="请输入企业税号" clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="企业名称" prop="sellername">
            <el-input v-model="queryParams.sellername" placeholder="请输入企业名称" clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="企业联系电话" prop="phone">
            <el-input v-model="queryParams.phone" placeholder="请输入企业联系电话" clearable size="small" @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 icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="companyserviceList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="企业编号" align="center" prop="companyid" :show-overflow-tooltip="true" />
      <el-table-column label="企业税号" align="center" prop="sellertax" :show-overflow-tooltip="true" />
      <el-table-column label="企业名称" align="center" prop="sellername" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleChose(scope.row)">选择
          </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="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="企业税号" prop="sellertax">
          <el-input v-model="form.sellertax" placeholder="请输入企业税号" />
        </el-form-item>
        <el-form-item label="企业名称" prop="sellername">
          <el-input v-model="form.sellername" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="注册地址" prop="selleraddress">
          <el-input v-model="form.selleraddress" placeholder="请输入注册地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="sellerphone">
          <el-input v-model="form.sellerphone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="开户行" prop="sellerbank">
          <el-input v-model="form.sellerbank" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="开户行账号" prop="selleraccount">
          <el-input v-model="form.selleraccount" placeholder="请输入开户行账号" />
        </el-form-item>
        <el-form-item label="企业联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入企业联系电话" />
        </el-form-item>
        <el-form-item label="加密secret" prop="secret">
          <el-input v-model="form.secret" placeholder="请输入加密secret" />
        </el-form-item>
        <el-form-item label="认证Identity" prop="identity">
          <el-input v-model="form.identity" placeholder="请输入认证Identity" />
        </el-form-item>
        <el-form-item label="开票员" prop="clerk">
          <el-input v-model="form.clerk" placeholder="请输入开票员" />
        </el-form-item>
        <el-form-item label="金税盘编码" prop="goldenTaxDiskNo">
          <el-input v-model="form.goldenTaxDiskNo" placeholder="请输入金税盘编码" />
        </el-form-item>
        <el-form-item label="复核员" prop="checker">
          <el-input v-model="form.checker" placeholder="请输入复核员" />
        </el-form-item>
        <el-form-item label="收款人" prop="payer">
          <el-input v-model="form.payer" placeholder="请输入收款人" />
        </el-form-item>
        <el-form-item label="回写地址" prop="rebackurl">
          <el-input v-model="form.rebackurl" placeholder="请输入回写地址" />
        </el-form-item>
        <el-form-item label="回写方式" prop="rebackType">
          <el-select v-model="form.rebackType" placeholder="请选择回写方式">
            <el-option v-for="dict in dict.type.invoice_reback_type" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分机号" prop="fjh">
          <el-input v-model="form.fjh" placeholder="请输入分机号" />
        </el-form-item>
        <el-form-item label="终端号" prop="terminalNumber">
          <el-input v-model="form.terminalNumber" placeholder="请输入终端号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompanyservice,
  getCompanyservice,
  delCompanyservice,
  addCompanyservice,
  updateCompanyservice
} from "@/api/system/companyservice";

export default {
  name: "Companyservice",
  dicts: ['invoice_reback_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销方信息表格数据
      companyserviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sellertax: null,
        sellername: null,
        phone: null,
        clerk: null,
        checker: null,
        payer: null,
        rebackType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销方信息列表 */
    getList() {
      this.loading = true;
      listCompanyservice(this.queryParams).then(response => {
        this.companyserviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companyid: null,
        sellertax: null,
        sellername: null,
        selleraddress: null,
        sellerphone: null,
        sellerbank: null,
        selleraccount: null,
        phone: null,
        status: "0",
        secret: null,
        identity: null,
        clerk: null,
        goldenTaxDiskNo: null,
        checker: null,
        payer: null,
        rebackurl: null,
        rebackType: null,
        fjh: null,
        terminalNumber: null,
        createUser: null,
        createDept: 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.companyid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 选中事件
    handleChose(row) {
      this.$emit("onChose", row)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销方信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const companyid = row.companyid || this.ids
      getCompanyservice(companyid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销方信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.companyid != null) {
            updateCompanyservice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompanyservice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companyids = row.companyid || this.ids;
      this.$modal.confirm('是否确认删除销方信息编号为"' + companyids + '"的数据项？').then(function () {
        return delCompanyservice(companyids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/companyservice/export', {
        ...this.queryParams
      }, `companyservice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
