<template>
  <div>
    <BaseDialog
      width="800px"
      :isShow.sync="isShow"
      :title="activeData.tenantId ? '修改' : '新增'"
      @submit="submitForm"
      @close="close"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formData"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="机构名称：" prop="tenantName">
          <el-input
            clearable
            v-model="formData.tenantName"
            placeholder="输入机构名称"
          ></el-input> </el-form-item
        ><el-form-item label="h5域名：" prop="hostH5"
          ><el-input
            clearable
            v-model="formData.hostH5"
            placeholder="输入h5域名"
          ></el-input></el-form-item
        ><el-form-item label="PC域名：" prop="hostPc"
          ><el-input
            clearable
            v-model="formData.hostPc"
            placeholder="输入PC域名"
          ></el-input
        ></el-form-item>
        <el-form-item label="账期设置：" required>
          <el-col :span="9">
            <el-form-item label="" prop="billType">
              <el-select
                v-model="formData.billType"
                placeholder="选择账期类型"
                clearable
              >
                <el-option label="月度" :value="1"> </el-option
                ><el-option label="季度" :value="2"> </el-option
                ><el-option label="半年" :value="3"> </el-option>
                <el-option label="年度" :value="4"> </el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="9"
            ><el-form-item label="" prop="billDay">
              <el-select
                v-model="formData.billDay"
                placeholder="选择次月"
                clearable
              >
                <el-option
                  v-for="(item, index) in days"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-form-item>
        <el-form-item label="收款账号：" required>
          <el-col :span="9">
            <el-form-item
              label=""
              :prop="'accountList.' + 0 + '.openingName'"
              :rules="rules['openingName']"
            >
              <el-input
                clearable
                v-model="formData.accountList[0].openingName"
                placeholder="输入开户名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item
              label=""
              :prop="'accountList.' + 0 + '.openingBank'"
              :rules="rules['openingBank']"
            >
              <el-input
                clearable
                v-model="formData.accountList[0].openingBank"
                placeholder="输入开户行"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item
              label=""
              :prop="'accountList.' + 0 + '.proceedsAccount'"
              :rules="rules['proceedsAccount']"
            >
              <el-input
                style="margin-top: 20px"
                clearable
                v-model="formData.accountList[0].proceedsAccount"
                placeholder="输入账号"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="开票信息：" required>
          <el-col :span="9">
            <el-form-item
              label=""
              :prop="'invoiceBo.' + 'invoiceType'"
              :rules="rules['invoiceType']"
            >
              <el-select
                v-model="formData.invoiceBo.invoiceType"
                placeholder="选择发票类型"
                clearable
              >
                <el-option label="普票" :value="1"> </el-option
                ><el-option label="专票" :value="2"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item
              label=""
              :prop="'invoiceBo.' + 'invoice'"
              :rules="rules['invoice']"
              ><el-input
                clearable
                v-model="formData.invoiceBo.invoice"
                placeholder="输入发票抬头(企业或个人)"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="9">
            <el-form-item
              label=""
              :prop="'invoiceBo.' + 'taxpayer'"
              :rules="rules['taxpayer']"
            >
              <el-input
                style="margin-top: 20px"
                clearable
                v-model="formData.invoiceBo.taxpayer"
                placeholder="输入纳税人"
              ></el-input> </el-form-item
          ></el-col>
        </el-form-item>
      </el-form>
    </BaseDialog>
  </div>
</template>

<script>
export default {
  name: "",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    activeData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      formData: {
        accountList: [{}],
        invoiceBo: {},
      },
      rules: {
        tenantName: [
          { required: true, message: "请输入机构名称", trigger: "blur" },
        ],
        billType: [
          { required: true, message: "请选择账期类型", trigger: "change" },
        ],
        billDay: [
          { required: true, message: "请选择次月日期", trigger: "change" },
        ],
        openingName: [
          { required: true, message: "请输入开户名", trigger: "blur" },
        ],
        openingBank: [
          { required: true, message: "请输入开户行", trigger: "blur" },
        ],
        proceedsAccount: [
          { required: true, message: "请输入账号", trigger: "blur" },
        ],
        invoiceType: [
          { required: true, message: "请选择发票类型", trigger: "change" },
        ],
        invoice: [
          { required: true, message: "请输入发票抬头", trigger: "blur" },
        ],
        taxpayer: [
          { required: true, message: "请输入纳税人", trigger: "blur" },
        ],
      },
      days: [],
    };
  },
  mounted() {
    this.getDays();
  },

  methods: {
    //获取天数 从第6天算起 至31日
    getDays() {
      for (let i = 6; i <= 31; i++) {
        this.days.push({ label: i + "日", value: i });
      }
    },
    init() {
      this.formData = JSON.parse(JSON.stringify(this.activeData));
      this.formData.accountList = this.formData.accountList || [{}];
      this.formData.invoiceBo = this.formData.invoiceBo || {};
    },
    close() {
      this.$refs["formData"].resetFields();
    },
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$api[
            this.activeData.tenantId ? "systemtenantedittop" : "systemtenantadd"
          ](this.formData).then((res) => {
            this.$message.success("成功");
            this.isShow = false;
            this.$emit("search");
          });
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    isShow: {
      get() {
        if (this.dialogVisible) {
          this.init();
        }
        return this.dialogVisible;
      },
      set(val) {
        this.$emit("update:dialogVisible", false);
      },
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 220px;
  margin-right: 10px;
}
.el-select {
  width: 220px;
  margin-right: 10px;
}
</style>
