<template>
  <div>
    <el-card >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="单位名称" prop="unitName">
          <el-input
            v-model="queryParams.unitName"
            placeholder="请输入单位名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="挂账人" prop="personCredit">
          <el-input
            v-model="queryParams.personCredit"
            placeholder="请输入挂账人名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactMobile">
          <el-input
            v-model="queryParams.contactMobile"
            placeholder="请输入挂账人联系电话"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="全部"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.zhzt"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="margin-top: 20px">
      <div>
        <el-button
          type="primary"
          @click="handleAdd"
        >新增挂账单位</el-button>
      </div>

      <el-table ref="tables" v-loading="loading" :data="list">
        <el-table-column type="index" width="80" align="center" label="序号"/>
        <el-table-column label="单位名称" prop="unitName" align="center" width="120"/>
        <el-table-column label="挂账人" prop="personCredit" align="center" width="160"/>
        <el-table-column label="联系电话" prop="contactMobile" align="center" width="120"/>
        <el-table-column label="预设挂账额度" prop="creditLimit" align="center" width="160"/>
        <el-table-column label="已使用挂账额度" prop="usedCreditLimit" align="center" width="160"/>
        <el-table-column label="剩余可挂账额度" prop="residueCreditLimit" align="center" width="160"/>
        <el-table-column label="备注" prop="remark" align="center" width="120"/>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status=='qy'">启用</el-tag>
            <el-tag type="info" v-if="scope.row.status=='jy'">禁用</el-tag>
            <!--            <dict-tag :options="dict.type.zhzt" :value="scope.row.status"/>-->
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" width="160" prop="createTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="190" fixed='right'>
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!--      新增挂账单位信息-->
    <el-dialog :title="title" :visible.sync="open1" width="700px" append-to-body>
      <el-form ref="formName" :model="form1" :rules="rules1" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="挂账单位" prop="unitName">
              <el-input v-model="form1.unitName" show-word-limit placeholder="请输入挂账单位名称" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="负责人" prop="personCredit">
              <el-input v-model="form1.personCredit" show-word-limit placeholder="请输入挂账单位负责人姓名" maxlength="10" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="联系电话" prop="contactMobile">
              <el-input v-model="form1.contactMobile" show-word-limit placeholder="请输入挂账单位联系电话" maxlength="15" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="预设挂账额度" prop="creditLimit">
              <el-input v-model="form1.creditLimit" @input="changeCreditAmount" placeholder="请输入预设挂账额度" maxlength="10">
                <template slot="append">元</template>
              </el-input>
              <span style="font-size: 12px;color: grey">
                额度为当前单位最大可挂账金额,如已挂账金额归还,额度将也同步返还
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="已使用挂账额度" prop="creditLimit">
              <el-input v-model="form1.usedCreditLimit" disabled placeholder="0" maxlength="10">
                <template slot="append">元</template>
              </el-input>
              <span style="font-size: 12px;color: grey">
                额度为当前单位最大可挂账金额,如已挂账金额归还,额度将也同步返还
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="剩余挂账额度" prop="creditLimit">
              <el-input v-model="form1.residueCreditLimit" disabled placeholder="0" maxlength="10">
                <template slot="append">元</template>
              </el-input>
              <span style="font-size: 12px;color: grey">
                额度为当前单位最大可挂账金额,如已挂账金额归还,额度将也同步返还
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form1.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="24">
            <el-form-item label="单位状态" prop="status">
              <el-radio-group v-model="form1.status">
                <el-radio label="qy" value="qy">启用</el-radio>
                <el-radio label="jy" value="jy">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="open1 = false">取 消</el-button>
        <el-button type="primary" @click="addCredit">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {
  addCreditUnit,
  creditUnitInfo,
  deleteCreditUnit,
  editCreditUnit,
  listsCreditUnit
} from "@/api/staff/user/creditunit";

export default {
  dicts: ['zhzt'],
  data(){
    return {
      list:[],
      loading:false,
      total:0,
      form1:{},
      open1:false,
      title:"",
      queryParams:{
        page:1,
        pageSize: 10,
        unitName:"",
        personCredit:"",
        contactMobile:"",
        status:"",
      },
      rules1: {
        unitName: [ { required: true, message: "请填写挂账单位名称", trigger: "blur" }, ],
        personCredit: [{ required: true, message: "请填写挂账单位负责人姓名", trigger: "blur" }],
        contactMobile: [ { required: true, message: "请填写挂账单位联系电话", trigger: "blur" }, ],
        creditLimit: [ { required: true, message: "请填写挂账额度", trigger: "blur" }, ],
        status: [ { required: true, message: "请选择挂账单位状态", trigger: "blur" }, ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    changeCreditAmount(){
      if (this.title = "新增挂账单位") {
        this.form1.usedCreditLimit = this.form1.creditLimit
        this.form1.residueCreditLimit = 0
      }
      if (this.title = "修改挂账单位") {
        this.form1.usedCreditLimit = this.form1.creditLimit - this.form1.residueCreditLimit
      }
    },
    getList(){
      this.loading = true;
      listsCreditUnit(this.queryParams).then(res => {
        this.list = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      })
    },
    handleAdd(){
      this.title = "新增挂账单位"
      this.open1 = true
    },
    handleUpdate(data){
      creditUnitInfo(data.id).then(res => {
        this.form1 = res.data
        this.title = "修改挂账单位"
        this.open1 = true
      })
    },
    handleDelete(row){
      const name = row.unitName
      this.$modal.confirm('确定删除"' + name + '"的挂账单位信息？').then(function() {
        // return deleteMember(row.id);
        return deleteCreditUnit(row.id);
      }).then(() => {
        this.queryParams.page = 1
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 添加挂账单位信息
    addCredit(){
      this.$refs["formName"].validate((valid) => {
        if (valid) {
          if (this.form1.id){
            if (this.form1.creditLimit < this.form1.residueCreditLimit){
              this.$modal.msgError("请确保预设挂账额度大于剩余挂账额度");
              return ;
            }
            editCreditUnit(this.form1).then(res => {
              if (res.data==1) {
                this.$modal.msgSuccess("挂账单位信息修改成功");
                this.open1 = false;
                this.getList();
              }else {
                this.$modal.msgError("挂账单位信息修改失败");
              }
            })
          }else {
            addCreditUnit(this.form1).then( response => {
              if (response.data==1) {
                this.$modal.msgSuccess("挂账单位信息创建成功");
                this.open1 = false;
                this.getList();
              }else {
                this.$modal.msgError("挂账单位信息创建失败，挂站单位名称不可重复");
              }
            })
          }

        }else {
          return false;
        }
      })
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
}
</script>

<style lang="scss" scoped>

</style>
