<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">

        <el-collapse v-model="activeNames">
          <!--   供应商基本信息     -->
          <el-collapse-item title="供应商基本信息" name="1">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="供应商名称" prop="name">
                  <el-input v-model="formData.name" placeholder="请输入供应商名称"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系人" prop="linkName">
                  <el-input v-model="formData.linkName" placeholder="请输入联系人"/>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="联系电话" prop="linkPhone">
                  <el-input v-model="formData.linkPhone" placeholder="请输入联系电话"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系方式" prop="phone">
                  <el-input v-model="formData.phone" placeholder="请输入联系方式"/>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="传真" prop="fax">
                  <el-input v-model="formData.fax" placeholder="请输入传真"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="结算方式" prop="billing">
                  <el-select v-model="formData.billing" placeholder="请选择结算方式">
                    <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PAY_TYPE)" :key="dict.value"
                               :label="dict.label" :value="dict.value" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="状态" prop="status">
                  <el-switch
                    v-model="formData.status"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="01"
                    inactive-value="02">
                  </el-switch>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="关联子公司" prop="corpId">
                  <el-select v-model="formData.corpIds" multiple placeholder="请选择">
                    <el-option
                      v-for="item in companyList"
                      :key="item.id"
                      :label="item.corpName"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="地址" prop="address">
                  <el-input type="textarea" v-model="formData.address" placeholder="请输入地址"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>

          <el-collapse-item title="银行账户" name="2">
            <el-table :data="formData.accountList" border size="small" style="width: 100%">
              <el-table-column prop="countName" align="left" width="180">
                <template slot="header">
                  <i style="color: #409EFF;cursor:pointer" class="el-icon-circle-plus-outline" @click="addItem"/>
                  开户名
                </template>
                <template v-slot="scope">
                  <el-input v-model="scope.row.countName" placeholder="请输入银行卡号"/>
                </template>
              </el-table-column>

              <el-table-column prop="countNo" align="center" label="银行卡号">
                <template v-slot="scope">
                  <el-input v-model="scope.row.countNo" placeholder="请输入银行卡号"/>
                </template>
              </el-table-column>

              <el-table-column prop="bankName" align="center" label="开户行">
                <template v-slot="scope">
                  <el-input v-model="scope.row.bankName" placeholder="请输入开户行"/>
                </template>
              </el-table-column>

              <el-table-column align="center" width="100" label="操作">
                <template v-slot="scope">
                  <el-button v-if="formData.accountList.length > 1" size="mini" type="text" icon="el-icon-delete"
                             @click="deleteItem(scope.$index, formData.accountList)">删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-collapse-item>
        </el-collapse>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as BaseSupplierApi from '@/api/repair/supplier';
import { getDictDatas, DICT_TYPE } from '@/utils/dict';
import {getCompanyList} from '@/api/base/company'

export default {
  name: "BaseSupplierForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      //折叠面板默认展开
      activeNames: ['1', '2'],
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        name: undefined,
        linkName: undefined,
        linkPhone: undefined,
        phone: undefined,
        fax: undefined,
        billing: undefined,
        sort: undefined,
        address: undefined,
        status: '01',
        remark: undefined,
        corpId: undefined,
        corpIds: [],
        accountList: [],
      },
      // 表单校验
      formRules: {
        name: [{required: true, message: '供应商名称不能为空', trigger: 'blur'}],
        linkName: [{required: true, message: '联系人不能为空', trigger: 'blur'}],
        linkPhone: [{required: true, message: '联系电话不能为空', trigger: 'blur'}],
      },
      companyList: []
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await BaseSupplierApi.getBaseSupplier(id);
          this.formData = res.data;
          this.title = "修改供应商";
        } finally {
          this.formLoading = false;
        }
      }
      //获取子公司
      await this.listCompany();
      this.title = "新增供应商";
    },
    /**查询子公司*/
    async listCompany() {
      const res = await getCompanyList()
      this.companyList = res.data
    },
    /**
     * 新增银行账户
     */
    addItem() {
      const item = {
        bankName: '',
        countName: '',
        countNo: ''
      }
      this.formData.accountList.push(item)
    },

    /**
     * 移除账户内容
     */
    deleteItem(index, rows) {
      rows.splice(index, 1);
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        this.formData.corpId = this.formData.corpIds + ''
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await BaseSupplierApi.updateBaseSupplier(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await BaseSupplierApi.createBaseSupplier(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        name: undefined,
        linkName: undefined,
        linkPhone: undefined,
        phone: undefined,
        fax: undefined,
        billing: undefined,
        sort: undefined,
        address: undefined,
        status: '01',
        remark: undefined,
        corpId: undefined,
        corpIds: [],
        accountList: [],
      };
      this.resetForm("formRef");
    }
  }
};
</script>
