<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :permission="permissionList"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      :page.sync="page"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft"></template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getTitleList, getTitle, updateTitle } from "@/api/manage/fp";
import { mapGetters } from "vuex";

export default {
  name: "manage_company_fpList",
  components: {},
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: "auto",
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        addBtn: false,
        editBtn: true,
        delBtn: false,
        menuWidth: 300,
        dialogClickModal: false,
        dialogType: "drawer",
        column: [
          {
            label: "用工单位",
            prop: "companyName",
            type: "input",
            search: true,
            editDisplay: false,
          },
          {
            label: "纳税人识别号",
            prop: "regNumber",
            type: "input",
            editDisplay: false,
          },
          {
            label: "发票类型",
            prop: "type",
            type: "select",
            dicData: [
              { label: "增值税专用发票", value: 1 },
              { label: "增值税普通发票", value: 0 },
            ],
            search: true,
            searchSpan: 4,
            editDisplay: false,
          },
          {
            label: "银行账号",
            prop: "bankNumber",
            type: "input",
            editDisplay: false,
          },
        ],
        group: [
          {
            label: "",
            column: [
              {
                type: "input",
                label: "发票抬头",
                span: 24,
                display: true,
                size: "small",
                prop: "titleName",
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "发票抬头必须填写",
                  },
                ],
                placeholder: "请填写您的发票抬头",
              },
              {
                type: "radio",
                label: "发票类型",
                dicData: [
                  {
                    label: "增值税专用发票",
                    value: 1,
                  },
                  {
                    label: "增值税普通发票",
                    value: 0,
                  },
                ],
                span: 24,
                display: true,
                size: "small",
                prop: "type",
                rules: [
                  {
                    required: true,
                    message: "请选择发票类型",
                  },
                ],
              },
              {
                type: "input",
                label: "纳税人识别号",
                span: 24,
                display: true,
                size: "small",
                prop: "regNumber",
                readonly: false,
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "纳税人识别号必须填写",
                  },
                ],
                placeholder: "请填写18位有效社会纳税人识别号",
              },
              {
                type: "input",
                label: "开户银行",
                span: 24,
                display: true,
                size: "small",
                prop: "bankName",
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "开户银行必须填写",
                  },
                ],
                placeholder: "请填写您开户许可证上的开户银行名称",
              },
              {
                type: "input",
                label: "银行账号",
                span: 24,
                display: true,
                size: "small",
                prop: "bankNumber",
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "银行账号必须填写",
                  },
                ],
                placeholder: "请填写基本开户账号最长30位",
              },
              {
                type: "input",
                label: "注册地址",
                span: 24,
                display: true,
                size: "small",
                prop: "regAddress",
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "注册地址必须填写",
                  },
                ],
                placeholder: "请填写营业执照上面的注册地址",
              },
              {
                type: "input",
                label: "注册电话",
                span: 24,
                display: true,
                size: "small",
                prop: "regTelphone",
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "注册电话必须填写",
                  },
                ],
                placeholder: "请填写您公司有效的联系电话",
              },
            ],
          },
        ],
        labelWidth: 150,
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        editBtn: this.vaildData(
          this.permission.manage_company_fpList_edit,
          false
        ),
      };
    },
  },
  methods: {
    initData() {
      this.onLoad(this.page, this.query);
    },
    rowUpdate(row, index, done, loading) {
      updateTitle(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          // 数据回调进行刷新
          done(row);
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    beforeOpen(done, type) {
      if (["edit"].includes(type)) {
        this.loading = true;
        getTitle(this.form.id).then((res) => {
          this.loading = false;
          this.form = res.data.data;
          done();
        });
      }
    },
    beforeClose(done) {
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getTitleList(
        page.currentPage,
        page.pageSize,
        Object.assign(this.query, params)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
  },
};
</script>

<style>
</style>
