<template>
  <el-dialog
    title="凭证选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="false"
    width="80%"
    center
  >
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="凭证号" prop="voucherNum">
        <el-input
          v-model="queryParams.voucherNum"
          placeholder="请输入凭证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="凭证类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择凭证类型"
          clearable
          size="small"
        >
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :value="item.value"
            :label="item.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="凭证日期" prop="voucherTime">
        <el-date-picker
          v-model="queryParams.voucherTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item label="摘要" prop="digest">
        <el-input
          v-model="queryParams.digest"
          placeholder="请输入摘要"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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-table
      v-loading="loading"
      :data="accountList"
      @current-change="handleCurrent"
      @row-dblclick="handleRowDbClick"
    >
      <el-table-column width="50" align="center">
        <template v-slot="scope">
          <el-radio
            v-model="selectedVoucherId"
            :label="scope.row.id"
            @change="handleRowChange(scope.row)"
            >{{ "" }}</el-radio
          >
        </template>
      </el-table-column>
      <el-table-column label="凭证号" align="center" prop="voucherNum" />
      <el-table-column label="凭证日期" align="center" prop="voucherTime" />
      <el-table-column label="摘要" align="center" prop="digest" />
      <el-table-column label="会计年份" align="center" prop="year" />
      <el-table-column label="会计月份" align="center" prop="period" />
      <el-table-column label="纸档位置" align="center" prop="position" />
      <el-table-column label="审核人" align="center" prop="audit" />
      <el-table-column label="制单人" align="center" prop="createBy" />
      <el-table-column label="凭证类型" align="center" prop="type">
        <template v-slot="scope">
          <dict-tag
            :type="DICT_TYPE.ACCOUNTING_VOUCHER_TYPE"
            :value="scope.row.type"
          />
        </template>
      </el-table-column>
      <el-table-column label="完整性" align="center" prop="cherks">
        <template v-slot="scope">
          <dict-tag
            :type="DICT_TYPE.ACCOUNTING_FILE_STATUS"
            :value="scope.row.cherks"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getVoucherPage } from "@/api/accounting/voucher";
import { listData } from "@/api/system/dict/data";
export default {
  name: "VoucherSelect",
  data() {
    return {
      showFlag: false,
      selectedVoucherId: undefined,
      selectedRow: undefined,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会计凭证
      accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        companyId: null,
        company: null,
        frequency: null,
        fileType: null,
        year: null,
        period: null,
        vouNum: null,
        caseNum: null,
        catalogNum: null,
        filesNum: null,
        site: null,
        flowId: null,
        createTime: [],
        status: null,
        note: null,
        fileStatus: null,
        borrowStatus: null,
        identifyStatus: null,
        remark: null,
        recordTime: [],
        deptId: null,
        deptName: null,
      },
      typeOptions: [], //凭证类别
    };
  },
  created() {
    this.getList();
    this.handleInitList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getVoucherPage(this.queryParams).then((response) => {
        this.accountList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    //获取字典数据
    handleInitList() {
      // 获取凭证类别
      listData({
        pageNo: 1,
        pageSize: 100,
        dictType: "accounting_voucher_type",
      }).then((res) => {
        this.typeOptions = res.data.list || [];
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRow = row;
      }
    },
    //行双击选中
    handleRowDbClick(row) {
      if (row) {
        this.selectedRow = row;
        this.$emit("onSelected", this.selectedRow);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRow = row;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedVoucherId == null || this.selectedVoucherId == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });
        return;
      }
      this.$emit("onSelected", this.selectedRow);
      this.showFlag = false;
    },
  },
};
</script>
