<template>
  <div id="workSpace">
    <el-card style="position: relative">
      <el-form label-width="100px">
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item label="单据标题">
              <el-input
                v-model.trim="queryParams.docSubject"
                :size="size"
                clearable
                style="width: 200px"
              />
            </el-form-item>
          </el-col>

          <el-col :span="1.5">
            <el-form-item label="单据类型">
              <el-select
                multiple
                v-model="queryParams.modelIds"
                :size="size"
                style="width: 200px"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.oa_templete_id"
                  :prop="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div style="position: absolute; top: 18px; right: 10px">
        <el-button type="primary" :size="size" @click="searchList"
          >搜索</el-button
        >
        <el-button :size="size" plain @click="resetList">重置</el-button>
      </div>

      <el-table
        fit
        :data="tableList"
        :cell-style="{ borderColor: '#c0c0c0' }"
        :header-cell-style="{ borderColor: '#c0c0c0' }"
        class="exporttable"
        max-height="410"
        border
        highlight-current-row
        style="font-size: 12px"
        @selection-change="(selection) => (ids = selection)"
        @row-click="rowSelect"
        @row-dblclick="doubleClick"
        ref="tables"
      >
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="50px"
        />
        <el-table-column
          show-overflow-tooltip
          label="标题"
          align="center"
          prop="docSubject"
        />
        <el-table-column
          show-overflow-tooltip
          label="创建人"
          align="center"
          prop="docCreate"
          width="100px"
        />
        <el-table-column fixed="right" label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button type="text" :size="size" @click.stop="check(scope.row)"
              >查看</el-button
            >
            <el-button type="text" :size="size" @click.stop="audit(scope.row)"
              >审批</el-button
            >
            <!-- <el-button type="text" :size="size" @click.stop="reject(scope.row)"
              >驳回</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>

      <pagination
        :total="page.total"
        :page.sync="page.pageNum"
        :limit.sync="page.pageSize"
        @pagination="getList({ ...page, ...queryParams })"
        style="height: 32px; padding: 0 !important; flex: 1; overflow-x: auto"
      />

      <el-card class="box-card" style="margin-top: 10px">
        <div slot="header" class="clearfix" style="line-height: 38px">
          <span>单据概述</span>
        </div>
        <el-table
          v-if="cols.length !== 0"
          v-loading="loading"
          :data="sonTableList"
          fit
          :cell-style="{ borderColor: '#c0c0c0' }"
          :header-cell-style="{ borderColor: '#c0c0c0' }"
          class="exporttable"
          max-height="410"
          border
          highlight-current-row
          style="font-size: 12px"
        >
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="50px"
          />
          <el-table-column
            v-for="(col, index) in cols"
            :prop="col.prop"
            :label="col.label"
            :width="col.width"
            :formatter="col.formatter"
          >
            <template slot-scope="scope">
              <template v-if="col.attr">
                <component
                  v-if="col.attr.is === 'el-dict-tag'"
                  v-bind="col.attr"
                  :size="$attrs.size"
                  :value="scope.row[col.prop]"
                  :options="dict.type[col.attr.dictName]"
                ></component>
              </template>
              <template v-else>
                <component v-if="col.formatter" is="span">{{
                  col.formatter(scope.row)
                }}</component>
                <component v-else is="span">{{
                  scope.row[col.prop]
                }}</component>
              </template>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          :total="sonTotal"
          :page.sync="sonQuery.pageNum"
          :limit.sync="sonQuery.pageSize"
          @pagination="getDetailList(checkRow)"
          style="height: 32px; padding: 0 !important; flex: 1; overflow-x: auto"
        />
      </el-card>
    </el-card>
  </div>
</template>

<script>
import {
  getWorkSpaceList,
  auditWork,
  getWorkDetailList,
} from "@/api/purchase/workSpace.js";
import RULE from "./columns";
import { dicts } from "./dicts";
export default {
  name: "workSpace",
  dicts: [...dicts, "oa_templete_id"],
  components: {
    ElDictTag: () => import("@/components/DictTag/index.vue"),
  },
  data() {
    const page = this.$init.page();
    const columns = RULE;
    return {
      page: page,
      Columns: columns,
      size: "mini",
      queryParams: {
        modelIds: [],
        docSubject: "",
      },
      loading: false,
      tableList: [],
      ids: [],
      sonTableList: [],
      sonQuery: {
        pageNum: 1,
        pageSize: 10,
      },
      sonTotal: 0,
      checkRow: {
        billCode: "",
        type: "",
      },
      cols: [],
      // 记录点击行的下标
      recordIndex: "",
    };
  },
  created() {
    this.resetList();
  },
  methods: {
    async getList(params) {
      try {
        this.$modal.loading("加载中...");
        let { code, rows, total } = await getWorkSpaceList(params);
        if (code === 200) {
          this.tableList = rows;
          this.page.total = total;
        }
      } catch (error) {
      } finally {
        this.$modal.closeLoading();
      }
    },
    searchList() {
      let { page, queryParams } = this;
      this.getList({ ...page, ...queryParams });
    },
    resetList() {
      this.queryParams = {
        modelIds: [],
        docSubject: "",
      };
      this.page = this.$init.page();
      this.searchList();
    },
    doubleClick(row) {
      this.rowSelect(row);
    },
    rowSelect(row, column, event) {
      // this.recordIndex = this.tableList.findIndex((value, index ,arr) => {
      //   return value.billCode == row.billCode
      // })
      // this.check(row)
      // 选择行存储便于子表分页
      this.checkRow = row;
      this.cols = [...this.Columns[row.type]];
      this.getDetailList(this.checkRow);
    },
    audits() {
      if (this.ids.length == 0) {
        this.$modal.notifyWarning("请选中至少一条数据");
      } else {
        this.$modal
          .confirm("确认审批选中单据?")
          .then(() => {
            this.$modal.loading("审批中...");
            auditWork(this.ids).then((res) => {
              if (res.code === 200) {
                this.$modal.closeLoading();
                this.$modal.notifySuccess("审批成功");
                this.searchList();
                this.sonTableList = [];
              }
            });
          })
          .catch(() => {
            this.$modal.closeLoading();
            this.searchList();
            this.sonTableList = [];
          });
      }
    },
    check(row) {
      console.log(row);
      this.$router.push({
        path: row.linkUrl,
        query: { billCode: row.billCode },
      });
    },
    audit(row) {
      this.$modal.loading("审批中...");
      auditWork([row])
        .then((res) => {
          if (res.code === 200) {
            this.$modal.closeLoading();
            this.$modal.notifySuccess("审批成功");
            this.searchList();
            this.sonTableList = [];
          }
        })
        // .then(() => {
        //   this.$refs.tables.setCurrentRow(this.tableList[this.recordIndex])
        //   let param = {
        //     billCode: this.tableList[this.recordIndex].billCode,
        //     type: this.tableList[this.recordIndex].type,
        //     ...this.sonQuery
        //   }
        //   getWorkDetailList(param).then(res => {
        //     if(res.code === 200) {
        //       this.sonTableList = res.rows
        //       this.sonTotal = res.total
        //     }
        //   }).catch(err => {
        //   })
        // })
        .catch(() => {
          this.$modal.closeLoading();
          this.searchList();
          this.sonTableList = [];
        });
    },
    // 驳回
    reject(row) {
      this.$prompt(
        "<div>请填写驳回原因 <span style='color:#ff6262'>注：只能驳回到起草节点</span></div>",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPattern: /\s*\S+?/,
          inputErrorMessage: "驳回原因不能为空",
          dangerouslyUseHTMLString: true,
        }
      )
        .then(async ({ value }) => {
          console.log(value, "value");
          try {
            this.$modal.loading("加载中...");
            // let {code,msg} = await
            // if(code=== 200){
            // }
          } catch (error) {
          } finally {
            this.$modal.closeLoading();
          }
        })
        .catch(() => {
          this.$modal.closeLoading();
        });
    },

    // 获取单据概述列表
    async getDetailList(row) {
      try {
        this.loading = true;
        let param = {
          billCode: row.billCode,
          type: row.type,
          ...this.sonQuery,
        };
        let { code, rows, total } = await getWorkDetailList(param);
        if (code === 200) {
          this.sonTableList = rows;
          this.sonTotal = total;
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
#workSpace {
  padding: 12px;
  box-sizing: border-box;
  overflow-y: scroll;
}
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
.el-pagination {
  margin-top: 10px;
  text-align: right;
}
</style>
<style>
.exporttable {
  border: solid 1px #c0c0c0;
}
.el-table .el-table__header-wrapper th {
  font-size: 14px;
}
</style>
