<template>
  <div>
    <el-form :inline="true" :model="query" class="query-form" size="mini">
      <el-form-item class="query-form-item">
        <el-input
          v-model="filterQuery.coName"
          placeholder="产品通用名"
        ></el-input>
      </el-form-item>
      <el-form-item class="query-form-item">
        <el-input v-model="filterQuery.unit" placeholder="生产企业"></el-input>
      </el-form-item>
      <el-form-item class="query-form-item">
        <el-input
          v-model="filterQuery.nameCode"
          placeholder="产品编码"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button-group>
          <el-button
            type="primary"
            icon="el-icon-refresh"
            @click="onReset"
          ></el-button>
          <el-button type="primary" icon="search" @click="getList"
            >查询</el-button
          >
        </el-button-group>
      </el-form-item>
    </el-form>
    <el-form :inline="true" :model="query" style="display: flex" size="mini">
      <el-form-item class="query-form-item">
        <el-input
          v-model="editContrast.drugCode"
          placeholder="药品追溯码"
        ></el-input>
      </el-form-item>

      <el-form-item class="query-form-item">
        <el-input
          v-model="editContrast.saleCode"
          placeholder="商品条码"
        ></el-input>
      </el-form-item>
      <el-form-item class="query-form-item" prop="name">
        <el-checkbox v-model="checked" @change="handleCheckedChange"
          >以使用单元数量入库</el-checkbox
        >
      </el-form-item>
      <el-form-item>
        <el-button-group>
          <el-button type="primary" icon="search" @click="onAddSubmit"
            >新增对照</el-button
          >
        </el-button-group>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column label="产品通用名" prop="coName" fixed></el-table-column>
      <el-table-column label="产品编码" prop="drugCode" fixed></el-table-column>
      <el-table-column label="商品条码" prop="saleCode" fixed></el-table-column>
      <el-table-column label="是否零头销售" prop="isOldSale" fixed>
        <template slot-scope="scope">
          <span>{{ isTip[scope.row.isOldSale] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产企业" prop="unit" fixed></el-table-column>
      <el-table-column label="批准文号" prop="authCode" fixed></el-table-column>
      <el-table-column
        label="包装比例"
        prop="packRatio"
        fixed
      ></el-table-column>
      <el-table-column
        label="包装级别"
        prop="packLevel"
        fixed
      ></el-table-column>

      <el-table-column label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click.native.stop="deleteDialog(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-size="filterQuery.limit"
      @current-change="handleCurrentChange"
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import {
  codeContrast,
  addCodeContrast,
  deleteCodeContrast,
} from "../../api/sale/codeContrast";
import store from "../../store/index";
export default {
  data() {
    return {
      filterQuery: {
        unit: "",
        coName: "",
        nameCode: "",
        page: 1,
        limit: 20,
        customerId: store.getters.customerId,
      },

      postQuery: {
        codeContrastData: [],
      },

      isTip: {
        0: "否",
        1: "是",
      },

      editContrast: {
        drugCode: "",
        saleCode: "",
        isOldSale: "",
      },

      selectDrugDialogVisible: false,
      list: [],
      total: 0,
      multipleSelection: [],
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        unit: "",
        coName: "",
        nameCode: "",
        page: 1,
        limit: 20,
        customerId: store.getters.customerId,
      };
      this.getList();
    },

    getList() {
      this.loading = true;
      codeContrast(this.filterQuery)
        .then((response) => {
          this.loading = false;
          this.list = response.data.list || [];
          this.total = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },

    handleCheckedChange(value) {
      if (value) {
        this.editContrast.isOldSale = 1;
      } else {
        this.editContrast.isOldSale = 0;
      }
    },

    deleteOrders(data) {
      this.loading = true;
      let tquery = {
        codeContrastId: data.codeContrastId + "",
      };

      deleteCodeContrast(tquery)
        .then((response) => {
          if (response.code == 20000) {
            this.$message.success("删除成功！");
            this.getList();
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {});
    },

    onAddSubmit() {
      this.postQuery.codeContrastData.push(this.editContrast);
      addCodeContrast(this.postQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.loading = false;
            this.getList();
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    deleteDialog(rowId) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteOrders(rowId);
        })
        .catch(() => {});
    },

    closeMain(val) {
      this.selectDrugDialogVisible = false;
      if (val) {
        this.$message.success("添加成功！");
        this.getList();
      } else {
        this.$message.error("添加失败！");
      }
    },
    exportTxt() {},

    intentDetail() {
      // this.$router.push({ path: "../product/drugrevance" });
      this.selectDrugDialogVisible = true;
      //  this.$router.push({name:'home',query: {id:'1'}})
    },

    handleSelectionChange(val) {
      console.log("----" + val);
      this.multipleSelection = val;
    },

    handleCurrentChange(val) {
      this.filterQuery.page = val;
      this.getList();
    },
  },
  mounted() {},

  created() {
    this.getList();
  },
};
</script>