<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="公司ID" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入公司ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="激活情况" prop="active">
        <el-select v-model="queryParams.active" placeholder="请选择激活情况">
          <el-option
            v-for="dict in dict.type.sub_active"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订阅计划ID" prop="subplanId">
        <el-input
          v-model="queryParams.subplanId"
          placeholder="请输入订阅计划ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订阅名称" prop="subplanName">
        <el-input
          v-model="queryParams.subplanName"
          placeholder="请输入订阅名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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>

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

    <el-table v-loading="loading" :data="subInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订阅ID" align="center" prop="id" v-if="true"/>
      <el-table-column label="公司ID" align="center" prop="tenantId" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="开始时间" align="center" prop="startTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="激活情况" align="center" prop="active" />
      <el-table-column label="订阅计划ID" align="center" prop="subplanId" />
      <el-table-column label="订阅名称" align="center" prop="subplanName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <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="['pay:subInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pay:subInfo: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input placeholder="请选择公司名称" readonly v-model="form.companyName" @focus="changeCompany" v-if="!form.id">
            <i slot="suffix" class="el-input__icon el-icon-search" style="color:#0f00d1; font-weight: bolder"></i>
          </el-input>
          <el-input v-model="form.companyName" placeholder="请输入公司名称" disabled v-if="form.id"/>
        </el-form-item>
        <el-form-item label="订阅名称" prop="subplanName">
          <el-input placeholder="请选择订阅名称" readonly v-model="form.subplanName" @focus="changePlan">
            <i slot="suffix" class="el-input__icon el-icon-search" style="color:#0f00d1; font-weight: bolder"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable style="width: 100%;"
            v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable style="width: 100%;"
            v-model="form.endTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="激活情况" prop="active">
          <el-select v-model="form.active" placeholder="请选择激活情况" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.sub_active"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择公司信息 -->
    <el-dialog title="公司选择" :visible.sync="company.open" width="900px" append-to-body top="6vh">
      <div>
        <el-form :model="company.queryParams" :inline="true" label-width="68px">
          <el-form-item label="公司名称" prop="name" style="margin-bottom: 10px">
            <el-input clearable
              v-model="company.queryParams.name"
              placeholder="请输入账号名称"
              clearablesize="small"
              @keyup.enter.native="gsInfoQuery"
            />
          </el-form-item>
          <el-form-item style="margin-bottom: 10px">
            <el-button type="primary" icon="el-icon-search" size="small" @click="gsInfoQuery">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-table border v-loading="company.loading" :data="company.list" @row-click="companyRowChange">
          <el-table-column label="序号" align="center" prop="index" width="50">
            <template slot-scope="scope">
              {{
                company.queryParams.pageSize ? scope.$index + 1 + company.queryParams.pageSize * (company.queryParams.pageNum - 1) : scope.$index + 1
              }}
            </template>
          </el-table-column>
          <el-table-column label="公司名称" align="center" prop="name" />
          <el-table-column label="联系人手机" align="center" prop="phone" />
          <el-table-column label="公司固话" align="center" prop="fixPhone" />
          <el-table-column label="公司地址" align="center" prop="address" />
          <el-table-column label="公司网址" align="center" prop="website" />
          <el-table-column label="备注" align="center" prop="remark" />
        </el-table>
        <pagination
          v-show="company.total>0"
          :total="company.total"
          :page.sync="company.queryParams.pageNum"
          :limit.sync="company.queryParams.pageSize"
          @pagination="gsInfoQuery"
        />
        <div slot="footer" class="dialog-footer">
          <el-button @click="companyCancel">取 消</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 选择订阅计划 -->
    <el-dialog title="订阅计划" :visible.sync="planInfo.open" width="900px" append-to-body top="6vh">
      <div>
        <el-form :model="planInfo.queryParams" :inline="true" label-width="68px">
          <el-form-item label="订阅名称" prop="name" style="margin-bottom: 10px">
            <el-input clearable
              v-model="planInfo.queryParams.name"
              placeholder="请输入订阅名称"
              clearablesize="small"
              @keyup.enter.native="planInfoQuery"
            />
          </el-form-item>
          <el-form-item style="margin-bottom: 10px">
            <el-button type="primary" icon="el-icon-search" size="small" @click="planInfoQuery">搜索</el-button>
          </el-form-item>
        </el-form>

        <el-table border v-loading="planInfo.loading" :data="planInfo.list" @row-click="planInfoRowChange">
          <el-table-column label="序号" fixed align="center" prop="index" width="50">
            <template slot-scope="scope">
              {{
                planInfo.queryParams.pageSize ? scope.$index + 1 + planInfo.queryParams.pageSize * (planInfo.queryParams.pageNum - 1) : scope.$index + 1
              }}
            </template>
          </el-table-column>
          <el-table-column label="订阅ID" align="center" prop="id" v-if="true"/>
          <el-table-column label="订阅名称" align="center" prop="name" />
          <el-table-column label="价格" align="center" prop="price" />
          <el-table-column label="计划描述" align="center" prop="description" />
          <el-table-column label="备注" align="center" prop="remark" />
        </el-table>

        <pagination
          v-show="planInfo.total>0"
          :total="planInfo.total"
          :page.sync="planInfo.queryParams.pageNum"
          :limit.sync="planInfo.queryParams.pageSize"
          @pagination="planInfoQuery"
        />

        <div slot="footer" class="dialog-footer">
          <el-button @click="planInfoCancel">取 消</el-button>
        </div>
      </div>

    </el-dialog>

  </div>
</template>

<script>
import { listSubInfo, getSubInfo, delSubInfo, addSubInfo, updateSubInfo } from "@/api/pay/subInfo";
import { listTenant } from "@/api/pay/tenant";
import { listSubPlan } from "@/api/pay/subPlan";

export default {
  name: "SubInfo",
  dicts: ['sub_active'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租户订阅信息表格数据
      subInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: undefined,
        companyName: undefined,
        startTime: undefined,
        endTime: undefined,
        active: undefined,
        subplanId: undefined,
        subplanName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "订阅ID不能为空", trigger: "blur" }
        ],
        tenantId: [
          { required: true, message: "公司ID不能为空", trigger: "blur" }
        ],
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        active: [
          { required: true, message: "激活情况不能为空", trigger: "blur" }
        ],
        subplanId: [
          { required: true, message: "订阅计划ID不能为空", trigger: "blur" }
        ],
        subplanName: [
          { required: true, message: "订阅名称不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      },
      /** 选择公司*/
      company: {
        // 是否显示弹出层
        open: false,
        // 列表遮罩层
        loading: true,
        // 列表查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null
        },
        // 表格数据
        list: [],
        // 总条数
        total: 0,
      },
      /** 选择订阅计划 */
      planInfo: {
        // 是否显示弹出层
        open: false,
        // 列表遮罩层
        loading: true,
        // 列表查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null
        },
        // 表格数据
        list: [],
        // 总条数
        total: 0,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询租户订阅信息列表 */
    getList() {
      this.loading = true;
      listSubInfo(this.queryParams).then(response => {
        this.subInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        tenantId: undefined,
        companyName: undefined,
        startTime: undefined,
        endTime: undefined,
        active: undefined,
        subplanId: undefined,
        subplanName: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      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.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSubInfo(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改租户订阅信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSubInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSubInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除租户订阅信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSubInfo(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pay/subInfo/export', {
        ...this.queryParams
      }, `subInfo_${new Date().getTime()}.xlsx`)
    },
    /** 选择公司信息*/
    changeCompany() {
      this.company.open = true;
      this.company.queryParams.pageNum = 1;
      this.company.queryParams.name = null;
      this.gsInfoQuery();
    },
    /** 公司信息列表查询 */
    gsInfoQuery() {
      this.company.loading = true;
      listTenant(this.company.queryParams).then(response => {
        this.company.list = response.rows;
        this.company.total = response.total;
        this.company.loading = false;
      });
    },
    /** 公司信息选择弹出框关闭*/
    companyCancel() {
      this.company.open = false
    },
    // 公司选择 选中数据
    companyRowChange(val) {
      this.form.tenantId = val.id;
      this.form.companyName = val.name;
      this.company.open = false;
    },
    /** 选择订阅计划*/
    changePlan() {
      this.planInfo.open = true
      this.planInfo.queryParams.pageNum = 1;
      this.planInfo.queryParams.name = null;
      this.planInfoQuery()
    },
    /** 订阅计划列表查询 */
    planInfoQuery() {
      this.planInfo.loading = true;
      listSubPlan(this.planInfo.queryParams).then(response => {
        this.planInfo.list = response.rows;
        this.planInfo.total = response.total;
        this.planInfo.loading = false;
      });
    },
    /** 订阅计划选择弹出框关闭*/
    planInfoCancel() {
      this.planInfo.open = false;
    },
    // 订阅计划 选中数据
    planInfoRowChange(val) {
      this.form.subplanId = val.id;
      this.form.subplanName = val.name;
      this.planInfo.open = false;
    },
  }
};
</script>
