<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="companyName">
        <el-input
        style="width: 220px"
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="状态" prop="status">
        <el-select style="width: 220px" v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['company:company:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['company:company:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['company:company:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['company:company:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  height="500" border v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="companyId" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="管理员帐号" width="100px" align="center" prop="userName" />
      <el-table-column label="余额" align="center" prop="money" />
      <el-table-column label="推广佣金" align="center" prop="tuiMoney" />
      <el-table-column label="联系人" align="center" prop="linkName" />
      <el-table-column label="联系电话" align="center" prop="companyMobile" />
      <el-table-column label="员工数量" align="center" prop="limitUserCount" />
      <el-table-column label="坐席数量" align="center" prop="voiceCallerNumber" />
      <el-table-column label="公司地址" align="center" prop="companyAddress" />
      <el-table-column label="通话API" align="center" prop="voiceApiName" />
      <!-- <el-table-column label="AppId" align="center" prop="appId" />
      <el-table-column label="AppKey" align="center" prop="appKey" /> -->
      <el-table-column label="类型" align="center" prop="companyType" >
        <template slot-scope="scope">
              <el-tag prop="companyType" v-for="(item, index) in typeOptions"     v-if="scope.row.companyType==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in statusOptions"  :type="scope.row.status==1?'success':'danger'"  v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
      </el-table-column>
      <!-- <el-table-column label="到期时间" align="center" prop="limitTime">
      </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark">
      </el-table-column>
      <el-table-column label="操作" align="center"  fixed="right" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            style="margin-left:10px"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['company:company:edit']"
          >编辑</el-button>
           
           <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleResetPwd(scope.row)"
            v-hasPermi="['company:company:resetPwd']"
          >重置密码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['company:company:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRecharge(scope.row)"
            v-hasPermi="['company:company:recharge']"
          >充值</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDeduct(scope.row)"
            v-hasPermi="['company:company:deduct']"
          >扣款</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="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="公司类型" prop="companyType">
           <el-select
              v-model="form.companyType"
              placeholder="请选择"
              clearable
              size="small"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="linkName">
          <el-input v-model="form.linkName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="companyMobile">
          <el-input v-model="form.companyMobile" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="员工数量" prop="limitUserCount">
          <el-input-number v-model="form.limitUserCount"  :min="1" :max="10000"  ></el-input-number>
        </el-form-item>
        <el-form-item label="坐席数量" prop="voiceCallerNumber">
          <el-input-number v-model="form.voiceCallerNumber"  :min="0" :max="10000"  ></el-input-number>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="选择快递" prop="omsCode">
              <el-select clearable v-model="form.omsCode"  placeholder="请选择快递名称"  size="small" @change="selectOmsCode">
                  <el-option
                    v-for="item in expressOptions"
                    :key="item.omsCode"
                    :label="item.name"
                    :value="item.omsCode"
                  />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="OMS编码" >
              <el-input v-model="erpForm.omsCode" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        
        
        <el-form-item  label="管理员帐号" prop="userName" v-if="form.companyId==null">
          <el-input v-model="form.userName" placeholder="请输入管理员帐号" />
        </el-form-item>
         <el-form-item  label="管理员密码" prop="password" v-if="form.companyId==null">
          <el-input type="password" v-model="form.password" placeholder="请输入管理员密码" />
        </el-form-item>
        <el-form-item label="地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
           <el-radio-group v-model="form.status">
            <el-radio v-for="dict in statusOptions" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="通话API接口" prop="voiceApiId">
           <el-select
              v-model="form.voiceApiId"
              placeholder="请选择"
              clearable
              size="small"
            >
              <el-option
                v-for="item in voiceApis"
                :key="item.apiId"
                :label="item.apiName"
                :value="item.apiId"
              />
            </el-select>
        </el-form-item>
         <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea"  :rows="2" placeholder="请输入备注" />
        </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>
    <el-dialog :title="recharge.title" :visible.sync="recharge.open" width="500px" append-to-body>
      <el-form ref="rechargeForm" :rules="rechargeRules" :model="rechargeForm"  label-width="80px">
        <el-form-item label="公司" >
          <el-input v-model="rechargeForm.companyName" disabled />
        </el-form-item>
        <el-form-item label="余额" >
          <el-input v-model="rechargeForm.balance" disabled />
        </el-form-item>
        <el-form-item label="充值金额" prop="money">
          <el-input-number v-model="rechargeForm.money" :min="0" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="rechargeForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRechargeForm">确 定</el-button>
        <el-button @click="recharge.open=false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="deduct.title" :visible.sync="deduct.open" width="500px" append-to-body>
      <el-form ref="deductForm" :rules="deductRules" :model="deductForm"  label-width="80px">
        <el-form-item label="公司" >
          <el-input v-model="deductForm.companyName" disabled />
        </el-form-item>
        <el-form-item label="余额" >
          <el-input v-model="deductForm.balance" disabled />
        </el-form-item>
        <el-form-item label="扣款金额" prop="money">
          <el-input-number v-model="deductForm.money" :min="0" placeholder="请输入扣款金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="deductForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDeductForm">确 定</el-button>
        <el-button @click="deduct.open=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {recharge,deduct, resetPwd,listCompany, getCompany, delCompany, addCompany, updateCompany, exportCompany } from "@/api/company/company";
import { getExpressList } from "@/api/store/express";
import {getVoiceApiList } from "@/api/company/companyVoiceApi";

export default {
  name: "Company",
  data() {
    return {
      // 表单参数
      rechargeForm: {
        money:0,
      },
      // 表单校验
      rechargeRules: {
        money: [
          { required: true, message: "充值金额不能为空", trigger: "blur" }
        ],
      },
      recharge:{
        open:false,
        title:"后台充值"
      },
      // 表单参数
      deductForm: {
        money:0,
      },
      // 表单校验
      deductRules: {
        money: [
          { required: true, message: "扣款金额不能为空", trigger: "blur" }
        ],
      },
      deduct:{
        open:false,
        title:"后台扣款"
      },
      packageIds: [],
      packages:[],
      typeOptions:[],
      voiceApis:[],
      statusOptions:[],
      expressOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        companyMobile: null,
        companyAddress: null,
        status: null,
        startTime: null,
        endTime: null,
        money: null,
        times: null,
        voiceApiId: null
      },
      // 表单参数
      form: {},
      erpForm:{
        omsCode:null
      },
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        companyType: [
          { required: true, message: "公司类型不能为空", trigger: "blur" }
        ],
        companyMobile: [
          { required: true, message: "公司电话不能为空", trigger: "blur" }
        ],
        companyAddress: [
          { required: true, message: "公司地址不能为空", trigger: "blur" }
        ],
        limitUserCount: [
          { required: true, message: "用户数量不能为空", trigger: "blur" }
        ],
        voiceCallerNumber: [
          { required: true, message: "坐席数量不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "帐号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        voiceApiId: [
          { required: true, message: "语音通话API不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("company_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("company_type").then((response) => {
      this.typeOptions = response.data;
    });
    getVoiceApiList().then((response) => {
      this.voiceApis = response.data;
    });
    getExpressList().then(response => {
      this.expressOptions = response.data;
       
    });
    this.getList();
  },
  methods: {
    selectOmsCode(){
      const item = this.expressOptions.find(item1=> item1.omsCode === this.form.omsCode)
      this.erpForm.omsCode = item.omsCode;
    },
    handleDeduct(row) {
      const companyId = row.companyId  
      this.deductForm.companyId=row.companyId;
      this.deductForm.companyName=row.companyName;
      this.deductForm.balance=row.money;
      this.deduct.open = true;
    },
    /** 提交按钮 */
    submitDeductForm() {
      this.$refs["deductForm"].validate(valid => {
        if (valid) {
          deduct(this.deductForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess(response.msg);
              this.deduct.open = false;
              this.getList();
            }
          });
        }
      });
    },
    handleRecharge(row) {
      const companyId = row.companyId  
      this.rechargeForm.companyId=row.companyId;
      this.rechargeForm.companyName=row.companyName;
      this.rechargeForm.balance=row.money;
      this.recharge.open = true;
    },
    /** 提交按钮 */
    submitRechargeForm() {
      this.$refs["rechargeForm"].validate(valid => {
        if (valid) {
          recharge(this.rechargeForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess(response.msg);
              this.recharge.open = false;
              this.getList();
            }
          });
        }
      });
    },
    /** 查询企业列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companyId: null,
        companyName: null,
        companyMobile: null,
        companyAddress: null,
        createTime: null,
        updateTime: null,
        status: '0',
        startTime: null,
        endTime: null,
        money: null,
        times: null,
        voiceApiId: null
      };
      this.packageIds=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.companyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "创建公司";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const companyId = row.companyId || this.ids
      getCompany(companyId).then(response => {
        this.form = response.data;
        this.form.status = response.data.status.toString();
        this.form.companyType = response.data.companyType.toString();
        if(this.form.packageIds!=null){
          var packageList=this.form.packageIds.split(",");
          packageList.forEach(element => {
            this.packageIds.push(parseInt(element))
          });
           
        }
        this.erpForm.omsCode = response.data.omsCode;
        this.open = true;
        this.title = "修改公司";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.companyId != null) {
            updateCompany(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCompany(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    
    handleResetPwd(row) {
      const companyIds = row.companyId || this.ids;
      this.$confirm('是否确认重复密码为123456?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return resetPwd(companyIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("修改成功");
        }).catch(function() {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companyIds = row.companyId || this.ids;
      this.$confirm('是否确认删除公司编号为"' + companyIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompany(companyIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有公司数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCompany(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    
    
  }
};
</script>
