<template>
  <div class="await">
    <div class="search">
      <BaseNewForm
        ref="searchForm"
        :spanNumber="8"
        :formRow="SearchformRow"
        :ruleForm="searchForm"
        :isFunBtn="false"
        @onSelect="searchSelectChangeHandle"
        :newFlag="true"
        :formRule="false"
      ></BaseNewForm>
      <div class="btn">
        <el-button type="primary" @click="searchHandle">搜索</el-button>
        <el-button @click="resizeSearchHandle">重置</el-button>
      </div>
    </div>
    <div class="maintable">
      <div class="left">
        <BaseTable
          ref="mainTable"
          :tableData="tableData"
          :tableColumn="tableColumn"
          :tabLoading="mainTabLoading"
          :tableHeight="'60vh'"
          :funData="mainFunData"
          @onFunc="tableButtonHandle"
          :border="false"
        >
          <template v-slot:receipt_status="{ row: scope }">
            <div>{{ status[scope.row["receipt_status"]] }}</div>
          </template>
          <template v-slot:vou="{ row }">
            <div>{{ row.row.vou ? vou_dist[row.row.vou * 1] : "" }}</div>
          </template>
          <template v-slot:certificate_status="{ row }">
            <div>
              {{
                row.row.certificate_status
                  ? certificate_status_dist[row.row.certificate_status * 1]
                  : ""
              }}
            </div>
          </template>
        </BaseTable>
        <div class="footer">
          <basePage
            :pageModel="pageModel"
            @update:pageModel="currentChangeHandle"
          ></basePage>
        </div>
      </div>
    </div>
    <baseDialog
      class="dialog"
      :loading="dialogCommitLoading"
      v-model="dialogSwitch"
      :fullscreen="true"
      @close="dialogCloseHandle"
      @confirm="dialogconfirmHandle"
      :footerShow="true"
      :lookFlag="lookFlag"
      :title="dontmake ? '不制单备注' : '凭证制单'"
    >
      <template v-if="dontmake">
        <div>
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="dontmakeRemark"
          >
          </el-input>
        </div>
      </template>
      <template v-else>
        <div class="receipts">
          <div class="title">{{ receiptsTitle }}</div>
          <div class="date">
            <span class="demonstration">制单时间:</span>
            <div class="datepick">
              <el-date-picker
                :disabled="true"
                v-model="receiptsDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                size="mini"
              >
              </el-date-picker>
            </div>
          </div>
          <div class="main">
            <div class="top">
              <div class="left">
                <div class="select">
                  <el-select
                    v-model="receiptsSelect"
                    placeholder=""
                    :disabled="true"
                  >
                    <el-option
                      v-for="item in receiptsOptions"
                      :key="item.id"
                      :label="item.label"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </div>
                <div class="input">
                  <el-input
                    v-model="receiptsnumber"
                    :disabled="true"
                    placeholder=""
                  ></el-input>
                </div>
              </div>
              <div class="right">
                <el-button type="primary" @click="addTableRow" :disabled="true"
                  >新增行</el-button
                >
                <el-button :disabled="true">删除行</el-button>
              </div>
            </div>
            <div class="table">
              <BaseTable
                ref="receiptsTable"
                :showIndex="true"
                :highlightCurrent="true"
                @onRowClick="tableClick"
                :tableData="receiptsTableData"
                :tableColumn="receiptsTableColumn"
                :tabLoading="receiptsabLoading"
                @sumsChange="sumsChangeHandle"
                :showsummary="true"
                :tableHeight="400"
                :receipSum="receipSum"
                :border="true"
                :SummariesIndex="[3, 4]"
              >
                <!-- 摘要 -->
                <template v-slot:abstract_msg="{ row: scope }">
                  <div v-if="!scope.row[scope.column.property + 'show']">
                    {{ scope.row[scope.column.property] }}
                  </div>
                  <div v-else>
                    <el-input
                      v-model="scope.row[scope.column.property]"
                      :ref="scope.column.property + scope.$index"
                      @blur="scope.row[scope.column.property + 'show'] = false"
                    ></el-input>
                  </div>
                </template>
                <!-- 科目 -->
                <template v-slot:subject="{ row: scope }">
                  <!-- <div v-if="!scope.row[scope.column.property + 'show']">
                    {{ scope.row[scope.column.property] }}
                  </div>
                  <div v-else> -->
                  <baseNewSelect
                    :lookflag="!scope.row[scope.column.property + 'show']"
                    :ref="scope.column.property + scope.$index"
                    v-model="scope.row[scope.column.property]"
                    :itemObj="scope.row"
                    @showValue="showValueHandle"
                    @blur="scope.row[scope.column.property + 'show'] = false"
                    :idKey="scope.column.property"
                  ></baseNewSelect>
                  <!-- </div> -->
                </template>
                <!-- 借方金额 -->
                <template v-slot:debit_amount="{ row: scope }">
                  <div v-if="!scope.row[scope.column.property + 'show']">
                    {{ scope.row[scope.column.property] }}
                  </div>
                  <div v-else>
                    <el-input-number
                      v-model="scope.row[scope.column.property]"
                      :ref="scope.column.property + scope.$index"
                      controls-position="right"
                      @blur="
                        jineoneBlur(
                          scope.row,
                          scope.row[scope.column.property],
                          scope.row[scope.column.property + 'show']
                        )
                      "
                      @keyup.enter.native="
                        jineoneBlur(
                          scope.row,
                          scope.row[scope.column.property],
                          scope.row[scope.column.property + 'show']
                        )
                      "
                      :min="1"
                      :max="99999"
                    ></el-input-number>
                  </div>
                </template>
                <!-- 贷方金额 -->
                <template v-slot:credit_amount="{ row: scope }">
                  <div v-if="!scope.row[scope.column.property + 'show']">
                    {{ scope.row[scope.column.property] }}
                  </div>
                  <div v-else>
                    <el-input-number
                      v-model="scope.row[scope.column.property]"
                      :ref="scope.column.property + scope.$index"
                      controls-position="right"
                      @blur="
                        credit_amountBlur(
                          scope.row,
                          scope.row[scope.column.property],
                          scope.row[scope.column.property + 'show']
                        )
                      "
                      @keyup.enter.native="
                        credit_amountBlur(
                          scope.row,
                          scope.row[scope.column.property],
                          scope.row[scope.column.property + 'show']
                        )
                      "
                      :min="1"
                      :max="99999"
                    ></el-input-number>
                  </div>
                </template>
              </BaseTable>
            </div>
            <div class="footer">
              <div class="left">
                <div class="top">
                  <div class="left">
                    分 录：{{
                      receiptsclickRow.index + 1
                        ? receiptsclickRow.index + 1
                        : ""
                    }}
                  </div>
                  <div class="right">
                    <el-radio
                      :disabled="true"
                      v-model="receiptsclickRow.direction"
                      label="1"
                      >借</el-radio
                    >
                    <el-radio
                      :disabled="true"
                      v-model="receiptsclickRow.direction"
                      label="2"
                      >贷</el-radio
                    >
                  </div>
                </div>
                <div class="bottom">
                  科 目：{{ receiptsclickRow.subjectShowValue }}
                </div>
              </div>
              <div class="right">
                <div v-if="receiptsclickRow.project_flag" class="chunk">
                  <div class="smalltitle">项目：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.project"
                      :lookflag="true"
                      :journalingType="'project'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
                <div v-if="receiptsclickRow.suppliter_flag" class="chunk">
                  <div class="smalltitle">供应商：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.suppliter"
                      :lookflag="true"
                      :journalingType="'supplier'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
                <div v-if="receiptsclickRow.client_flag" class="chunk">
                  <div class="smalltitle">客户：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.client"
                      :lookflag="true"
                      :journalingType="'client'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
                <div v-if="receiptsclickRow.salesman_flag" class="chunk">
                  <div class="smalltitle">业务员：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.salesman"
                      :lookflag="true"
                      :journalingType="'salesman'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
                <div v-if="receiptsclickRow.section_flag" class="chunk">
                  <div class="smalltitle">部门：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.section"
                      :lookflag="true"
                      :journalingType="'section'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <footer>
            <div class="top">
              <div class="chunk">
                <div class="smtitle">账套名称：</div>
                <div class="value">{{ attribute.sob }}</div>
              </div>
            </div>
            <div class="bottom">
              <div class="chunk">
                <div class="smtitle">账套年：</div>
                <div class="value">{{ attribute.year }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">制单：</div>
                <div class="value">{{ attribute.documenterman }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">审核：</div>
                <div class="value">{{ attribute.auditorman }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">审核日期：</div>
                <div class="value">{{ attribute.auditor_date }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">记帐：</div>
                <div class="value">{{ attribute.bookkeeperman }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">出纳：</div>
                <div class="value">{{ attribute.tellerman }}</div>
              </div>
            </div>
          </footer>
        </div>
      </template>
    </baseDialog>
  </div>
</template>

<script>
import BaseNewForm from "./compoments/baseNewForm";
import BaseTable from "./compoments/baseTable";
import basePage from "./compoments/basePage.vue";
import baseDialog from "./compoments/baseDialog.vue";
import baseNewSelect from "./compoments/baseNewSelect.vue";
import ToString from "./utils/numChange";
import { deepClone } from "@/utils/index.js";
import BaseRightLeftSelect from "./compoments/baseLeftRightSelect.vue";
import { getUserModuleApi } from "@/api/integrationOption/integrationOption.js";
export default {
  data() {
    return {
      attribute: {},
      vou_dist: {
        1: "转",
        2: "银行付",
        3: "银收",
        4: "现收",
        5: "现付",
        6: "调整",
      },
      certificate_status_dist: {
        1: "未推送",
        2: "已推送",
        3: "出纳审核",
        4: "推送失败",
        5: "已经推送撤销",
        6: "已删除",
      },
      status2: {
        1: "已制单带推送",
        2: "凭证审核",
        3: "出纳审核",
        4: "推送失败",
      },
      status: {
        1: "未制单",
        2: "已制单",
        3: "不制单",
      },
      lookFlag: true,
      mainFunData: [
        {
          type: "view",
          text: "查看",
          color: "#5a9cf8",
        },
      ],
      // 凭证相关数据
      assistformRow: [],
      assistLoading: false,
      assistdialogSwitch: false,
      receipSum: 0,
      dictionaries: {},
      receiptsabLoading: false,
      receiptsclickRow: {},
      receiptsclickRowClone: {},
      receiptsnumber: "11",
      receiptsTableData: [],
      receiptsTableColumn: [
        {
          id: "abstract_msg",
          title: "摘要",
          width: 300,
        },
        {
          id: "subject",
          title: "科目",
          total: 20,
          width: 600,
        },
        {
          id: "debit_amount",
          title: "借方金额",
        },
        {
          id: "credit_amount",
          title: "贷方金额",
        },
      ],
      receiptsOptions: [
        {
          id: 1,
          label: "转",
          title: "转账凭证",
        },
        {
          id: 2,
          label: "银付",
          title: "银行付款凭证",
        },
        {
          id: 3,
          label: "银收",
          title: "银行收款证",
        },
        {
          id: 4,
          label: "现收",
          title: "现金收款凭证",
        },
        {
          id: 5,
          label: "现付",
          title: "现金付款凭证",
        },
        {
          id: 6,
          label: "调整",
          title: "调整器凭证",
        },
      ],
      receiptsSelect: 2,
      receiptsDate: "",
      //   不制单相关
      dontmakeRemark: "",
      dontmake: false,
      dialogSwitch: false,
      dialogCommitLoading: false,
      pageModel: {
        pageIndex: 1,
        total: 10,
        limit: 10,
      },
      mainTabLoading: false,
      searchForm: {},
      SearchformRow: [
        {
          elCol: [
            {
              type: "input",
              title: "标题",
              id: "receipt_title",
              row: 8,
            },
            {
              type: "newSelect",
              title: "单据类型",
              id: "receipt_type",
              journalingType: "receiptConf",
              row: 8,
            },
          ],
        },
      ],
      tableData: [],
      tableColumn: [
        {
          id: "ap",
          title: "期间",
          width: 80,
        },
        {
          id: "vou",
          title: "凭证字",
          width: 80,
        },
        {
          id: "voucher_number",
          title: "凭证号",
          width: 80,
        },
        {
          id: "receipt_title",
          title: "标题",
          width: 400,
        },
        {
          id: "5",
          title: "协同发起人",
          width: 100,
        },
        {
          id: "6",
          title: "协同发起时间",
        },
        {
          type: "newSelect",
          journalingType: "salesman",
          id: "auditorman",
          title: "协同审核人",
          width: 100,
        },
        {
          id: "certificate_status",
          title: "凭证状态",
          width: 80,
        },
      ],
      asideformRow: [
        {
          elCol: [
            {
              type: "select",
              title: "分录合并",
              id: "1",
              row: 24,
              options: [
                { id: 1, label: "不合并" },
                { id: 2, label: "相同科目+辅助核算" },
                { id: 3, label: "相同科目+辅助核算+摘要" },
              ],
            },
          ],
        },
        {
          elCol: [
            {
              type: "select",
              title: "分录顺序",
              id: "test",
              row: 24,
              disabled: false,
              options: [
                { id: 1, label: "固定先借后贷" },
                { id: 2, label: "按表单先借后贷" },
                { id: 3, label: "按表单填制顺序" },
              ],
            },
          ],
        },
        {
          elCol: [
            {
              type: "onecheck",
              title: "多张表单生成同一张",
              id: "2",
              row: 24,
            },
          ],
        },
      ],
      aisdeForm: {},
      checkTable: [],
    };
  },
  components: {
    BaseNewForm,
    BaseTable,
    basePage,
    baseDialog,
    baseNewSelect,
    BaseRightLeftSelect,
  },
  methods: {
    // 查询已制单
    async getMainTableData(obj = {}) {
      const res = await getUserModuleApi(
        {
          tl: "sys_proofService",
          as: "sys_proofService",
          dj: "queryProofPage",
        },
        {
          pageNum: this.pageModel.pageIndex,
          pageSize: this.pageModel.limit,
          ...obj,
        }
      );
      if (res.status == 200) {
        this.tableData = res.attribute.list;
      }
    },
    // 凭证制单请求
    async finishHandle(id) {
      const res = await getUserModuleApi(
        {
          tl: "sys_proofService",
          as: "sys_proofService",
          dj: "getProof",
        },
        { id: id }
      );
      this.maleReceiptsLoding = false;
      if (res.status == 200) {
        this.dialogSwitch = true;
        this.attribute = res.attribute;
        this.searchSob(res.attribute.sob_no, "sob").then((val) => {
          this.attribute.sob = val;
        });
        if (res.attribute.documenter) {
          this.searchSob(res.attribute.documenter, "salesman").then((val) => {
            this.attribute.documenterman = val;
          });
        }
        if (res.attribute.auditor) {
          this.searchSob(res.attribute.auditor, "salesman").then((val) => {
            this.attribute.auditorman = val;
          });
        }

        res.attribute.bookkeeper &&
          this.searchSob(res.attribute.bookkeeper, "salesman").then((val) => {
            this.attribute.bookkeeperman = val;
          });
        res.attribute.teller &&
          this.searchSob(res.attribute.teller, "salesman").then((val) => {
            this.attribute.tellerman = val;
          });
        this.dontmake = false;
        this.dialogSwitch = true;
        this.receiptsSelect = res.attribute.vou * 1;
        this.receiptsnumber = res.attribute.voucher_number;
        this.receiptsTableData = [];
        this.receiptsDate = res.attribute.certificate_date;
        let obj = {
          abstract_msgshow: false,
          subjectshow: false,
          debit_amountshow: false,
          credit_amountshow: false,
        };
        res.attribute.proofDetails.forEach((item) => {
          this.receiptsTableData.push({ ...item, ...obj });
        });
      }
    },
    // 寻找各种数据/账套/业务员
    async searchSob(val, type) {
      const res = await getUserModuleApi(
        {
          tl: "sys_proofService",
          as: "sys_proofService",
          dj: "getJournaling",
        },
        {
          journalingType: type,
          codes: val,
        }
      );
      return res.attribute.names;
    },
    tableButtonHandle(val, item) {
      // getMainTableData()
      this.finishHandle(val.id);
    },
    assistdialogconfirmHandle() {
      this.$refs.assistForm.submitForm();
    },
    dialogCloseHandle() {
      if (this.receiptsclickRow) {
        this.receiptsclickRow = "";
        this.receiptsclickRowClone = "";
      }
    },
    dialogconfirmHandle() {},

    showValueHandle(val, row, idKey) {
      this.$set(row, idKey + "ShowValue", val);
    },
    sumsChangeHandle(arr) {
      if (arr[3] == arr[4] && arr[3]) {
        this.receipSum = ToString(arr[3]);
      } else {
        this.receipSum = "";
      }
    },
    addTableRow() {
      let obj = {
        abstract_msgshow: false,
        subjectshow: false,
        debit_amountshow: false,
        credit_amountshow: false,
      };
      this.receiptsTableData.push({ ...obj });
    },
    receipPageChange() {},
    receipDoubleClick(row, column, cell, event) {
      row[column.property + "show"] = true;
      this.$nextTick(() => {
        this.$refs[column.property + row.index] &&
          this.$refs[column.property + row.index].focus();
      });
    },
    look(val) {
    },
    tableClick(row) {
      if (row != this.receiptsclickRow) {
        this.receiptsclickRow = {};
      }
      this.$nextTick(() => {
        this.receiptsclickRow = row;
        this.receiptsclickRowClone = deepClone(row);
      });
    },
    dialogconfirmHandle() {},

    currentChangeHandle(pageModel) {
      this.pageModel = pageModel;
      this.$nextTick(() => {
        this.getMainTableData(this.searchForm);
      });
    },
    onSubmitHandle() {},
    searchSelectChangeHandle(val, index, indexRow, row) {},
    searchHandle() {
      this.getMainTableData(this.searchForm);
    },
    resizeSearchHandle() {
      this.searchForm = {};
    },
  },
  computed: {
    receiptsTitle() {
      return this.receiptsOptions.find((item) => item.id == this.receiptsSelect)
        .title;
    },
    subjectComputed() {},
  },
  created() {
    this.getMainTableData();
    // this.receipOptions.forEach((item) => {
    //   this.$set(this.dictionaries, item.id, item.label);
    // });
  },
};
</script>

<style scoped lang="scss">
.await {
  width: 100%;
  .search {
    padding: 20px;
    background-color: #fff;
    border-radius: 20px;
    .btn {
      display: flex;
      justify-content: flex-end;
    }
  }
  .maintable {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    width: 100%;
    .left {
      background-color: #fff;
      padding: 20px;
      border-radius: 20px;
      width: 100%;
      > .footer {
        margin-top: 20px;
      }
    }
    .right {
      margin-left: 20px;
      background-color: #fff;
      border-radius: 20px;
      width: 25%;
      position: relative;
      > .wrap {
        padding: 20px;
        .title {
          font-size: 20px;
          margin-bottom: 20px;
        }
        > .footer {
          position: absolute;
          bottom: 20px;
          right: 10px;
        }
      }
    }
  }
}
.dialog {
  .receipts {
    text-align: center;
    position: relative;
    .img {
      position: absolute;
      top: 5%;
      left: 60%;
      z-index: 2;
    }
    .title {
      font-size: 20px;
      font-weight: 600;
    }
    .date {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      .demonstration {
        margin-right: 30px;
      }
      .datepick {
        width: 200px;
      }
    }
    .main {
      margin-top: 10px;
      border: 1px solid #000;
      border-radius: 10px;
      > .top {
        padding: 10px 10px 0;
        display: flex;
        justify-content: space-between;
        .left {
          display: flex;
          text-align: left;
          .select {
            width: 130px;
          }
          .input {
            margin-left: 10px;
            width: 100px;
          }
        }
        .right {
        }
      }
      > .table {
        padding: 0 10px 0;
        margin-top: 10px;
      }
      > .footer {
        border-top: 1px solid #000;
        margin-top: 10px;
        padding: 10px;
        display: flex;
        align-items: center;
        font-weight: 700;
        > .left {
          display: flex;
          flex-direction: column;
          align-items: flex-start;
          > .top {
            display: flex;
            justify-content: flex-start;
            align-items: center;
            .left {
              margin-right: 20px;
            }
          }
          > .bottom {
            margin-top: 10px;
            display: flex;
            justify-content: flex-start;
          }
        }
        > .right {
          padding: 20px;
          text-align: left;
          border: 1px solid #ccc;
          width: 80%;
          margin-left: 20px;
          height: 80px;
          display: flex;
          .chunk {
            width: 20%;
            display: flex;
            align-items: center;
            > .value {
              width: 60%;
              text-align: left;
              display: -webkit-box; /*作为弹性伸缩盒子模型显示。*/
              -webkit-box-orient: vertical; /*作为弹性伸缩盒子模型显示。*/
              -webkit-line-clamp: 1; //*显示的行*/
              overflow: hidden; /*溢出隐藏*/
              /* 对于连字情况或者纯字母，可以解决 */
              word-break: break-all;
            }
          }
        }
      }
    }
    footer {
      padding: 20px;
      margin-top: 20px;
      background-color: #e1e3e8;
      height: 80px;
      border-radius: 10px;
      font-weight: 800;
      > .top {
        text-align-last: left;
        > .chunk {
          display: flex;
          font-weight: 800;
        }
      }
      > .bottom {
        margin-top: 10px;
        text-align-last: left;
        display: flex;
        justify-content: space-between;
        .chunk {
          margin-right: 20px;
          display: flex;
          font-weight: 800;
        }
      }
    }
  }
}
</style>
