<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-row :gutter="10" justify="space-between">
        <el-col :span="5">
          <el-form-item label="所属客户" prop="customerName">
            <el-popover-select-v2 v-model="queryParams.customerName" title="客户" valueKey="name"
              referName="CUSTOMER_PARAM_ZT" :dataMapping="{ customer: 'id', customerName: 'name'}"
              :source.sync="queryParams" placeholder="请输入所属客户" @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="产线" prop="productionLineName">
            <el-popover-tree-select v-model="queryParams.productionLineName" valueKey='name' title="产线"
              referName="PRODUCTLINE" :dataMapping="{ customCode: 'id', custom: 'name'}" :source.sync="queryParams"
              placeholder="请输入产线" @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="供应商" prop="rivalName" label-width="120px">
            <el-popover-select-v2 v-model="queryParams.rivalName" title="供应商" valueKey="name" referName="MKRIVAL_PARAM"
              :dataMapping="{ rival: 'id', rivalName: 'name'}" :source.sync="queryParams" placeholder="请输入供应商"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-row :gutter="10" class="mb8" style="float: right">
          <el-col :span="1.5">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8" style="float: right">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-download" size="mini" @click="handleDownload">模板下载</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-upload ref="upload" action="" :http-request="handleUpload">
          <el-button size="mini" type="primary">导入</el-button>
        </el-upload>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="gatherList" @selection-change="handleSelectionChange" v-horizontal-scroll>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" fixed />
      <el-table-column label="所属客户" align="center" prop="customerName" width="220" />
      <el-table-column label="产线" align="center" prop="productionLineName" width="150" />
      <el-table-column label="供应商" align="center" prop="rivalName" width="220" />
      <el-table-column label="供应商体量（万）" align="center" prop="rivalBulk" width="130" />
      <el-table-column label="品牌" align="center" prop="brand" width="100" />
      <el-table-column label="品牌份额（万）" align="center" prop="brandPortion" width="120" />
      <el-table-column label="合作期限（起）" align="center" prop="cooperationStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cooperationStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合作期限（止）" align="center" prop="cooperationEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cooperationEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="科室关键人" align="center" prop="sectionKeyPsnname" width="90" />
      <el-table-column label="科室关键人联系电话" align="center" prop="sectionKeyPsnphone" width="120" />
      <el-table-column label="院级关键人" align="center" prop="hospitalKeyPsnname" width="90" />
      <el-table-column label="院级关键人联系电话" align="center" prop="hospitalKeyPsnphone" width="120" />
      <el-table-column label="供应商关键人" align="center" prop="rivalKeyPsnname" width="90" />
      <el-table-column label="供应商关键人联系电话" align="center" prop="rivalKeyPsnphone" width="120" />
      <el-table-column label="开发思路" align="center" prop="idea" min-width="220" show-overflow-tooltip />
      <el-table-column label="机会点/困难点所需资源" align="center" prop="chance" min-width="220" show-overflow-tooltip />
      <el-table-column label="备注" align="center" prop="remark" min-width="220" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180px" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleSee(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改竞争产品信息收集对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="pageState == 'see'">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="所属客户" prop="customerName">
              <el-popover-select-v2 v-model="form.customerName" title="客户" valueKey="name" referName="CUSTOMER_PARAM_ZT"
                :dataMapping="{ customer: 'id', customerName: 'name'}" :source.sync="form" placeholder="请输入所属客户" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="产线" prop="productionLineName">
              <el-popover-tree-select v-model="form.productionLineName" valueKey='name' title="产线"
                referName="PRODUCTLINE" :dataMapping="{ customCode: 'id', custom: 'name'}" :source.sync="form"
                placeholder="请输入产线" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="供应商" prop="rivalName" label-width="100px">
              <el-popover-select-v2 v-model="form.rivalName" title="供应商" valueKey="name" referName="MKRIVAL_PARAM"
                :dataMapping="{ rival: 'id', rivalName: 'name'}" :source.sync="form" placeholder="请输入供应商" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="供应商体量（万）" prop="rivalBulk" label-width="130px">
              <el-input-number v-model="form.rivalBulk" :precision="2" :step="1"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="品牌" prop="brand">
              <el-input v-model="form.brand" placeholder="请输入品牌" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="品牌份额（万）" prop="brandPortion" label-width="120px">
              <el-input-number v-model="form.brandPortion" :precision="2" :step="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="合作期限" prop="cooperationStart">
              <el-date-picker clearable v-model="form.cooperationStart" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择合作期限">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="合作期限" prop="cooperationEnd">
              <el-date-picker clearable v-model="form.cooperationEnd" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择合作期限">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="科室关键人" prop="sectionKeyPsnname" label-width="110px">
              <el-input v-model="form.sectionKeyPsnname" placeholder="请输入科室关键人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="科室关键人联系电话" prop="sectionKeyPsnphone" label-width="140px">
              <el-input v-model="form.sectionKeyPsnphone" placeholder="请输入科室关键人联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="院级关键人" prop="hospitalKeyPsnname" label-width="110px">
              <el-input v-model="form.hospitalKeyPsnname" placeholder="请输入院级关键人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="院级关键人联系电话" prop="hospitalKeyPsnphone" label-width="140px">
              <el-input v-model="form.hospitalKeyPsnphone" placeholder="请输入院级关键人联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="供应商关键人" prop="rivalKeyPsnname" label-width="140px">
              <el-input v-model="form.rivalKeyPsnname" placeholder="请输入供应商关键人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="供应商关键人联系电话" prop="rivalKeyPsnphone" label-width="170px">
              <el-input v-model="form.rivalKeyPsnphone" placeholder="请输入供应商关键人联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="开发思路" prop="idea">
          <el-input v-model="form.idea" placeholder="请输入开发思路" type="textarea" />
        </el-form-item>
        <el-form-item label="机会点/困难点所需资源" prop="chance" label-width="160px">
          <el-input v-model="form.chance" placeholder="请输入机会点/困难点所需资源" type="textarea" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="pageState == 'see'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listGather,
    getGather,
    delGather,
    addGather,
    updateGather,
    upload
  } from "@/api/business/spd/bo/gather";
  import {
    getToken
  } from "@/utils/auth";

  export default {
    name: "Gather",
    components: {
      ElPopoverSelectV2: () =>
        import("@/components/popover-select-v2"),
      ElPopoverTreeSelect: () =>
        import("@/components/popover-tree-select/index.vue"),
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 竞争产品信息收集表格数据
        gatherList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          customer: null,
          customerName: null,
          productionLine: null,
          productionLineName: null,
          rival: null,
          rivalName: null,
          rivalBulk: null,
          brand: null,
          brandPortion: null,
          cooperationStart: null,
          cooperationEnd: null,
          sectionKeyPsnname: null,
          sectionKeyPsnphone: null,
          hospitalKeyPsnname: null,
          hospitalKeyPsnphone: null,
          rivalKeyPsnname: null,
          rivalKeyPsnphone: null,
          idea: null,
          chance: null,
          tenantId: null,
          revision: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        // 页面状态
        pageState: null,
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询竞争产品信息收集列表 */
      getList() {
        this.loading = true;
        listGather(this.queryParams).then(response => {
          this.gatherList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          customer: null,
          customerName: null,
          productionLine: null,
          productionLineName: null,
          rival: null,
          rivalName: null,
          rivalBulk: null,
          brand: null,
          brandPortion: null,
          cooperationStart: null,
          cooperationEnd: null,
          sectionKeyPsnname: null,
          sectionKeyPsnphone: null,
          hospitalKeyPsnname: null,
          hospitalKeyPsnphone: null,
          rivalKeyPsnname: null,
          rivalKeyPsnphone: null,
          idea: null,
          chance: null,
          remark: null,
          tenantId: null,
          revision: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          delFlag: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加竞争产品信息收集";
        this.pageState = 'insert';
      },
      /** 模板下载按钮操作 */
      handleDownload() {
        this.download('mk/bo/gathercg/download', {}, `竞争对手信息收集导入模板_${new Date().getTime()}.xlsx`)
      },
      /** 导入按钮操作 */
      handleUpload(file) {
        this.loading = true;
        let formData = new FormData()
        formData.append('file', file.file)
        upload(formData).then((res) => {
          console.log('res', res);
          if (res.code == '200') {
            this.resetQuery();
            if (res.msg.indexOf('导入失败') >= 0) {
            this.$message({
              duration: 5000,
              message: res.msg
            });
            } else {
            this.$message.success(res.msg);
            }
          } else {
            this.$message.error(res.msg);
          }
          this.loading = false;
        }).catch((e) => {
          this.$message.error(e.message)
        }).finally((e) => {
          this.$refs['upload'].clearFiles();
          this.loading = false;
        })
      },
      /** 查看按钮操作 */
      handleSee(row) {
        this.reset();
        const id = row.id || this.ids
        getGather(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看竞争产品信息收集";
          this.pageState = 'see';
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getGather(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改竞争产品信息收集";
          this.pageState = 'update';
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateGather(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addGather(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        if (row.id != undefined) {
          this.ids.push(row.id);
        }
        const ids = this.ids;
        this.$modal.confirm('是否确认删除？').then(function () {
          return delGather(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('mk/bo/gathercg/export', {
          ...this.queryParams
        }, `gather_${new Date().getTime()}.xlsx`)
      },
    }
  };

</script>
