<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.code" style="width: 90%" placeholder="请输入合同编号" clearable @change="getList"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="合同名称">
            <el-input v-model="filterQuery.name" style="width: 90%" clearable placeholder="请输入合同名称" @change="getList">
            </el-input>
          </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='0'></el-option>
              <el-option label="审核中" :value='1'></el-option>
              <el-option label="已审核" :value='2'></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="是否已导入:">
            <el-select v-model="filterQuery.isGenerateOrder" 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="code"></el-table-column>
      <el-table-column label="合同名称" prop="name"></el-table-column>
      <el-table-column label="往来单位/供应商" prop="fromCorp"></el-table-column>
      <el-table-column label="合同仓库" prop="invCode" :formatter="invCodeFormat"></el-table-column>

      <!--        <el-table-column label="合同状态" prop="status" :formatter="statusFormat"></el-table-column>-->
      <el-table-column label="合同状态" prop="status" >
        <template slot-scope="scope">
          <el-tag :type="(scope.row.status) | statusFilterType">
            {{ statusMap[scope.row.status] }}
          </el-tag>
          <el-tag v-if="scope.row.nextNodeName !=  null && scope.row.nextNodeName != '' && scope.row.status == 1 " class="tag-right-align">
            <b v-if="scope.row.nextApprovalNodeType == 1">待人员</b>
            <b v-if="scope.row.nextApprovalNodeType == 2">待岗位</b>
            【{{ scope.row.nextNodeName }}】审核
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否已导入" prop="isGenerateOrder">
        <template slot-scope="scope">
          <el-tag :type="(scope.row.isGenerateOrder) | statusFilterType" >
            {{scope.row.isGenerateOrder == 1? "已导入":"未导入"  }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="合同类型" prop="type" :formatter="typeFormat"></el-table-column>
      <el-table-column label="付款方式" prop="payType" :formatter="payTypeFormat"></el-table-column>

      <el-table-column label="合同签订日期" prop="contractDate"
                       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="importContract(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"></el-table-column>
        <el-table-column label="生产厂家" prop="productCompany"></el-table-column>
        <el-table-column label="注册证/备案号" prop="zczbhhzbapzbh"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh"></el-table-column>
        <el-table-column label="包装级别" prop="packRatio"></el-table-column>
        <el-table-column label="供应商名称" prop="supName"></el-table-column>
        <el-table-column label="数量" prop="count"></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/purContract";
// import {importContract} from "@/api/warehouse/order";

export default {
  name: "purContractSelectPlan",
  props: {
    pageType: {
      type: Object,
      required: true,
    },
    closeDialog: {
      type: Function,
      required: true,
    },
    invList: {
      type: Array,
      required: true,
    },
    action: {
      type: Array,
      required: true,
    },
    orderType: {
      type: Array,
      required: true,
    },
    invCode: {
      type: Array,
      required: true,
    }
  },
  data() {
    return {
      typeList:[
        {name: '战略' ,code: 1 },
        {name: '长期' ,code: 2 },
        {name: '短期' ,code: 3 }
      ],
      statusList:[
        {name: '草稿' ,code: 1 },
        {name: '未审核' ,code: 2 },
        {name: '已审核' ,code: 3 }
      ],
      payTypeList:[
        {name: '银行' ,code: 1 },
        {name: '微信' ,code: 2 },
        {name: '支付宝' ,code: 3}
      ],
      statusMap: {
        0: "草稿",
        1: "审核中",
        2: "通过",
        3: "拒绝"
      },
      list: [],
      filterQuery:{
        code:null,
        name:null,
        status:2,
        isGenerateOrder:0,
        total:0,
        page: 1,
        limit: 10,
        pageType: this.pageType,
      },
      detailquery:
        {
          orderIdFk: null,
          total: 0,
          page: 1,
          limit: 10,
        },
      detailList: [],
      row: {},
    }
  },
  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;
        });
    },
    typeFormat(row) {
      let obj = ''
      this.typeList.forEach((item) => {
        //循环list，如果数据相等return出去就好了
        //不会影响原来的数据，只是展示出来的是name
        if (row.type == item.code) {
          obj = item.name
        }
      })
      return obj
    },
    payTypeFormat(row) {
      let obj = ''
      this.payTypeList.forEach((item) => {
        //循环list，如果数据相等return出去就好了
        //不会影响原来的数据，只是展示出来的是name
        if (row.payType == item.code) {
          obj = item.name
        }
      })
      return obj
    },
    // handleDetail(row) {
    //   console.log("handleDetail:" + row)
    // },
    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
    },
    importContract(index, row) {
      this.loading = true;
      let tQuery = {
        id: row.id,
        action: this.action,
        invCode: this.invCode,
        orderType: this.orderType
      }
      console.log(tQuery)
      importContract(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 = {
        0: "info",
        1: "warning",
        2: "success",
        3: "danger"
      };
      return statusMap[status];
    }
    ,
  },
  created() {
    this.getList();
  }
}
</script>

<style scoped>

</style>
