<template>
  <div class="app-container">
      <div class="app-container-search main-bgcolor main-radius">
      <h1 class="">查询码仓</h1>
      <br />
    <!-- 搜索工作栏 -->
     <Bound
        ref="searchForm"
        :searchFormParam="{ queryParams, dataSource }"
        :showSearch.sync="showSearch"
        @handleQuery="handleQuery"
        @resetQuery="resetQuery"
      ></Bound>
    </div>

    <!-- 列表 -->
    <div class="app-container-list main-bgcolor main-radius shopping">
      <h1>码仓列表  </h1>
      <br/>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center" type="index" width="80" >
        <template v-slot="{ $index }">
          <span>{{($index+1)+ (queryParams.pageNo-1)*queryParams.pageSize }}</span>
        </template>
      </el-table-column>
      <!-- 列表 -->
      <el-table-column label="记录编号" align="center" prop="generateRecordId" >
        <template slot-scope="{ row }">
          <el-tooltip class="item" effect="dark" :content="row.generateRecordId" placement="top">
            <div class="flex-allcenter">
              <span class="more-width-20">{{row.generateRecordId}}</span>
              <el-button type="text" icon="el-icon-copy-document" @click.stop="copyContent(row.generateRecordId)"></el-button>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="码规则" align="center" prop="codeRuleFormat" >
        <template slot-scope="{ row }">
          <el-tooltip class="item" effect="dark" :content="row.codeRuleFormat" placement="top">
            <div class="flex-allcenter">
              <span class="more-width-20">{{row.codeRuleFormat}}</span>
              <el-button type="text" icon="el-icon-copy-document" @click.stop="copyContent(row.codeRuleFormat)"></el-button>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="所属商户" align="center" prop="belongBusinessName" />
      <el-table-column label="生成时间" align="center" prop="createTime" >
        <template v-slot="{ row }">
          <span>{{parseTime(row.createTime)}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remarks" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)"
                     >查看</el-button>
        </template>
      </el-table-column>
      <div slot="empty">
        <EmptyBound />
      </div>
    </el-table>

    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    </div>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="700px" v-dialogDrag append-to-body>
      <div>码值</div>
      <div class="flex-jcenter" style="margin: 20px 0;">
        <el-input style="width: 300px;" v-model="formDialog.identityCode" clearable placeholder="请输入"></el-input>
        <div>
          <el-button @click="resetFormDialog">重置</el-button>
          <el-button type="primary" @click="searchFormDialog">查询</el-button>
        </div>
      </div>
      <el-table
      :data="tableData"
      style="width: 100%">
        <el-table-column
          prop="index"
          label="序号"
          width="70">
          <template v-slot="{$index}">{{$index+1}}</template>
        </el-table-column>
        <el-table-column
          prop="identityCode"
          label="码值">
          <template slot-scope="{ row }">
            <el-tooltip class="item" effect="dark" :content="row.identityCode" placement="top">
              <div class="flex-acenter">
                <span class="more-width-20">{{row.identityCode}}</span>
                <el-button type="text" icon="el-icon-copy-document" @click.stop="copyContent(row.identityCode)"></el-button>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          prop="batchId"
          label="绑定批次">
        </el-table-column>
      </el-table>
      <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 Bound from '@/components/Customer/SearchForm'
import EmptyBound from '@/components/EmptyTable'
import { createBusinessRule, updateBusinessRule, deleteBusinessRule, getBusinessRule, getBusinessRulePage, exportBusinessRuleExcel } from "@/api/identity/businessRule";
import { getCodeList , getCodesByBelongBusinessId , getCodesByIdentityCode , getCodePage , getCodesByGenerateRecordId } from '@/api/identity/code';
import { getGenerateRecordPage } from "@/api/identity/generateRecord";

export default {
  name: "BusinessRule",
  components: {
    EmptyBound,
    Bound,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 码规则列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        businessId: null,
        businessName: null,
        codeRuleId: null,
        codeRuleName: null,
        codeType: null,
        codeRuleFormat: null,
        createTime: [],
        belongBusinessName: null,
        belongBusinessId: null,

      },
      dataSource: [
        {
          key: 'generateRecordId', //记录编码
          itemType: 'input',
          label: '记录编号',
          placeholder: '请输入',
          onEnterKeyUp: this.handleQuery,
        },
        {
          key: 'codeRuleFormat', //码规则
          itemType: 'input',
          label: '码规则',
          placeholder: '请输入',
          onEnterKeyUp: this.handleQuery,
        },
        {
          key: 'belongBusinessName', //所属商户
          itemType: 'input',
          label: '所属商户',
          placeholder: '请输入',
          onEnterKeyUp: this.handleQuery,
        },
        {
          key: 'createTime', //创建时间
          itemType: 'datePicker',
          label: '生成时间',
          placeholder: '请选择',
          onEnterKeyUp: this.handleQuery,
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        generateRecordId: [{ required: true, message: "记录编码不能为空", trigger: "blur" }],
        ruleId: [{ required: true, message: "码规则不能为空", trigger: "blur" }],
        belongBusinessNme: [{ required: true, message: "所属商户不能为空", trigger: "blur" }],
      },
      tableData: [],
      formDialog: {
        identityCode: undefined,
      },
      // 当前查看的belongBusinessId,
      watchId: 0,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      // getBusinessRulePage(this.queryParams).then(response => {
      getGenerateRecordPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        businessId: undefined,
        businessName: undefined,
        codeRuleId: undefined,
        codeRuleName: undefined,
        codeType: undefined,
        codeRuleFormat: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm", "searchForm");
      this.handleQuery();
    },
    async handleDetail(row){
      let res = await getCodesByGenerateRecordId({generateRecordId:row.generateRecordId});
      console.log('查询结果',res);
      this.watchId = row.generateRecordId;
      this.tableData = res.data;
      this.open = true;
      this.title = "查看";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateBusinessRule(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createBusinessRule(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除码规则编号为"' + id + '"的数据项?').then(function() {
          return deleteBusinessRule(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有码规则数据项?').then(() => {
          this.exportLoading = true;
          return exportBusinessRuleExcel(params);
        }).then(response => {
          this.$download.excel(response, '码规则.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    resetFormDialog(){
      this.formDialog = {
        identityCode: undefined,
      };
    },
    async searchFormDialog(){
      let form = {
        generateRecordId: this.watchId,
        identityCode: this.formDialog.identityCode,
      };
      let res = await getCodesByIdentityCode(form);
      this.tableData = res.data;
    },
  }
};
</script>
