<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" class="query-form" label-width="100px" >
        <el-row>
          <el-col :span="11">
            <el-form-item label="采购单号">
              <el-input v-model="filterQuery.billNo" style="width: 90%" placeholder="请输入单据号" clearable @change="getList"></el-input>
            </el-form-item>
          </el-col>


          <el-col :span="11">
            <el-form-item label="采购仓库">
              <el-select v-model="filterQuery.invCode" style="width: 90%" clearable placeholder="请选择所属仓库" @change="getList">
                <el-option
                  v-for="item in invList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="单据状态：" prop="emergency">
              <el-select v-model="filterQuery.status" placeholder="请选单据状态" style="width: 90%" @change="getList"
                         clearable>
                <el-option label="草稿" :value='1'></el-option>
                <el-option label="审核中" :value='2'></el-option>
                <el-option label="已审核" :value='3'></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item label="是否已导入:">
              <el-select v-model="filterQuery.importStatus" placeholder="请选导入状态" style="width: 90%" @change="getList"
                         clearable>
                <el-option label="未导入" :value='0'></el-option>
                <el-option label="已导入" :value='1'></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

    <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row
              @current-change="handleDetail" :default-sort="defaultSort">
      <el-table-column label="序号" type="index" width="60"></el-table-column>
      <el-table-column label="采购单号" prop="billNo"></el-table-column>
      <el-table-column label="采购仓库" prop="invCode" :formatter="invCodeFormat">
      </el-table-column>
      <el-table-column label="到货截止" prop="arrivalTime"></el-table-column>
      <!--        <el-table-column label="采购总金额" prop="amount"></el-table-column>-->
      <el-table-column label="单据状态" prop="flowStatus">
        <template slot-scope="scope">
          <el-tag :type="(scope.row.flowStatus==null? scope.row.status : scope.row.flowStatus) | statusFilterType">
            {{ statusMap[(scope.row.flowStatus==null? scope.row.status : scope.row.flowStatus)] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否已导入" prop="importStatus">
        <template slot-scope="scope">
          <el-tag :type="(scope.row.importStatus) | statusFilterType" >
            {{scope.row.importStatus == 1 ? "已导入":"未导入"  }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="单据日期" prop="billDate"
                       sortable="custom" :sort-orders="['ascending', 'descending']"></el-table-column>
      <el-table-column label="备注" prop="remark"></el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click.native="importPlan(scope.$index, scope.row)">导入
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="filterQuery.total>0"
      :total="filterQuery.total"
      :page.sync="filterQuery.page"
      :limit.sync="filterQuery.limit"
      @pagination="productListChange"/>
    </el-card>
    <br>

    <el-card class="el-card">
      <el-table v-loading="loading" :data="detailList" style="width: 100%" border highlight-current-row>
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="DI/物资编码" prop="nameCode"></el-table-column>
        <el-table-column label="物资名称" prop="productName" show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="计量单位" prop="measname" show-overflow-tooltip="true" width="100"></el-table-column>
        <el-table-column label="价格" prop="price" show-overflow-tooltip="true" width="100"></el-table-column>
        <el-table-column label="申购数量" prop="count"></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh"></el-table-column>
        <el-table-column label="生产厂家" prop="productCompany"></el-table-column>
        <el-table-column label="供应商" prop="supName"></el-table-column>
        <!--        <el-table-column label="备注" prop="remark"></el-table-column>-->
      </el-table>

      <pagination
        v-show="detailquery.total>0"
        :total="detailquery.total"
        :page.sync="detailquery.page"
        :limit.sync="detailquery.limit"
        @pagination="handleCurrentChangeDetail"
      ></pagination>
    </el-card>
  </div>
</template>

<script>
import { listPlan, listApplyDetail} from "@/api/purchase/purPlan";
import {importPlan} from "@/api/purchase/purContract";

export default {
  name: "purContractSelectPlan",
  props: {
    pageType: {
      type: Object,
      required: true,
    },
    closeDialog: {
      type: Function,
      required: true,
    },
    invList: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      statusMap: {
        1: "草稿",
        2: "审核中",
        3: "通过",
        4: "拒绝"
      },
      list: [],
      detailquery:
        {
          orderIdFk: null,
          total: 0,
          page: 1,
          limit: 10,
        },
      detailList: [],
      row: {},
      filterQuery:{
        billAction: null,
        billNo: "",
        originType: null,
        thirdSysFk: "",
        corpName: null,
        type: 1,
        status: 3,
        editStatus: 1,
        emergency: '',
        deptCode: '',
        invCode: null,
        importStatus: 0,
        total:0,
        page: 1,
        limit: 10,
        pageType: this.pageType,
      }
    }
  },
  methods: {
    handleCurrentChangeDetail(val) {
      this.detailquery.page = val.page;
      this.handleDetail(this.row)
    },
    handleDetail(row) {
      if (row.id == null) {
        this.row = this.list[0]
        this.detailquery.orderIdFk=this.row.id
      }else {
        this.detailquery.orderIdFk=row.id
      }

      this.loading = true;
      listApplyDetail(this.detailquery) //查找该单号下的所有条码
        .then((response) => {
          this.detailList = response.data.list || [];
          this.detailquery.total = response.data.total || 0;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.detailList = [];
          this.total = 0;
        });
    },
    productListChange() {
      this.getList()
    },
    getList() {
      this.loading = true;
      listPlan(this.filterQuery).then((response) => {
        if (response.code == 20000) {
          this.list = response.data.list || [];
          this.filterQuery.total = response.data.total || 0;
          this.detailList = []
        } else {
          this.$message.error(response.message);
        }
        this.loading = false;
      })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    invCodeFormat(row) {
      let obj = ''
      this.invList.forEach((item) => {
        //循环list，如果数据相等return出去就好了
        //不会影响原来的数据，只是展示出来的是name
        if (row.invCode == item.code) {
          obj = item.name
        }
      })
      return obj
    },
    importPlan(index, row) {
      this.loading = true;
      let tQuery = {
        id: row.id,
        pageType: this.pageType
      }
      importPlan(tQuery).then((response) => {
        if (response.code == 20000) {
          this.closeDialog(true);
        } else {
          this.$message.error(response.message);
        }
        this.loading = false;
      })
        .catch(() => {
          this.loading = false;
        });
    },

  },
  filters: {
    statusFilterType(status) {
      const statusMap = {
        1: "info",
        2: "warning",
        3: "success",
        4: "danger"
      };
      return statusMap[status];
    }
    ,
  },
  created() {
    this.getList();
  }
}
</script>

<style scoped>

</style>
