<template>
  <div class="app-container">
    <div class="company-info-wrap">
      <div class="info-detail">
        <div class="info-item">
          <div class="item-label">所属公司:</div>
          <div class="item-content">{{ companyInfo.name }}</div>
        </div>
        <div class="info-item">
          <div class="item-label">信用代码:</div>
          <div class="item-content">{{ companyInfo.code }}</div>
        </div>
        <div class="info-item">
          <div class="item-label">联系人:</div>
          <div class="item-content">{{ companyInfo.contacts }}</div>
        </div>
      </div>
      <div class="info-detail">
        <div class="info-item">
          <div class="item-label">联系电话:</div>
          <div class="item-content">{{ companyInfo.phone }}</div>
        </div>
        <div class="info-item">
          <div class="item-label">会员到期时间:</div>
          <div v-if="companyInfo.isExpire == '0'" class="item-content">{{ companyInfo.expireTime }}</div>
          <div v-else><span>会员已到期</span></div>
        </div>
      </div>
    </div>
    <div class="tab-title" style="margin-left: -10px;padding-bottom: 20px">年度财务数据</div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="年度" prop="expireTime">
        <el-date-picker
          clearable
          v-model="queryParams.year"
          type="year"
          value-format="yyyy"
          placeholder="请选择年度"
        >
        </el-date-picker>
      </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-form-item>
        <el-button
          type="primary"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['buyer:info:add']"
        >新增
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="danger"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['buyer:info:remove']"
        >批量删除
        </el-button>
      </el-form-item>
    </el-form>

    <el-table stripe v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年度" align="center" prop="year" />
      <el-table-column label="总资产（万元）" align="center" prop="totalAssets" />
      <el-table-column label="销售收入（万元）" align="center" prop="saleRevenue" />
      <el-table-column label="附件数量" align="center" prop="buyerFileInfos">
        <template slot-scope="scope">
          {{ scope.row.buyerFileInfos.length }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['buyer:info:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['buyer:info:remove']"
          >删除
          </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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="年度" prop="year">
          <el-date-picker
            clearable
            v-model="form.year"
            type="year"
            value-format="yyyy"
            placeholder="请选择年度"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总资产（万元）" prop="totalAssets">
          <el-input v-model="form.totalAssets" type="number" placeholder="请输入总资产" />
        </el-form-item>
        <el-form-item label="销售收入（万元）" prop="saleRevenue">
          <el-input v-model="form.saleRevenue" type="number" placeholder="请输入销售收入" />
        </el-form-item>
        <el-form-item label="纳税申报表" prop="buyerFileInfos">
          <FileUploadMultiple v-model:value="form.buyerFileInfos" :limit="10" :file-size="10" :drag="true" :fileType="null"></FileUploadMultiple>
        </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 { listInfo, getInfo, delInfo, addInfo, updateInfo, checkYearExist } from '@/api/buyer/company'
import store from '@/store'

export default {
  name: 'Info',
  dicts: ['buyer_company_account_status', 'buyer_company_auth_status', 'buyer_company_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员企业信息表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        year: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: '请选择年度', trigger: 'blur' }
        ]
      },
      companyInfo: store.getters.companyInfo
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询会员企业信息列表 */
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams= {
        pageNum: 1,
          pageSize: 10,
          year: null
      }
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加会员企业信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data
        this.form.year = this.form.year.toString()
        this.open = true
        this.title = '修改会员企业信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            checkYearExist({ year: this.form.year }).then(res => {
              if (res.repeat) {
                this.$alert(res.msg, '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  addInfo(this.form).then(response => {
                    this.$modal.msgSuccess('新增成功')
                    this.open = false
                    this.getList()
                  })
                }).catch(() => {

                })
              } else {
                addInfo(this.form).then(response => {
                  this.$modal.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                })
              }
            })

          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除会员企业信息选中的数据项？').then(function() {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('buyer/company/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style scoped lang="scss">
.company-info-wrap {

  .info-detail {
    display: flex;

    .info-item {
      display: flex;
      width: 33.33%;
      align-items: center;
      color: #333333;
      font-size: 14px;
      padding: 8px 8px 8px 0px;

      .item-label {
        font-weight: bold;
      }

      .item-content {
        margin-left: 8px;
      }
    }
  }
}
</style>
