<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <div class="manage-input">
        <el-form ref="queryForm" :model="queryForm" label-width="80px">
          <vab-query-form>
            <el-form-item width="100" prop="name" label="单据编号">
              <el-input
                v-model="queryForm.name"
                size="small"
                placeholder="请输入单据编号"
              ></el-input>
            </el-form-item>
          </vab-query-form>
        </el-form>
      </div>
      <div class="manage-button">
        <el-button size="small" @click="resetForm()">重置</el-button>
        <el-button type="primary" size="small" @click="search">查询</el-button>
        <el-button type="primary" size="small" @click="deletes">
          批量删除
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="groupData"
      border
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        prop="fid"
        label="ID"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="billNo"
        label="单据编号"
        width="160"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="customerName"
        label="客户名称"
        width="240"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="materialCode"
        label="物料编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="materialName"
        label="物料名称"
        align="center"
      ></el-table-column>
      <el-table-column prop="realQty" label="数量/单位" align="center">
        <template #default="{ row }">
          {{ row.realQty }} /{{ row.unitName }}
        </template>
      </el-table-column>
      <el-table-column
        prop="stockName"
        label="仓库"
        align="center"
      ></el-table-column>
      <el-table-column prop="" label="时间" align="center">
        <template #default="{ row }">
          {{ getFormatData(row.date) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="documentStatus"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">出库详情</el-button>
          <el-button type="text" @click="qrProject(row)">二维码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      layout="prev, pager, next, jumper"
      :page-size="queryForm.size"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>

    <el-dialog
      v-model="dialogFormVisible"
      :title="title"
      width="50%"
      @close="close"
    >
      <el-tabs>
        <el-tab-pane label="基本信息" v-if="dialogFormVisible">
          <info :data="data.basicInfo" keyName="basicInfo"></info>
        </el-tab-pane>
        <el-tab-pane label="客户信息" name="second">
          <info :data="data.customerInfo" keyName="customerInfo"></info>
        </el-tab-pane>
        <el-tab-pane label="财务信息" name="third">
          <info :data="data.financialInfo" keyName="financialInfo"></info>
        </el-tab-pane>
        <el-tab-pane label="物流信息" name="fourth">暂无物流信息</el-tab-pane>
      </el-tabs>
      <div class="dialog-footer"></div>
    </el-dialog>
    <el-dialog
      v-model="dialogqrcodeVisible"
      title="二维码信息"
      width="50%"
      @close="close"
    >
      <qcode
        v-if="dialogqrcodeVisible"
        :url="codeUrl"
        :nameplate="nameplate"
        :id="codeId"
      ></qcode>

      <el-descriptions :column="3" border>
        <el-descriptions-item
          :label="item.attributeName"
          v-for="(item, index) in qrinfoData"
        >
          {{ item.attributeValue }}
        </el-descriptions-item>
      </el-descriptions>

      <div class="dialog-footer"></div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, details, productAttribute, getNameplate } from '@/api/ywry';
import qcode from './qcode';
import { baseURL } from '@/config';
import info from './info';
import { getDate } from '@/common/times';
import QRCode from 'qrcode';
export default {
  name: 'Index',
  components: {
    info,
    qcode,
  },
  data() {
    return {
      groupData: [],
      lazy: true,

      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      codeId: '',
      background: true,
      listLoading: true,
      dialogFormVisible: false,
      dialogqrcodeVisible: false,
      title: '详情',
      elementLoadingText: '正在加载...',
      ids: [],
      queryForm: {
        page: 1,
        size: 10,
        name: '',
      },
      codeUrl: '',
      data: {},
      qrinfoData: {},
      nameplate: '',
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;

      const { data } = await getList(this.queryForm);
      this.groupData = data;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    close() {},
    getFormatData(time) {
      return getDate(time);
    },
    search() {
      this.fetchData();
    },
    qrProject(row) {
      let code = row.materialCode;

      let producInfo = new Promise((resolve, reject) => {
        productAttribute(code).then(res => {
          resolve(res.data);
        });
      });
      let getPlate = new Promise((resolve, reject) => {
        getNameplate(code).then(res => {
          resolve(res.data ? baseURL + '/static/img/' + res.data : null);
        });
      });

      Promise.all([producInfo, getPlate]).then(proData => {
        if (proData[1]) {
          this.codeId = row.billNo;
          this.codeUrl =
            'http://sm.lidinghb.com?materialCode=' +
            code +
            '&billNo=' +
            row.billNo;
          this.qrinfoData = proData[0];
          this.nameplate = proData[1];
          this.dialogqrcodeVisible = true;
        } else {
          this.$message({
            type: 'warning',
            message: '当前物料产品未配置',
          });
        }
      });
    },
    handleAdd() {
      this.$refs['edit'].showEdit();
    },
    handleEdit(row) {
      details(row.fid).then(res => {
        this.data = res.data;
        this.dialogFormVisible = true;
      });
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      if (val) {
        this.queryForm.page = val;
        this.fetchData();
      }
    },
    deletes() {
      const ids = this.ids;
      const that = this;
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = remove(ids);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
    },
    handleDelete(row) {
      var that = this;
      if (row.id) {
        this.$confirm('你确定要删除当前项吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const { msg } = doDelete([row.id]);
            this.$message({
              type: 'success',
              message: msg == undefined ? '删除成功' : msg,
            });
            setTimeout(function () {
              that.fetchData();
            }, 1000);
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            });
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-wrap {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  .el-input {
    width: 200px !important;
  }
  .el-select {
    width: 200px !important;
  }
  .manage-button {
    padding-left: 12px;
    margin-bottom: 30px;
  }
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
.el-dialog {
  .el-dialog__header {
    background-color: #1890ff !important;
    padding: 15px 20px;
    text-align: left !important;
    .el-dialog__title {
      color: #e8f4ff !important;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #e8f4ff !important;
    }
  }
  .el-dialog__body {
    padding: 20px !important;
    .el-form-item {
      margin-bottom: 10px !important;
      .el-input {
        width: 100% !important;
      }
      .el-select {
        width: 100% !important;
      }
    }
    .el-form-item:last-child {
      margin-bottom: 20px !important;
    }
  }
}
</style>
